Make public and reuse IOUtils.EMPTY_BYTE_ARRAY.
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -169,6 +169,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add and use IOUtils.byteArray(*).
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Make public and reuse IOUtils.EMPTY_BYTE_ARRAY.
+ </action>
<!-- UPDATES -->
<action dev="ggregory" type="update" due-to="Dependabot">
Update junit-jupiter from 5.6.2 to 5.7.0 #153.

MODIFY
src/main/java/org/apache/commons/io/IOUtils.java
src/main/java/org/apache/commons/io/IOUtils.java
// Writer. Each method should take at least one of these as a parameter,
// or return one of them.
- private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
+ /**
+ * A singleton empty byte array.
+ *
+ * @since 2.9.0
+ */
+ public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
/**
* The default buffer size ({@value}) to use in copy methods.

MODIFY
src/main/java/org/apache/commons/io/output/AbstractByteArrayOutputStream.java
src/main/java/org/apache/commons/io/output/AbstractByteArrayOutputStream.java
static final int DEFAULT_SIZE = 1024;
- /** A singleton empty byte array. */
- private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
-
/** The list of buffers, which grows and never reduces. */
private final List<byte[]> buffers = new ArrayList<>();
/** The index of the current buffer. */
@@ -363,7 +360,7 @@ protected void writeToImpl(final OutputStream out) throws IOException {
protected byte[] toByteArrayImpl() {
int remaining = count;
if (remaining == 0) {
- return EMPTY_BYTE_ARRAY;
+ return IOUtils.EMPTY_BYTE_ARRAY;
}
final byte[] newbuf = IOUtils.byteArray(remaining);
int pos = 0;

MODIFY
src/test/java/org/apache/commons/io/CharsetsTestCase.java
src/test/java/org/apache/commons/io/CharsetsTestCase.java
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.SortedMap;
+
import org.junit.jupiter.api.Test;
/**

MODIFY
src/test/java/org/apache/commons/io/FileUtilsDeleteDirectoryBaseTestCase.java
src/test/java/org/apache/commons/io/FileUtilsDeleteDirectoryBaseTestCase.java
import java.io.File;
import java.nio.file.Files;
+
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

MODIFY
src/test/java/org/apache/commons/io/file/PathUtilsDeleteFileTest.java
src/test/java/org/apache/commons/io/file/PathUtilsDeleteFileTest.java
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
+
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
+
import org.apache.commons.io.file.Counters.PathCounters;
import org.apache.commons.lang3.SystemUtils;
import org.junit.jupiter.api.AfterEach;

MODIFY
src/test/java/org/apache/commons/io/function/IOFunctionTest.java
src/test/java/org/apache/commons/io/function/IOFunctionTest.java
package org.apache.commons.io.function;
-import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.function.Function;
import java.util.function.Supplier;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertThrows;
+import org.junit.jupiter.api.Test;
public class IOFunctionTest {

MODIFY
src/test/java/org/apache/commons/io/input/BoundedInputStreamTest.java
src/test/java/org/apache/commons/io/input/BoundedInputStreamTest.java
@@ -68,7 +68,7 @@ public void testReadArray() throws Exception {
compare("limit = -1", helloWorld, IOUtils.toByteArray(bounded));
bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), 0);
- compare("limit = 0", new byte[0], IOUtils.toByteArray(bounded));
+ compare("limit = 0", IOUtils.EMPTY_BYTE_ARRAY, IOUtils.toByteArray(bounded));
bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length);
compare("limit = length", helloWorld, IOUtils.toByteArray(bounded));

MODIFY
src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java
src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java
@@ -118,7 +118,7 @@ public void testResetting() throws Exception {
@Test
public void testZeroLength1() throws Exception {
- final ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
+ final ByteArrayInputStream bais = new ByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY);
try (final CountingInputStream cis = new CountingInputStream(bais)) {
final int found = cis.read();
@@ -129,7 +129,7 @@ public void testZeroLength1() throws Exception {
@Test
public void testZeroLength2() throws Exception {
- final ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
+ final ByteArrayInputStream bais = new ByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY);
try (final CountingInputStream cis = new CountingInputStream(bais)) {
final byte[] result = new byte[10];
@@ -142,7 +142,7 @@ public void testZeroLength2() throws Exception {
@Test
public void testZeroLength3() throws Exception {
- final ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
+ final ByteArrayInputStream bais = new ByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY);
try (final CountingInputStream cis = new CountingInputStream(bais)) {
final byte[] result = new byte[10];

MODIFY
src/test/java/org/apache/commons/io/input/MarkShieldInputStreamTest.java
src/test/java/org/apache/commons/io/input/MarkShieldInputStreamTest.java
*/
package org.apache.commons.io.input;
-import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.io.InputStream;
-import static org.junit.jupiter.api.Assertions.*;
+import org.junit.jupiter.api.Test;
public class MarkShieldInputStreamTest {

MODIFY
src/test/java/org/apache/commons/io/input/QueueInputStreamTest.java
src/test/java/org/apache/commons/io/input/QueueInputStreamTest.java
*/
package org.apache.commons.io.input;
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.io.output.QueueOutputStream;
-import org.apache.commons.io.output.QueueOutputStreamTest;
-import org.apache.commons.lang3.StringUtils;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.params.ParameterizedTest;
-import org.junit.jupiter.params.provider.Arguments;
-import org.junit.jupiter.params.provider.MethodSource;
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.stream.Stream;
-import static java.nio.charset.StandardCharsets.UTF_8;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertThrows;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.output.QueueOutputStream;
+import org.apache.commons.io.output.QueueOutputStreamTest;
+import org.apache.commons.lang3.StringUtils;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
/**
* Test {@link QueueInputStream}.

MODIFY
src/test/java/org/apache/commons/io/input/TimestampedObserverTest.java
src/test/java/org/apache/commons/io/input/TimestampedObserverTest.java
package org.apache.commons.io.input;
-import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.ByteArrayInputStream;

MODIFY
src/test/java/org/apache/commons/io/input/UnsynchronizedByteArrayInputStreamTest.java
src/test/java/org/apache/commons/io/input/UnsynchronizedByteArrayInputStreamTest.java
*/
package org.apache.commons.io.input;
-import org.junit.jupiter.api.Test;
-
import static org.apache.commons.io.input.UnsynchronizedByteArrayInputStream.END_OF_STREAM;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.IOException;
+import org.apache.commons.io.IOUtils;
+import org.junit.jupiter.api.Test;
+
/**
* Basic unit tests for the alternative ByteArrayInputStream implementation.
*/
@Test
public void testConstructor1() throws IOException {
- final byte[] empty = new byte[0];
+ final byte[] empty = IOUtils.EMPTY_BYTE_ARRAY;
final byte[] one = new byte[1];
final byte[] some = new byte[25];
@@ -53,7 +54,7 @@ public void testConstructor1() throws IOException {
@Test
@SuppressWarnings("resource") // not necessary to close these resources
public void testConstructor2() {
- final byte[] empty = new byte[0];
+ final byte[] empty = IOUtils.EMPTY_BYTE_ARRAY;
final byte[] one = new byte[1];
final byte[] some = new byte[25];
@@ -82,7 +83,7 @@ public void testConstructor2() {
@Test
@SuppressWarnings("resource") // not necessary to close these resources
public void testConstructor3() {
- final byte[] empty = new byte[0];
+ final byte[] empty = IOUtils.EMPTY_BYTE_ARRAY;
final byte[] one = new byte[1];
final byte[] some = new byte[25];
@@ -137,28 +138,28 @@ public void testConstructor3() {
@Test
public void testInvalidConstructor2OffsetUnder() {
assertThrows(IllegalArgumentException.class, () -> {
- new UnsynchronizedByteArrayInputStream(new byte[0], -1);
+ new UnsynchronizedByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY, -1);
});
}
@Test
public void testInvalidConstructor3LengthUnder() {
assertThrows(IllegalArgumentException.class, () -> {
- new UnsynchronizedByteArrayInputStream(new byte[0], 0, -1);
+ new UnsynchronizedByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY, 0, -1);
});
}
@Test
public void testInvalidConstructor3OffsetUnder() {
assertThrows(IllegalArgumentException.class, () -> {
- new UnsynchronizedByteArrayInputStream(new byte[0], -1, 1);
+ new UnsynchronizedByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY, -1, 1);
});
}
@Test
@SuppressWarnings("resource") // not necessary to close these resources
public void testInvalidReadArrayExplicitLenUnder() {
- final byte[] buf = new byte[0];
+ final byte[] buf = IOUtils.EMPTY_BYTE_ARRAY;
final UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc});
assertThrows(IndexOutOfBoundsException.class, () -> {
is.read(buf, 0, -1);
@@ -167,7 +168,7 @@ public void testInvalidReadArrayExplicitLenUnder() {
@Test
public void testInvalidReadArrayExplicitOffsetUnder() {
- final byte[] buf = new byte[0];
+ final byte[] buf = IOUtils.EMPTY_BYTE_ARRAY;
@SuppressWarnings("resource") // not necessary to close these resources
final UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc});
assertThrows(IndexOutOfBoundsException.class, () -> {
@@ -177,7 +178,7 @@ public void testInvalidReadArrayExplicitOffsetUnder() {
@Test
public void testInvalidReadArrayExplicitRangeOver() {
- final byte[] buf = new byte[0];
+ final byte[] buf = IOUtils.EMPTY_BYTE_ARRAY;
@SuppressWarnings("resource") // not necessary to close these resources
final UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc});
assertThrows(IndexOutOfBoundsException.class, () -> {
@@ -234,12 +235,12 @@ public void testMarkReset() {
public void testReadArray() {
byte[] buf = new byte[10];
@SuppressWarnings("resource") // not necessary to close these resources
- UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[0]);
+ UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY);
int read = is.read(buf);
assertEquals(END_OF_STREAM, read);
assertArrayEquals(new byte[10], buf);
- buf = new byte[0];
+ buf = IOUtils.EMPTY_BYTE_ARRAY;
is = new UnsynchronizedByteArrayInputStream(new byte[]{(byte) 0xa, (byte) 0xb, (byte) 0xc});
read = is.read(buf);
assertEquals(0, read);
@@ -268,24 +269,24 @@ public void testReadArray() {
public void testReadArrayExplicit() {
byte[] buf = new byte[10];
@SuppressWarnings("resource") // not necessary to close these resources
- UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[0]);
+ UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY);
int read = is.read(buf, 0, 10);
assertEquals(END_OF_STREAM, read);
assertArrayEquals(new byte[10], buf);
buf = new byte[10];
- is = new UnsynchronizedByteArrayInputStream(new byte[0]);
+ is = new UnsynchronizedByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY);
read = is.read(buf, 4, 2);
assertEquals(END_OF_STREAM, read);
assertArrayEquals(new byte[10], buf);
buf = new byte[10];
- is = new UnsynchronizedByteArrayInputStream(new byte[0]);
+ is = new UnsynchronizedByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY);
read = is.read(buf, 4, 6);
assertEquals(END_OF_STREAM, read);
assertArrayEquals(new byte[10], buf);
- buf = new byte[0];
+ buf = IOUtils.EMPTY_BYTE_ARRAY;
is = new UnsynchronizedByteArrayInputStream(new byte[]{(byte) 0xa, (byte) 0xb, (byte) 0xc});
read = is.read(buf, 0,0);
assertEquals(0, read);
@@ -305,7 +306,7 @@ public void testReadArrayExplicit() {
@Test
public void testReadSingle() {
@SuppressWarnings("resource") // not necessary to close these resources
- UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(new byte[0]);
+ UnsynchronizedByteArrayInputStream is = new UnsynchronizedByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY);
assertEquals(END_OF_STREAM, is.read());
is = new UnsynchronizedByteArrayInputStream(new byte[] {(byte)0xa, (byte)0xb, (byte)0xc});

MODIFY
src/test/java/org/apache/commons/io/output/ByteArrayOutputStreamTestCase.java
src/test/java/org/apache/commons/io/output/ByteArrayOutputStreamTestCase.java
*/
package org.apache.commons.io.output;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
+
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
-import static org.junit.jupiter.api.Assertions.assertArrayEquals;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertSame;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
-
/**
* Basic unit tests for the alternative ByteArrayOutputStream implementations.
*/
@@ -106,7 +106,7 @@ private void checkStreams(
@MethodSource("baosFactories")
public void testWriteZero(final String baosName, final BAOSFactory<?> baosFactory) throws IOException {
try (final AbstractByteArrayOutputStream baout = baosFactory.newInstance()) {
- baout.write(new byte[0], 0, 0);
+ baout.write(IOUtils.EMPTY_BYTE_ARRAY, 0, 0);
assertTrue(true, "Dummy");
}
}
@@ -123,7 +123,7 @@ public void testInvalidWriteOffsetUnder(final String baosName, final BAOSFactory
@MethodSource("baosFactories")
public void testInvalidWriteOffsetOver(final String baosName, final BAOSFactory<?> baosFactory) throws IOException {
try (final AbstractByteArrayOutputStream baout = baosFactory.newInstance()) {
- assertThrows(IndexOutOfBoundsException.class, () -> baout.write(new byte[0], 1, 0));
+ assertThrows(IndexOutOfBoundsException.class, () -> baout.write(IOUtils.EMPTY_BYTE_ARRAY, 1, 0));
}
}
@@ -174,7 +174,7 @@ public void testToInputStreamEmpty(final String baosName, final BAOSFactory<?> b
@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("toBufferedInputStreamFunctionFactories")
public void testToBufferedInputStreamEmpty(final String baosName, final IOFunction<InputStream, InputStream> toBufferedInputStreamFunction) throws IOException {
- try (final ByteArrayInputStream bain = new ByteArrayInputStream(new byte[0])) {
+ try (final ByteArrayInputStream bain = new ByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY)) {
assertEquals(0, bain.available());
try (final InputStream buffered = toBufferedInputStreamFunction.apply(bain)) {

MODIFY
src/test/java/org/apache/commons/io/output/QueueOutputStreamTest.java
src/test/java/org/apache/commons/io/output/QueueOutputStreamTest.java
*/
package org.apache.commons.io.output;
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.io.input.QueueInputStream;
-import org.apache.commons.io.input.QueueInputStreamTest;
-import org.junit.jupiter.api.AfterAll;
-import org.junit.jupiter.api.Test;
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.InterruptedIOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
-import static java.nio.charset.StandardCharsets.UTF_8;
-import static java.util.concurrent.TimeUnit.SECONDS;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.assertThrows;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.input.QueueInputStream;
+import org.apache.commons.io.input.QueueInputStreamTest;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Test;
/**
* Test {@link QueueOutputStream} and {@link QueueInputStream}

Make public and reuse IOUtils.EMPTY_BYTE_ARRAY.