Normalize internal buffers to 8192 bytes.
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -207,6 +207,9 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="IO-664" dev="ggregory" type="fix" due-to="Gary Gregory">
org.apache.commons.io.FileUtils.copyURLToFile(*) open but do not close streams.
</action>
+ <action issue="IO-666" dev="ggregory" type="update" due-to="Gary Gregory">
+ Normalize internal buffers to 8192 bytes.
+ </action>
</release>
<release version="2.6" date="2017-10-15" description="Java 7 required, Java 9 supported.">

MODIFY
src/main/java/org/apache/commons/io/IOUtils.java
src/main/java/org/apache/commons/io/IOUtils.java
/**
* The default buffer size ({@value}) to use in copy methods.
*/
- private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
+ public static final int DEFAULT_BUFFER_SIZE = 8192;
/**
* The system directory separator character.

MODIFY
src/main/java/org/apache/commons/io/input/ObservableInputStream.java
src/main/java/org/apache/commons/io/input/ObservableInputStream.java
import java.util.ArrayList;
import java.util.List;
+import org.apache.commons.io.IOUtils;
+
/**
* The {@link ObservableInputStream} allows, that an InputStream may be consumed
@@ -263,7 +265,7 @@ public void close() throws IOException {
* observers has thrown an exception.
*/
public void consume() throws IOException {
- final byte[] buffer = new byte[8192];
+ final byte[] buffer = new byte[IOUtils.DEFAULT_BUFFER_SIZE];
for (;;) {
final int res = read(buffer);
if (res == -1) {

MODIFY
src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java
src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java
import java.nio.file.StandardOpenOption;
import org.apache.commons.io.Charsets;
+import org.apache.commons.io.IOUtils;
/**
* Reads lines in a file reversely (similar to a BufferedReader, but starting at
public class ReversedLinesFileReader implements Closeable {
private static final String EMPTY_STRING = "";
- private static final int DEFAULT_BLOCK_SIZE = 4096;
+ private static final int DEFAULT_BLOCK_SIZE = IOUtils.DEFAULT_BUFFER_SIZE;
private final int blockSize;
private final Charset encoding;

MODIFY
src/main/java/org/apache/commons/io/input/Tailer.java
src/main/java/org/apache/commons/io/input/Tailer.java
import java.nio.charset.Charset;
import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
/**
* Simple implementation of the unix "tail -f" functionality.
private static final String RAF_MODE = "r";
- private static final int DEFAULT_BUFSIZE = 4096;
-
// The default charset used for reading files
private static final Charset DEFAULT_CHARSET = Charset.defaultCharset();
@@ -195,7 +194,7 @@ public Tailer(final File file, final TailerListener listener, final long delayMi
* @param end Set to true to tail from the end of the file, false to tail from the beginning of the file.
*/
public Tailer(final File file, final TailerListener listener, final long delayMillis, final boolean end) {
- this(file, listener, delayMillis, end, DEFAULT_BUFSIZE);
+ this(file, listener, delayMillis, end, IOUtils.DEFAULT_BUFFER_SIZE);
}
/**
@@ -208,7 +207,7 @@ public Tailer(final File file, final TailerListener listener, final long delayMi
*/
public Tailer(final File file, final TailerListener listener, final long delayMillis, final boolean end,
final boolean reOpen) {
- this(file, listener, delayMillis, end, reOpen, DEFAULT_BUFSIZE);
+ this(file, listener, delayMillis, end, reOpen, IOUtils.DEFAULT_BUFFER_SIZE);
}
/**
@@ -329,7 +328,7 @@ public static Tailer create(final File file, final Charset charset, final Tailer
*/
public static Tailer create(final File file, final TailerListener listener, final long delayMillis,
final boolean end) {
- return create(file, listener, delayMillis, end, DEFAULT_BUFSIZE);
+ return create(file, listener, delayMillis, end, IOUtils.DEFAULT_BUFFER_SIZE);
}
/**
@@ -344,7 +343,7 @@ public static Tailer create(final File file, final TailerListener listener, fina
*/
public static Tailer create(final File file, final TailerListener listener, final long delayMillis,
final boolean end, final boolean reOpen) {
- return create(file, listener, delayMillis, end, reOpen, DEFAULT_BUFSIZE);
+ return create(file, listener, delayMillis, end, reOpen, IOUtils.DEFAULT_BUFFER_SIZE);
}
/**

MODIFY
src/main/java/org/apache/commons/io/input/XmlStreamReader.java
src/main/java/org/apache/commons/io/input/XmlStreamReader.java
import java.util.regex.Pattern;
import org.apache.commons.io.ByteOrderMark;
+import org.apache.commons.io.IOUtils;
/**
* Character stream that handles all the necessary Voodoo to figure out the
* @since 2.0
*/
public class XmlStreamReader extends Reader {
- private static final int BUFFER_SIZE = 4096;
+ private static final int BUFFER_SIZE = IOUtils.DEFAULT_BUFFER_SIZE;
private static final String UTF_8 = "UTF-8";

MODIFY
src/main/java/org/apache/commons/io/input/buffer/CircularBufferInputStream.java
src/main/java/org/apache/commons/io/input/buffer/CircularBufferInputStream.java
import java.io.InputStream;
import java.util.Objects;
+import org.apache.commons.io.IOUtils;
+
/**
* Implementation of a buffered input stream, which is internally based on the
@@ -52,12 +54,12 @@ public CircularBufferInputStream(final InputStream inputStream, final int buffer
/**
* Creates a new instance, which filters the given input stream, and
- * uses a reasonable default buffer size (8192).
+ * uses a reasonable default buffer size ({@link IOUtils#DEFAULT_BUFFER_SIZE}).
*
* @param inputStream The input stream, which is being buffered.
*/
public CircularBufferInputStream(final InputStream inputStream) {
- this(inputStream, 8192);
+ this(inputStream, IOUtils.DEFAULT_BUFFER_SIZE);
}
/**

MODIFY
src/main/java/org/apache/commons/io/input/buffer/CircularByteBuffer.java
src/main/java/org/apache/commons/io/input/buffer/CircularByteBuffer.java
import java.util.Objects;
+import org.apache.commons.io.IOUtils;
+
/**
* A buffer, which doesn't need reallocation of byte arrays, because it
* reuses a single byte array. This works particularly well, if reading
@@ -42,10 +44,10 @@ public CircularByteBuffer(final int pSize) {
}
/**
- * Creates a new instance with a reasonable default buffer size (8192).
+ * Creates a new instance with a reasonable default buffer size ({@link IOUtils#DEFAULT_BUFFER_SIZE}).
*/
public CircularByteBuffer() {
- this(8192);
+ this(IOUtils.DEFAULT_BUFFER_SIZE);
}
/**

MODIFY
src/main/java/org/apache/commons/io/output/XmlStreamWriter.java
src/main/java/org/apache/commons/io/output/XmlStreamWriter.java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.XmlStreamReader;
/**
* @since 2.0
*/
public class XmlStreamWriter extends Writer {
- private static final int BUFFER_SIZE = 4096;
+ private static final int BUFFER_SIZE = IOUtils.DEFAULT_BUFFER_SIZE;
private final OutputStream out;

MODIFY
src/test/java/org/apache/commons/io/input/ObservableInputStreamTest.java
src/test/java/org/apache/commons/io/input/ObservableInputStreamTest.java
import java.io.ByteArrayInputStream;
import java.io.IOException;
+import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ObservableInputStream.Observer;
import org.junit.jupiter.api.Test;
@@ -72,7 +73,7 @@ void data(final byte[] pBuffer, final int pOffset, final int pLength) throws IOE
*/
@Test
public void testDataByteCalled() throws Exception {
- final byte[] buffer = MessageDigestCalculatingInputStreamTest.generateRandomByteStream(4096);
+ final byte[] buffer = MessageDigestCalculatingInputStreamTest.generateRandomByteStream(IOUtils.DEFAULT_BUFFER_SIZE);
final ObservableInputStream ois = new ObservableInputStream(new ByteArrayInputStream(buffer));
final LastByteKeepingObserver lko = new LastByteKeepingObserver();
assertEquals(-1, lko.lastByteSeen);
@@ -101,7 +102,7 @@ public void testDataByteCalled() throws Exception {
*/
@Test
public void testDataBytesCalled() throws Exception {
- final byte[] buffer = MessageDigestCalculatingInputStreamTest.generateRandomByteStream(4096);
+ final byte[] buffer = MessageDigestCalculatingInputStreamTest.generateRandomByteStream(IOUtils.DEFAULT_BUFFER_SIZE);
final ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
final ObservableInputStream ois = new ObservableInputStream(bais);
final LastBytesKeepingObserver lko = new LastBytesKeepingObserver();

MODIFY
src/test/java/org/apache/commons/io/input/ReversedLinesFileReaderTestSimple.java
src/test/java/org/apache/commons/io/input/ReversedLinesFileReaderTestSimple.java
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
+import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
@@ -55,14 +56,14 @@ public void testFileSizeIsExactMultipleOfBlockSize() throws URISyntaxException,
public void testUnsupportedEncodingUTF16() throws URISyntaxException {
final File testFileEmpty = new File(this.getClass().getResource("/test-file-empty.bin").toURI());
assertThrows(UnsupportedEncodingException.class,
- () -> new ReversedLinesFileReader(testFileEmpty, 4096, "UTF-16").close());
+ () -> new ReversedLinesFileReader(testFileEmpty, IOUtils.DEFAULT_BUFFER_SIZE, "UTF-16").close());
}
@Test
public void testUnsupportedEncodingBig5() throws URISyntaxException {
final File testFileEncodingBig5 = new File(this.getClass().getResource("/test-file-empty.bin").toURI());
assertThrows(UnsupportedEncodingException.class,
- () -> new ReversedLinesFileReader(testFileEncodingBig5, 4096, "Big5").close());
+ () -> new ReversedLinesFileReader(testFileEncodingBig5, IOUtils.DEFAULT_BUFFER_SIZE, "Big5").close());
}

MODIFY
src/test/java/org/apache/commons/io/input/TailerTest.java
src/test/java/org/apache/commons/io/input/TailerTest.java
@@ -132,7 +132,7 @@ public void testMultiByteBreak() throws Exception {
final boolean isWindows = osname.startsWith("Windows");
// Need to use UTF-8 to read & write the file otherwise it can be corrupted (depending on the default charset)
final Charset charsetUTF8 = StandardCharsets.UTF_8;
- tailer = new Tailer(file, charsetUTF8, listener, delay, false, isWindows, 4096);
+ tailer = new Tailer(file, charsetUTF8, listener, delay, false, isWindows, IOUtils.DEFAULT_BUFFER_SIZE);
final Thread thread = new Thread(tailer);
thread.start();
@@ -365,7 +365,7 @@ public void testInterrupt() throws Exception {
// Use a long delay to try to make sure the test thread calls interrupt() while the tailer thread is sleeping.
final int delay = 1000;
final int idle = 50; // allow time for thread to work
- tailer = new Tailer(file, listener, delay, false, 4096);
+ tailer = new Tailer(file, listener, delay, false, IOUtils.DEFAULT_BUFFER_SIZE);
final Thread thread = new Thread(tailer);
thread.setDaemon(true);
thread.start();

MODIFY
src/test/java/org/apache/commons/io/input/compatibility/XmlStreamReader.java
src/test/java/org/apache/commons/io/input/compatibility/XmlStreamReader.java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.XmlStreamWriter;
/**
* @see XmlStreamWriter
*/
public class XmlStreamReader extends Reader {
- private static final int BUFFER_SIZE = 4096;
+ private static final int BUFFER_SIZE = IOUtils.DEFAULT_BUFFER_SIZE;
private static final String UTF_8 = "UTF-8";

[IO-666] Normalize internal buffers to 8192 bytes.