Skip to content

Commit e59563d

Browse files
author
Gary Gregory
committed
Add and use IOUtils.byteArray(*).
1 parent 31668fd commit e59563d

15 files changed

Lines changed: 58 additions & 36 deletions

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ The <action> type attribute can be add,update,fix,remove.
166166
<action issue="IO-706" dev="ggregory" type="add" due-to="Gary Gregory">
167167
Add TimetampedObserver.
168168
</action>
169+
<action dev="ggregory" type="add" due-to="Gary Gregory">
170+
Add and use IOUtils.byteArray(*).
171+
</action>
169172
<!-- UPDATES -->
170173
<action dev="ggregory" type="update" due-to="Dependabot">
171174
Update junit-jupiter from 5.6.2 to 5.7.0 #153.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public int get(final int pos) {
122122
* @return a copy of the BOM's bytes
123123
*/
124124
public byte[] getBytes() {
125-
final byte[] copy = new byte[bytes.length];
125+
final byte[] copy = IOUtils.byteArray(bytes.length);
126126
for (int i = 0; i < bytes.length; i++) {
127127
copy[i] = (byte)bytes[i];
128128
}

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,6 @@
113113
@Deprecated
114114
public class CopyUtils {
115115

116-
/**
117-
* The default size of the buffer.
118-
*/
119-
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
120-
121116
/**
122117
* Instances should NOT be constructed in standard programming.
123118
*/
@@ -172,7 +167,7 @@ public static void copy(final byte[] input, final Writer output, final String en
172167
* @throws IOException In case of an I/O problem
173168
*/
174169
public static int copy(final InputStream input, final OutputStream output) throws IOException {
175-
final byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
170+
final byte[] buffer = IOUtils.byteArray();
176171
int count = 0;
177172
int n = 0;
178173
while (EOF != (n = input.read(buffer))) {
@@ -197,7 +192,7 @@ public static int copy(
197192
final Reader input,
198193
final Writer output)
199194
throws IOException {
200-
final char[] buffer = new char[DEFAULT_BUFFER_SIZE];
195+
final char[] buffer = new char[IOUtils.DEFAULT_BUFFER_SIZE];
201196
int count = 0;
202197
int n = 0;
203198
while (EOF != (n = input.read(buffer))) {

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

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public class IOUtils {
172172
/**
173173
* The default buffer to use for the skip() methods.
174174
*/
175-
private static final byte[] SKIP_BYTE_BUFFER = new byte[DEFAULT_BUFFER_SIZE];
175+
private static final byte[] SKIP_BYTE_BUFFER = byteArray();
176176

177177
// Allocated in the relevant skip method if necessary.
178178
/*
@@ -315,6 +315,29 @@ public static BufferedWriter buffer(final Writer writer, final int size) {
315315
return writer instanceof BufferedWriter ? (BufferedWriter) writer : new BufferedWriter(writer, size);
316316
}
317317

318+
/**
319+
* Returns a new byte array of size {@link #DEFAULT_BUFFER_SIZE}.
320+
*
321+
* @return a new byte array of size {@link #DEFAULT_BUFFER_SIZE}.
322+
* @since 2.9.0
323+
*/
324+
public static byte[] byteArray() {
325+
return byteArray(DEFAULT_BUFFER_SIZE);
326+
}
327+
328+
/**
329+
* Returns a new byte array of the given size.
330+
*
331+
* TODO Consider guarding or warning against large allocations...
332+
*
333+
* @param size array size.
334+
* @return a new byte array of the given size.
335+
* @since 2.9.0
336+
*/
337+
public static byte[] byteArray(final int size) {
338+
return new byte[size];
339+
}
340+
318341
/**
319342
* Closes the given {@link Closeable} as a null-safe operation.
320343
*
@@ -754,8 +777,8 @@ public static boolean contentEquals(final InputStream input1, final InputStream
754777
return false;
755778
}
756779

757-
final byte[] array1 = new byte[DEFAULT_BUFFER_SIZE];
758-
final byte[] array2 = new byte[DEFAULT_BUFFER_SIZE];
780+
final byte[] array1 = byteArray();
781+
final byte[] array2 = byteArray();
759782
int pos1;
760783
int pos2;
761784
int count1;
@@ -925,7 +948,7 @@ public static int copy(final InputStream inputStream, final OutputStream outputS
925948
*/
926949
public static long copy(final InputStream inputStream, final OutputStream outputStream, final int bufferSize)
927950
throws IOException {
928-
return copyLarge(inputStream, outputStream, new byte[bufferSize]);
951+
return copyLarge(inputStream, outputStream, IOUtils.byteArray(bufferSize));
929952
}
930953

931954
/**
@@ -1243,7 +1266,7 @@ public static long copyLarge(final InputStream inputStream, final OutputStream o
12431266
*/
12441267
public static long copyLarge(final InputStream input, final OutputStream output, final long inputOffset,
12451268
final long length) throws IOException {
1246-
return copyLarge(input, output, inputOffset, length, new byte[DEFAULT_BUFFER_SIZE]);
1269+
return copyLarge(input, output, inputOffset, length, byteArray());
12471270
}
12481271

12491272
/**
@@ -1730,7 +1753,7 @@ public static void readFully(final InputStream input, final byte[] buffer, final
17301753
* @since 2.5
17311754
*/
17321755
public static byte[] readFully(final InputStream input, final int length) throws IOException {
1733-
final byte[] buffer = new byte[length];
1756+
final byte[] buffer = IOUtils.byteArray(length);
17341757
readFully(input, buffer, 0, buffer.length);
17351758
return buffer;
17361759
}
@@ -2317,7 +2340,7 @@ public static byte[] toByteArray(final InputStream input, final int size) throws
23172340
return EMPTY_BYTE_ARRAY;
23182341
}
23192342

2320-
final byte[] data = new byte[size];
2343+
final byte[] data = IOUtils.byteArray(size);
23212344
int offset = 0;
23222345
int read;
23232346

src/main/java/org/apache/commons/io/filefilter/MagicNumberFileFilter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.nio.file.attribute.BasicFileAttributes;
3030
import java.util.Arrays;
3131

32+
import org.apache.commons.io.IOUtils;
33+
3234
/**
3335
* <p>
3436
* File filter for matching files containing a "magic number". A magic number
@@ -171,7 +173,7 @@ public MagicNumberFileFilter(final byte[] magicNumber, final long offset) {
171173
throw new IllegalArgumentException("The offset cannot be negative");
172174
}
173175

174-
this.magicNumbers = new byte[magicNumber.length];
176+
this.magicNumbers = IOUtils.byteArray(magicNumber.length);
175177
System.arraycopy(magicNumber, 0, this.magicNumbers, 0, magicNumber.length);
176178
this.byteOffset = offset;
177179
}
@@ -258,7 +260,7 @@ public boolean accept(final File file) {
258260
if (file != null && file.isFile() && file.canRead()) {
259261
try {
260262
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
261-
final byte[] fileBytes = new byte[this.magicNumbers.length];
263+
final byte[] fileBytes = IOUtils.byteArray(this.magicNumbers.length);
262264
randomAccessFile.seek(byteOffset);
263265
final int read = randomAccessFile.read(fileBytes);
264266
if (read != magicNumbers.length) {

src/main/java/org/apache/commons/io/input/ObservableInputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public void close() throws IOException {
164164
* @throws IOException The underlying {@link InputStream}, or either of the observers has thrown an exception.
165165
*/
166166
public void consume() throws IOException {
167-
final byte[] buffer = new byte[IOUtils.DEFAULT_BUFFER_SIZE];
167+
final byte[] buffer = IOUtils.byteArray();
168168
while (read(buffer) != EOF) {
169169
// empty
170170
}

src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private void createLeftOver() {
9090
final int lineLengthBytes = currentLastBytePos + 1;
9191
if (lineLengthBytes > 0) {
9292
// create left over for next block
93-
leftOver = new byte[lineLengthBytes];
93+
leftOver = IOUtils.byteArray(lineLengthBytes);
9494
System.arraycopy(data, 0, leftOver, 0, lineLengthBytes);
9595
} else {
9696
leftOver = null;
@@ -149,7 +149,7 @@ private String readLine() {
149149
if (lineLengthBytes < 0) {
150150
throw new IllegalStateException("Unexpected negative line length=" + lineLengthBytes);
151151
}
152-
final byte[] lineData = new byte[lineLengthBytes];
152+
final byte[] lineData = IOUtils.byteArray(lineLengthBytes);
153153
System.arraycopy(data, lineStart, lineData, 0, lineLengthBytes);
154154

155155
line = new String(lineData, charset);

src/main/java/org/apache/commons/io/input/Tailer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public Tailer(final File file, final Charset charset, final TailerListener liste
259259
this.delayMillis = delayMillis;
260260
this.end = end;
261261

262-
this.inbuf = new byte[bufSize];
262+
this.inbuf = IOUtils.byteArray(bufSize);
263263

264264
// Save and prepare the listener
265265
this.listener = listener;

src/main/java/org/apache/commons/io/input/XmlStreamReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private static String getXmlProlog(final InputStream inputStream, final String g
186186
throws IOException {
187187
String encoding = null;
188188
if (guessedEnc != null) {
189-
final byte[] bytes = new byte[IOUtils.DEFAULT_BUFFER_SIZE];
189+
final byte[] bytes = IOUtils.byteArray();
190190
inputStream.mark(IOUtils.DEFAULT_BUFFER_SIZE);
191191
int offset = 0;
192192
int max = IOUtils.DEFAULT_BUFFER_SIZE;

src/main/java/org/apache/commons/io/input/buffer/CircularBufferInputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ protected void fillBuffer() throws IOException {
7878
return;
7979
}
8080
int space = buffer.getSpace();
81-
final byte[] buf = new byte[space];
81+
final byte[] buf = IOUtils.byteArray(space);
8282
while (space > 0) {
8383
final int res = in.read(buf, 0, space);
8484
if (res == EOF) {

0 commit comments

Comments
 (0)