Skip to content

Commit 29e4f85

Browse files
author
Gary Gregory
committed
Make public and reuse IOUtils.EMPTY_BYTE_ARRAY.
1 parent e59563d commit 29e4f85

15 files changed

Lines changed: 77 additions & 64 deletions

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ The <action> type attribute can be add,update,fix,remove.
169169
<action dev="ggregory" type="add" due-to="Gary Gregory">
170170
Add and use IOUtils.byteArray(*).
171171
</action>
172+
<action dev="ggregory" type="add" due-to="Gary Gregory">
173+
Make public and reuse IOUtils.EMPTY_BYTE_ARRAY.
174+
</action>
172175
<!-- UPDATES -->
173176
<action dev="ggregory" type="update" due-to="Dependabot">
174177
Update junit-jupiter from 5.6.2 to 5.7.0 #153.

src/main/java/org/apache/commons/io/IOUtils.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,12 @@ public class IOUtils {
105105
// Writer. Each method should take at least one of these as a parameter,
106106
// or return one of them.
107107

108-
private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
108+
/**
109+
* A singleton empty byte array.
110+
*
111+
* @since 2.9.0
112+
*/
113+
public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
109114

110115
/**
111116
* The default buffer size ({@value}) to use in copy methods.

src/main/java/org/apache/commons/io/output/AbstractByteArrayOutputStream.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ public abstract class AbstractByteArrayOutputStream extends OutputStream {
6161

6262
static final int DEFAULT_SIZE = 1024;
6363

64-
/** A singleton empty byte array. */
65-
private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
66-
6764
/** The list of buffers, which grows and never reduces. */
6865
private final List<byte[]> buffers = new ArrayList<>();
6966
/** The index of the current buffer. */
@@ -363,7 +360,7 @@ protected interface InputStreamConstructor<T extends InputStream> {
363360
protected byte[] toByteArrayImpl() {
364361
int remaining = count;
365362
if (remaining == 0) {
366-
return EMPTY_BYTE_ARRAY;
363+
return IOUtils.EMPTY_BYTE_ARRAY;
367364
}
368365
final byte[] newbuf = IOUtils.byteArray(remaining);
369366
int pos = 0;

src/test/java/org/apache/commons/io/CharsetsTestCase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.nio.charset.Charset;
2323
import java.nio.charset.StandardCharsets;
2424
import java.util.SortedMap;
25+
2526
import org.junit.jupiter.api.Test;
2627

2728
/**

src/test/java/org/apache/commons/io/FileUtilsDeleteDirectoryBaseTestCase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.io.File;
2424
import java.nio.file.Files;
25+
2526
import org.junit.jupiter.api.Test;
2627
import org.junit.jupiter.api.io.TempDir;
2728

src/test/java/org/apache/commons/io/file/PathUtilsDeleteFileTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
import static org.junit.jupiter.api.Assertions.assertThrows;
2323
import static org.junit.jupiter.api.Assertions.assertTrue;
2424
import static org.junit.jupiter.api.Assumptions.assumeFalse;
25+
2526
import java.io.IOException;
2627
import java.nio.file.Files;
2728
import java.nio.file.LinkOption;
2829
import java.nio.file.NoSuchFileException;
2930
import java.nio.file.Path;
3031
import java.nio.file.Paths;
32+
3133
import org.apache.commons.io.file.Counters.PathCounters;
3234
import org.apache.commons.lang3.SystemUtils;
3335
import org.junit.jupiter.api.AfterEach;

src/test/java/org/apache/commons/io/function/IOFunctionTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
package org.apache.commons.io.function;
1919

20-
import org.junit.jupiter.api.Test;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
2122

2223
import java.io.ByteArrayInputStream;
2324
import java.io.IOException;
@@ -26,8 +27,7 @@
2627
import java.util.function.Function;
2728
import java.util.function.Supplier;
2829

29-
import static org.junit.jupiter.api.Assertions.assertEquals;
30-
import static org.junit.jupiter.api.Assertions.assertThrows;
30+
import org.junit.jupiter.api.Test;
3131

3232
public class IOFunctionTest {
3333

src/test/java/org/apache/commons/io/input/BoundedInputStreamTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void testReadArray() throws Exception {
6868
compare("limit = -1", helloWorld, IOUtils.toByteArray(bounded));
6969

7070
bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), 0);
71-
compare("limit = 0", new byte[0], IOUtils.toByteArray(bounded));
71+
compare("limit = 0", IOUtils.EMPTY_BYTE_ARRAY, IOUtils.toByteArray(bounded));
7272

7373
bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length);
7474
compare("limit = length", helloWorld, IOUtils.toByteArray(bounded));

src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public void testResetting() throws Exception {
118118

119119
@Test
120120
public void testZeroLength1() throws Exception {
121-
final ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
121+
final ByteArrayInputStream bais = new ByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY);
122122
try (final CountingInputStream cis = new CountingInputStream(bais)) {
123123

124124
final int found = cis.read();
@@ -129,7 +129,7 @@ public void testZeroLength1() throws Exception {
129129

130130
@Test
131131
public void testZeroLength2() throws Exception {
132-
final ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
132+
final ByteArrayInputStream bais = new ByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY);
133133
try (final CountingInputStream cis = new CountingInputStream(bais)) {
134134

135135
final byte[] result = new byte[10];
@@ -142,7 +142,7 @@ public void testZeroLength2() throws Exception {
142142

143143
@Test
144144
public void testZeroLength3() throws Exception {
145-
final ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
145+
final ByteArrayInputStream bais = new ByteArrayInputStream(IOUtils.EMPTY_BYTE_ARRAY);
146146
try (final CountingInputStream cis = new CountingInputStream(bais)) {
147147

148148
final byte[] result = new byte[10];

src/test/java/org/apache/commons/io/input/MarkShieldInputStreamTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
*/
1717
package org.apache.commons.io.input;
1818

19-
import org.junit.jupiter.api.Test;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
22+
import static org.junit.jupiter.api.Assertions.assertTrue;
2023

2124
import java.io.IOException;
2225
import java.io.InputStream;
2326

24-
import static org.junit.jupiter.api.Assertions.*;
27+
import org.junit.jupiter.api.Test;
2528

2629
public class MarkShieldInputStreamTest {
2730

0 commit comments

Comments
 (0)