You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2016/09/28 02:09:56 UTC
commons-io git commit: [IO-515] Allow Specifying Initial Buffer Size
of DeferredFileOutputStream. Redo submitted patch.
Repository: commons-io
Updated Branches:
refs/heads/master 9e2b2c097 -> c5f2e40e7
[IO-515] Allow Specifying Initial Buffer Size of
DeferredFileOutputStream. Redo submitted patch.
Project: http://git-wip-us.apache.org/repos/asf/commons-io/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-io/commit/c5f2e40e
Tree: http://git-wip-us.apache.org/repos/asf/commons-io/tree/c5f2e40e
Diff: http://git-wip-us.apache.org/repos/asf/commons-io/diff/c5f2e40e
Branch: refs/heads/master
Commit: c5f2e40e7a8234fe48e08d451d3152ba58a03ac6
Parents: 9e2b2c0
Author: Gary Gregory <gg...@apache.org>
Authored: Tue Sep 27 19:09:52 2016 -0700
Committer: Gary Gregory <gg...@apache.org>
Committed: Tue Sep 27 19:09:52 2016 -0700
----------------------------------------------------------------------
src/changes/changes.xml | 3 ++
.../io/output/ByteArrayOutputStream.java | 4 +-
.../io/output/DeferredFileOutputStream.java | 57 +++++++++++++++++---
.../io/output/DeferredFileOutputStreamTest.java | 49 +++++++++++------
4 files changed, 88 insertions(+), 25 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-io/blob/c5f2e40e/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 8413010..838ad3a 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -52,6 +52,9 @@ The <action> type attribute can be add,update,fix,remove.
</action>
</release>
<release version="2.6" date="2016-MM-DD" description="New features and bug fixes.">
+ <action issue="IO-515" dev="ggregory" type="fix" due-to="Brett Lounsbury, Gary Gregory">
+ Allow Specifying Initial Buffer Size of DeferredFileOutputStream.
+ </action>
<action issue="IO-512" dev="ggregory" type="fix" due-to="Ralf Hauser">
ThresholdingOutputStream.thresholdReached() results in FileNotFoundException.
</action>
http://git-wip-us.apache.org/repos/asf/commons-io/blob/c5f2e40e/src/main/java/org/apache/commons/io/output/ByteArrayOutputStream.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/io/output/ByteArrayOutputStream.java b/src/main/java/org/apache/commons/io/output/ByteArrayOutputStream.java
index 90b080a..324dabf 100644
--- a/src/main/java/org/apache/commons/io/output/ByteArrayOutputStream.java
+++ b/src/main/java/org/apache/commons/io/output/ByteArrayOutputStream.java
@@ -56,6 +56,8 @@ import org.apache.commons.io.input.ClosedInputStream;
*/
public class ByteArrayOutputStream extends OutputStream {
+ static final int DEFAULT_SIZE = 1024;
+
/** A singleton empty byte array. */
private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
@@ -77,7 +79,7 @@ public class ByteArrayOutputStream extends OutputStream {
* initially 1024 bytes, though its size increases if necessary.
*/
public ByteArrayOutputStream() {
- this(1024);
+ this(DEFAULT_SIZE);
}
/**
http://git-wip-us.apache.org/repos/asf/commons-io/blob/c5f2e40e/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java b/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java
index a745699..030b28a 100644
--- a/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java
+++ b/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java
@@ -42,7 +42,6 @@ import org.apache.commons.io.IOUtils;
public class DeferredFileOutputStream
extends ThresholdingOutputStream
{
-
// ----------------------------------------------------------- Data members
@@ -93,19 +92,38 @@ public class DeferredFileOutputStream
/**
* Constructs an instance of this class which will trigger an event at the
* specified threshold, and save data to a file beyond that point.
+ * The initial buffer size will default to 1024 bytes which is ByteArrayOutputStream's default buffer size.
*
* @param threshold The number of bytes at which to trigger an event.
* @param outputFile The file to which data is saved beyond the threshold.
*/
public DeferredFileOutputStream(final int threshold, final File outputFile)
{
- this(threshold, outputFile, null, null, null);
+ this(threshold, outputFile, null, null, null, ByteArrayOutputStream.DEFAULT_SIZE);
}
+ /**
+ * Constructs an instance of this class which will trigger an event at the
+ * specified threshold, and save data to a file beyond that point.
+ *
+ * @param threshold The number of bytes at which to trigger an event.
+ * @param initialBufferSize The initial size of the in memory buffer.
+ * @param outputFile The file to which data is saved beyond the threshold.
+ *
+ * @since 2.5
+ */
+ public DeferredFileOutputStream(final int threshold, final int initialBufferSize, final File outputFile)
+ {
+ this(threshold, outputFile, null, null, null, initialBufferSize);
+ if (initialBufferSize < 0) {
+ throw new IllegalArgumentException("Initial buffer size must be atleast 0.");
+ }
+ }
/**
* Constructs an instance of this class which will trigger an event at the
* specified threshold, and save data to a temporary file beyond that point.
+ * The initial buffer size will default to 32 bytes which is ByteArrayOutputStream's default buffer size.
*
* @param threshold The number of bytes at which to trigger an event.
* @param prefix Prefix to use for the temporary file.
@@ -116,7 +134,7 @@ public class DeferredFileOutputStream
*/
public DeferredFileOutputStream(final int threshold, final String prefix, final String suffix, final File directory)
{
- this(threshold, null, prefix, suffix, directory);
+ this(threshold, null, prefix, suffix, directory, ByteArrayOutputStream.DEFAULT_SIZE);
if (prefix == null) {
throw new IllegalArgumentException("Temporary file prefix is missing");
}
@@ -124,6 +142,30 @@ public class DeferredFileOutputStream
/**
* Constructs an instance of this class which will trigger an event at the
+ * specified threshold, and save data to a temporary file beyond that point.
+ *
+ * @param threshold The number of bytes at which to trigger an event.
+ * @param initialBufferSize The initial size of the in memory buffer.
+ * @param prefix Prefix to use for the temporary file.
+ * @param suffix Suffix to use for the temporary file.
+ * @param directory Temporary file directory.
+ *
+ * @since 2.5
+ */
+ public DeferredFileOutputStream(final int threshold, final int initialBufferSize, final String prefix,
+ final String suffix, final File directory)
+ {
+ this(threshold, null, prefix, suffix, directory, initialBufferSize);
+ if (prefix == null) {
+ throw new IllegalArgumentException("Temporary file prefix is missing");
+ }
+ if (initialBufferSize < 0) {
+ throw new IllegalArgumentException("Initial buffer size must be atleast 0.");
+ }
+ }
+
+ /**
+ * Constructs an instance of this class which will trigger an event at the
* specified threshold, and save data either to a file beyond that point.
*
* @param threshold The number of bytes at which to trigger an event.
@@ -131,17 +173,18 @@ public class DeferredFileOutputStream
* @param prefix Prefix to use for the temporary file.
* @param suffix Suffix to use for the temporary file.
* @param directory Temporary file directory.
+ * @param initialBufferSize The initial size of the in memory buffer.
*/
private DeferredFileOutputStream(final int threshold, final File outputFile, final String prefix,
- final String suffix, final File directory) {
+ final String suffix, final File directory, final int initialBufferSize) {
super(threshold);
this.outputFile = outputFile;
-
- memoryOutputStream = new ByteArrayOutputStream();
- currentOutputStream = memoryOutputStream;
this.prefix = prefix;
this.suffix = suffix;
this.directory = directory;
+
+ memoryOutputStream = new ByteArrayOutputStream(initialBufferSize);
+ currentOutputStream = memoryOutputStream;
}
http://git-wip-us.apache.org/repos/asf/commons-io/blob/c5f2e40e/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java
index 936d3e9..f34c994 100644
--- a/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java
@@ -16,7 +16,12 @@
*/
package org.apache.commons.io.output;
-import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileInputStream;
@@ -24,21 +29,29 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
/**
* Unit tests for the <code>DeferredFileOutputStream</code> class.
*
* @version $Id$
*/
+@RunWith(value=Parameterized.class)
public class DeferredFileOutputStreamTest
{
+ @Parameters(name = "initialBufferSize = {0}")
+ public static Iterable<Object[]> data() {
+ return Arrays.asList(new Object[][] { { 1 }, { 2 }, { 4 }, { 8 }, { 16 }, { 32 }, { 64 }, { 128 }, { 256 },
+ { 512 }, { 1024 }, { 2048 }, { 4096 } });
+ }
+
+ public DeferredFileOutputStreamTest(final int initialBufferSize) {
+ this.initialBufferSize = initialBufferSize;
+ }
/**
* The test data as a string (which is the simplest form).
*/
@@ -48,6 +61,8 @@ public class DeferredFileOutputStreamTest
* The test data as a byte array, derived from the string.
*/
private final byte[] testBytes = testString.getBytes();
+
+ private final int initialBufferSize;
/**
* Tests the case where the amount of data falls below the threshold, and
@@ -57,7 +72,7 @@ public class DeferredFileOutputStreamTest
public void testBelowThreshold()
{
final DeferredFileOutputStream dfos =
- new DeferredFileOutputStream(testBytes.length + 42, null);
+ new DeferredFileOutputStream(testBytes.length + 42, initialBufferSize, null);
try
{
dfos.write(testBytes, 0, testBytes.length);
@@ -81,7 +96,7 @@ public class DeferredFileOutputStreamTest
@Test
public void testAtThreshold() {
final DeferredFileOutputStream dfos =
- new DeferredFileOutputStream(testBytes.length, null);
+ new DeferredFileOutputStream(testBytes.length, initialBufferSize, null);
try
{
dfos.write(testBytes, 0, testBytes.length);
@@ -110,7 +125,7 @@ public class DeferredFileOutputStreamTest
testFile.delete();
final DeferredFileOutputStream dfos =
- new DeferredFileOutputStream(testBytes.length - 5, testFile);
+ new DeferredFileOutputStream(testBytes.length - 5, initialBufferSize, testFile);
try
{
dfos.write(testBytes, 0, testBytes.length);
@@ -141,7 +156,7 @@ public class DeferredFileOutputStreamTest
testFile.delete();
final DeferredFileOutputStream dfos =
- new DeferredFileOutputStream(testBytes.length / 2, testFile);
+ new DeferredFileOutputStream(testBytes.length / 2, initialBufferSize, testFile);
final int chunkSize = testBytes.length / 3;
try
@@ -171,12 +186,12 @@ public class DeferredFileOutputStreamTest
@Test
public void testWriteToSmall(){
final File testFile = new File("testWriteToMem.dat");
- final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream(initialBufferSize);
// Ensure that the test starts from a clean base.
testFile.delete();
final DeferredFileOutputStream dfos =
- new DeferredFileOutputStream(testBytes.length *2, testFile);
+ new DeferredFileOutputStream(testBytes.length *2, initialBufferSize, testFile);
try{
dfos.write(testBytes);
@@ -207,7 +222,7 @@ public class DeferredFileOutputStreamTest
@Test
public void testWriteToLarge(){
final File testFile = new File("testWriteToFile.dat");
- final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream(initialBufferSize);
// Ensure that the test starts from a clean base.
testFile.delete();
@@ -247,7 +262,7 @@ public class DeferredFileOutputStreamTest
final String suffix = ".out";
final File tempDir = new File(".");
final DeferredFileOutputStream dfos =
- new DeferredFileOutputStream(testBytes.length + 42, prefix, suffix, tempDir);
+ new DeferredFileOutputStream(testBytes.length + 42, initialBufferSize, prefix, suffix, tempDir);
assertNull("Check file is null-A", dfos.getFile());
try
{
@@ -271,7 +286,7 @@ public class DeferredFileOutputStreamTest
final String suffix = ".out";
final File tempDir = new File(".");
final DeferredFileOutputStream dfos =
- new DeferredFileOutputStream(testBytes.length - 5, prefix, suffix, tempDir);
+ new DeferredFileOutputStream(testBytes.length - 5, initialBufferSize, prefix, suffix, tempDir);
assertNull("Check file is null-A", dfos.getFile());
try
{
@@ -305,7 +320,7 @@ public class DeferredFileOutputStreamTest
final String suffix = null;
final File tempDir = null;
final DeferredFileOutputStream dfos =
- new DeferredFileOutputStream(testBytes.length - 5, prefix, suffix, tempDir);
+ new DeferredFileOutputStream(testBytes.length - 5, initialBufferSize, prefix, suffix, tempDir);
assertNull("Check file is null-A", dfos.getFile());
try
{