Add and use IOUtils.byteArray(*).
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -166,6 +166,9 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="IO-706" dev="ggregory" type="add" due-to="Gary Gregory">
Add TimetampedObserver.
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add and use IOUtils.byteArray(*).
+ </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/ByteOrderMark.java
src/main/java/org/apache/commons/io/ByteOrderMark.java
@@ -122,7 +122,7 @@ public int get(final int pos) {
* @return a copy of the BOM's bytes
*/
public byte[] getBytes() {
- final byte[] copy = new byte[bytes.length];
+ final byte[] copy = IOUtils.byteArray(bytes.length);
for (int i = 0; i < bytes.length; i++) {
copy[i] = (byte)bytes[i];
}

MODIFY
src/main/java/org/apache/commons/io/CopyUtils.java
src/main/java/org/apache/commons/io/CopyUtils.java
@Deprecated
public class CopyUtils {
- /**
- * The default size of the buffer.
- */
- private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
-
/**
* Instances should NOT be constructed in standard programming.
*/
@@ -172,7 +167,7 @@ public static void copy(final byte[] input, final Writer output, final String en
* @throws IOException In case of an I/O problem
*/
public static int copy(final InputStream input, final OutputStream output) throws IOException {
- final byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
+ final byte[] buffer = IOUtils.byteArray();
int count = 0;
int n = 0;
while (EOF != (n = input.read(buffer))) {
@@ -197,7 +192,7 @@ public static int copy(
final Reader input,
final Writer output)
throws IOException {
- final char[] buffer = new char[DEFAULT_BUFFER_SIZE];
+ final char[] buffer = new char[IOUtils.DEFAULT_BUFFER_SIZE];
int count = 0;
int n = 0;
while (EOF != (n = input.read(buffer))) {

MODIFY
src/main/java/org/apache/commons/io/IOUtils.java
src/main/java/org/apache/commons/io/IOUtils.java
/**
* The default buffer to use for the skip() methods.
*/
- private static final byte[] SKIP_BYTE_BUFFER = new byte[DEFAULT_BUFFER_SIZE];
+ private static final byte[] SKIP_BYTE_BUFFER = byteArray();
// Allocated in the relevant skip method if necessary.
/*
@@ -315,6 +315,29 @@ public static BufferedWriter buffer(final Writer writer, final int size) {
return writer instanceof BufferedWriter ? (BufferedWriter) writer : new BufferedWriter(writer, size);
}
+ /**
+ * Returns a new byte array of size {@link #DEFAULT_BUFFER_SIZE}.
+ *
+ * @return a new byte array of size {@link #DEFAULT_BUFFER_SIZE}.
+ * @since 2.9.0
+ */
+ public static byte[] byteArray() {
+ return byteArray(DEFAULT_BUFFER_SIZE);
+ }
+
+ /**
+ * Returns a new byte array of the given size.
+ *
+ * TODO Consider guarding or warning against large allocations...
+ *
+ * @param size array size.
+ * @return a new byte array of the given size.
+ * @since 2.9.0
+ */
+ public static byte[] byteArray(final int size) {
+ return new byte[size];
+ }
+
/**
* Closes the given {@link Closeable} as a null-safe operation.
*
@@ -754,8 +777,8 @@ public static boolean contentEquals(final InputStream input1, final InputStream
return false;
}
- final byte[] array1 = new byte[DEFAULT_BUFFER_SIZE];
- final byte[] array2 = new byte[DEFAULT_BUFFER_SIZE];
+ final byte[] array1 = byteArray();
+ final byte[] array2 = byteArray();
int pos1;
int pos2;
int count1;
@@ -925,7 +948,7 @@ public static int copy(final InputStream inputStream, final OutputStream outputS
*/
public static long copy(final InputStream inputStream, final OutputStream outputStream, final int bufferSize)
throws IOException {
- return copyLarge(inputStream, outputStream, new byte[bufferSize]);
+ return copyLarge(inputStream, outputStream, IOUtils.byteArray(bufferSize));
}
/**
@@ -1243,7 +1266,7 @@ public static long copyLarge(final InputStream inputStream, final OutputStream o
*/
public static long copyLarge(final InputStream input, final OutputStream output, final long inputOffset,
final long length) throws IOException {
- return copyLarge(input, output, inputOffset, length, new byte[DEFAULT_BUFFER_SIZE]);
+ return copyLarge(input, output, inputOffset, length, byteArray());
}
/**
@@ -1730,7 +1753,7 @@ public static void readFully(final InputStream input, final byte[] buffer, final
* @since 2.5
*/
public static byte[] readFully(final InputStream input, final int length) throws IOException {
- final byte[] buffer = new byte[length];
+ final byte[] buffer = IOUtils.byteArray(length);
readFully(input, buffer, 0, buffer.length);
return buffer;
}
@@ -2317,7 +2340,7 @@ public static BufferedReader toBufferedReader(final Reader reader, final int siz
return EMPTY_BYTE_ARRAY;
}
- final byte[] data = new byte[size];
+ final byte[] data = IOUtils.byteArray(size);
int offset = 0;
int read;

MODIFY
src/main/java/org/apache/commons/io/filefilter/MagicNumberFileFilter.java
src/main/java/org/apache/commons/io/filefilter/MagicNumberFileFilter.java
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
+import org.apache.commons.io.IOUtils;
+
/**
* <p>
* File filter for matching files containing a "magic number". A magic number
@@ -171,7 +173,7 @@ public MagicNumberFileFilter(final byte[] magicNumber, final long offset) {
throw new IllegalArgumentException("The offset cannot be negative");
}
- this.magicNumbers = new byte[magicNumber.length];
+ this.magicNumbers = IOUtils.byteArray(magicNumber.length);
System.arraycopy(magicNumber, 0, this.magicNumbers, 0, magicNumber.length);
this.byteOffset = offset;
}
@@ -258,7 +260,7 @@ public boolean accept(final File file) {
if (file != null && file.isFile() && file.canRead()) {
try {
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
- final byte[] fileBytes = new byte[this.magicNumbers.length];
+ final byte[] fileBytes = IOUtils.byteArray(this.magicNumbers.length);
randomAccessFile.seek(byteOffset);
final int read = randomAccessFile.read(fileBytes);
if (read != magicNumbers.length) {

MODIFY
src/main/java/org/apache/commons/io/input/ObservableInputStream.java
src/main/java/org/apache/commons/io/input/ObservableInputStream.java
@@ -164,7 +164,7 @@ public void close() throws IOException {
* @throws IOException The underlying {@link InputStream}, or either of the observers has thrown an exception.
*/
public void consume() throws IOException {
- final byte[] buffer = new byte[IOUtils.DEFAULT_BUFFER_SIZE];
+ final byte[] buffer = IOUtils.byteArray();
while (read(buffer) != EOF) {
// empty
}

MODIFY
src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java
src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java
@@ -90,7 +90,7 @@ private void createLeftOver() {
final int lineLengthBytes = currentLastBytePos + 1;
if (lineLengthBytes > 0) {
// create left over for next block
- leftOver = new byte[lineLengthBytes];
+ leftOver = IOUtils.byteArray(lineLengthBytes);
System.arraycopy(data, 0, leftOver, 0, lineLengthBytes);
} else {
leftOver = null;
@@ -149,7 +149,7 @@ private String readLine() {
if (lineLengthBytes < 0) {
throw new IllegalStateException("Unexpected negative line length=" + lineLengthBytes);
}
- final byte[] lineData = new byte[lineLengthBytes];
+ final byte[] lineData = IOUtils.byteArray(lineLengthBytes);
System.arraycopy(data, lineStart, lineData, 0, lineLengthBytes);
line = new String(lineData, charset);

MODIFY
src/main/java/org/apache/commons/io/input/Tailer.java
src/main/java/org/apache/commons/io/input/Tailer.java
@@ -259,7 +259,7 @@ public Tailer(final File file, final Charset charset, final TailerListener liste
this.delayMillis = delayMillis;
this.end = end;
- this.inbuf = new byte[bufSize];
+ this.inbuf = IOUtils.byteArray(bufSize);
// Save and prepare the listener
this.listener = listener;

MODIFY
src/main/java/org/apache/commons/io/input/XmlStreamReader.java
src/main/java/org/apache/commons/io/input/XmlStreamReader.java
@@ -186,7 +186,7 @@ private static String getXmlProlog(final InputStream inputStream, final String g
throws IOException {
String encoding = null;
if (guessedEnc != null) {
- final byte[] bytes = new byte[IOUtils.DEFAULT_BUFFER_SIZE];
+ final byte[] bytes = IOUtils.byteArray();
inputStream.mark(IOUtils.DEFAULT_BUFFER_SIZE);
int offset = 0;
int max = IOUtils.DEFAULT_BUFFER_SIZE;

MODIFY
src/main/java/org/apache/commons/io/input/buffer/CircularBufferInputStream.java
src/main/java/org/apache/commons/io/input/buffer/CircularBufferInputStream.java
@@ -78,7 +78,7 @@ protected void fillBuffer() throws IOException {
return;
}
int space = buffer.getSpace();
- final byte[] buf = new byte[space];
+ final byte[] buf = IOUtils.byteArray(space);
while (space > 0) {
final int res = in.read(buf, 0, space);
if (res == EOF) {

MODIFY
src/main/java/org/apache/commons/io/input/buffer/CircularByteBuffer.java
src/main/java/org/apache/commons/io/input/buffer/CircularByteBuffer.java
* @param size the size of buffer to create
*/
public CircularByteBuffer(final int size) {
- buffer = new byte[size];
+ buffer = IOUtils.byteArray(size);
startOffset = 0;
endOffset = 0;
currentNumberOfBytes = 0;

MODIFY
src/main/java/org/apache/commons/io/output/AbstractByteArrayOutputStream.java
src/main/java/org/apache/commons/io/output/AbstractByteArrayOutputStream.java
*/
package org.apache.commons.io.output;
+import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ClosedInputStream;
import java.io.InputStream;
@@ -103,7 +104,7 @@ protected void needNewBuffer(final int newcount) {
}
currentBufferIndex++;
- currentBuffer = new byte[newBufferSize];
+ currentBuffer = IOUtils.byteArray(newBufferSize);
buffers.add(currentBuffer);
}
}
@@ -364,7 +365,7 @@ protected void writeToImpl(final OutputStream out) throws IOException {
if (remaining == 0) {
return EMPTY_BYTE_ARRAY;
}
- final byte[] newbuf = new byte[remaining];
+ final byte[] newbuf = IOUtils.byteArray(remaining);
int pos = 0;
for (final byte[] buf : buffers) {
final int c = Math.min(buf.length, remaining);

MODIFY
src/test/java/org/apache/commons/io/input/ObservableInputStreamTest.java
src/test/java/org/apache/commons/io/input/ObservableInputStreamTest.java
@@ -254,7 +254,7 @@ public void testGetObserversOrder() throws IOException {
}
private void testNotificationCallbacks(final int bufferSize) throws IOException {
- final byte[] buffer = new byte[IOUtils.DEFAULT_BUFFER_SIZE];
+ final byte[] buffer = IOUtils.byteArray();
final LengthObserver lengthObserver = new LengthObserver();
final MethodCountObserver methodCountObserver = new MethodCountObserver();
try (final ObservableInputStream ois = new ObservableInputStream(new ByteArrayInputStream(buffer),

MODIFY
src/test/java/org/apache/commons/io/input/compatibility/XmlStreamReader.java
src/test/java/org/apache/commons/io/input/compatibility/XmlStreamReader.java
* @see XmlStreamWriter
*/
public class XmlStreamReader extends Reader {
- private static final int BUFFER_SIZE = IOUtils.DEFAULT_BUFFER_SIZE;
-
private static final String UTF_8 = "UTF-8";
private static final String US_ASCII = "US-ASCII";
@@ -421,7 +419,7 @@ public void close() throws IOException {
private void doRawStream(final InputStream inputStream)
throws IOException {
- final BufferedInputStream pis = new BufferedInputStream(inputStream, BUFFER_SIZE);
+ final BufferedInputStream pis = new BufferedInputStream(inputStream, IOUtils.DEFAULT_BUFFER_SIZE);
final String bomEnc = getBOMEncoding(pis);
final String xmlGuessEnc = getXMLGuessEncoding(pis);
final String xmlEnc = getXmlProlog(pis, xmlGuessEnc);
@@ -431,7 +429,7 @@ private void doRawStream(final InputStream inputStream)
private void doHttpStream(final InputStream inputStream, final String httpContentType,
final boolean lenient) throws IOException {
- final BufferedInputStream pis = new BufferedInputStream(inputStream, BUFFER_SIZE);
+ final BufferedInputStream pis = new BufferedInputStream(inputStream, IOUtils.DEFAULT_BUFFER_SIZE);
final String cTMime = getContentTypeMime(httpContentType);
final String cTEnc = getContentTypeEncoding(httpContentType);
final String bomEnc = getBOMEncoding(pis);
@@ -669,14 +667,14 @@ private static String getXmlProlog(final BufferedInputStream is, final String gu
throws IOException {
String encoding = null;
if (guessedEnc != null) {
- final byte[] bytes = new byte[BUFFER_SIZE];
- is.mark(BUFFER_SIZE);
+ final byte[] bytes = IOUtils.byteArray();
+ is.mark(IOUtils.DEFAULT_BUFFER_SIZE);
int offset = 0;
- int max = BUFFER_SIZE;
+ int max = IOUtils.DEFAULT_BUFFER_SIZE;
int c = is.read(bytes, offset, max);
int firstGT = -1;
String xmlProlog = ""; // avoid possible NPE warning (cannot happen; this just silences the warning)
- while (c != -1 && firstGT == -1 && offset < BUFFER_SIZE) {
+ while (c != -1 && firstGT == -1 && offset < IOUtils.DEFAULT_BUFFER_SIZE) {
offset += c;
max -= c;
c = is.read(bytes, offset, max);

Add and use IOUtils.byteArray(*).