Skip to content

Commit c5f2e40

Browse files
committed
[IO-515] Allow Specifying Initial Buffer Size of
DeferredFileOutputStream. Redo submitted patch.
1 parent 9e2b2c0 commit c5f2e40

4 files changed

Lines changed: 88 additions & 25 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ The <action> type attribute can be add,update,fix,remove.
5252
</action>
5353
</release>
5454
<release version="2.6" date="2016-MM-DD" description="New features and bug fixes.">
55+
<action issue="IO-515" dev="ggregory" type="fix" due-to="Brett Lounsbury, Gary Gregory">
56+
Allow Specifying Initial Buffer Size of DeferredFileOutputStream.
57+
</action>
5558
<action issue="IO-512" dev="ggregory" type="fix" due-to="Ralf Hauser">
5659
ThresholdingOutputStream.thresholdReached() results in FileNotFoundException.
5760
</action>

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
*/
5757
public class ByteArrayOutputStream extends OutputStream {
5858

59+
static final int DEFAULT_SIZE = 1024;
60+
5961
/** A singleton empty byte array. */
6062
private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
6163

@@ -77,7 +79,7 @@ public class ByteArrayOutputStream extends OutputStream {
7779
* initially 1024 bytes, though its size increases if necessary.
7880
*/
7981
public ByteArrayOutputStream() {
80-
this(1024);
82+
this(DEFAULT_SIZE);
8183
}
8284

8385
/**

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

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
public class DeferredFileOutputStream
4343
extends ThresholdingOutputStream
4444
{
45-
4645
// ----------------------------------------------------------- Data members
4746

4847

@@ -93,19 +92,38 @@ public class DeferredFileOutputStream
9392
/**
9493
* Constructs an instance of this class which will trigger an event at the
9594
* specified threshold, and save data to a file beyond that point.
95+
* The initial buffer size will default to 1024 bytes which is ByteArrayOutputStream's default buffer size.
9696
*
9797
* @param threshold The number of bytes at which to trigger an event.
9898
* @param outputFile The file to which data is saved beyond the threshold.
9999
*/
100100
public DeferredFileOutputStream(final int threshold, final File outputFile)
101101
{
102-
this(threshold, outputFile, null, null, null);
102+
this(threshold, outputFile, null, null, null, ByteArrayOutputStream.DEFAULT_SIZE);
103103
}
104104

105+
/**
106+
* Constructs an instance of this class which will trigger an event at the
107+
* specified threshold, and save data to a file beyond that point.
108+
*
109+
* @param threshold The number of bytes at which to trigger an event.
110+
* @param initialBufferSize The initial size of the in memory buffer.
111+
* @param outputFile The file to which data is saved beyond the threshold.
112+
*
113+
* @since 2.5
114+
*/
115+
public DeferredFileOutputStream(final int threshold, final int initialBufferSize, final File outputFile)
116+
{
117+
this(threshold, outputFile, null, null, null, initialBufferSize);
118+
if (initialBufferSize < 0) {
119+
throw new IllegalArgumentException("Initial buffer size must be atleast 0.");
120+
}
121+
}
105122

106123
/**
107124
* Constructs an instance of this class which will trigger an event at the
108125
* specified threshold, and save data to a temporary file beyond that point.
126+
* The initial buffer size will default to 32 bytes which is ByteArrayOutputStream's default buffer size.
109127
*
110128
* @param threshold The number of bytes at which to trigger an event.
111129
* @param prefix Prefix to use for the temporary file.
@@ -116,12 +134,36 @@ public DeferredFileOutputStream(final int threshold, final File outputFile)
116134
*/
117135
public DeferredFileOutputStream(final int threshold, final String prefix, final String suffix, final File directory)
118136
{
119-
this(threshold, null, prefix, suffix, directory);
137+
this(threshold, null, prefix, suffix, directory, ByteArrayOutputStream.DEFAULT_SIZE);
120138
if (prefix == null) {
121139
throw new IllegalArgumentException("Temporary file prefix is missing");
122140
}
123141
}
124142

143+
/**
144+
* Constructs an instance of this class which will trigger an event at the
145+
* specified threshold, and save data to a temporary file beyond that point.
146+
*
147+
* @param threshold The number of bytes at which to trigger an event.
148+
* @param initialBufferSize The initial size of the in memory buffer.
149+
* @param prefix Prefix to use for the temporary file.
150+
* @param suffix Suffix to use for the temporary file.
151+
* @param directory Temporary file directory.
152+
*
153+
* @since 2.5
154+
*/
155+
public DeferredFileOutputStream(final int threshold, final int initialBufferSize, final String prefix,
156+
final String suffix, final File directory)
157+
{
158+
this(threshold, null, prefix, suffix, directory, initialBufferSize);
159+
if (prefix == null) {
160+
throw new IllegalArgumentException("Temporary file prefix is missing");
161+
}
162+
if (initialBufferSize < 0) {
163+
throw new IllegalArgumentException("Initial buffer size must be atleast 0.");
164+
}
165+
}
166+
125167
/**
126168
* Constructs an instance of this class which will trigger an event at the
127169
* specified threshold, and save data either to a file beyond that point.
@@ -131,17 +173,18 @@ public DeferredFileOutputStream(final int threshold, final String prefix, final
131173
* @param prefix Prefix to use for the temporary file.
132174
* @param suffix Suffix to use for the temporary file.
133175
* @param directory Temporary file directory.
176+
* @param initialBufferSize The initial size of the in memory buffer.
134177
*/
135178
private DeferredFileOutputStream(final int threshold, final File outputFile, final String prefix,
136-
final String suffix, final File directory) {
179+
final String suffix, final File directory, final int initialBufferSize) {
137180
super(threshold);
138181
this.outputFile = outputFile;
139-
140-
memoryOutputStream = new ByteArrayOutputStream();
141-
currentOutputStream = memoryOutputStream;
142182
this.prefix = prefix;
143183
this.suffix = suffix;
144184
this.directory = directory;
185+
186+
memoryOutputStream = new ByteArrayOutputStream(initialBufferSize);
187+
currentOutputStream = memoryOutputStream;
145188
}
146189

147190

src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,42 @@
1616
*/
1717
package org.apache.commons.io.output;
1818

19-
import org.junit.Test;
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertNull;
23+
import static org.junit.Assert.assertTrue;
24+
import static org.junit.Assert.fail;
2025

2126
import java.io.File;
2227
import java.io.FileInputStream;
2328
import java.io.FileNotFoundException;
2429
import java.io.IOException;
2530
import java.util.Arrays;
2631

27-
import static org.junit.Assert.assertEquals;
28-
import static org.junit.Assert.assertFalse;
29-
import static org.junit.Assert.assertNotNull;
30-
import static org.junit.Assert.assertNull;
31-
import static org.junit.Assert.assertTrue;
32-
import static org.junit.Assert.fail;
32+
import org.junit.Test;
33+
import org.junit.runner.RunWith;
34+
import org.junit.runners.Parameterized;
35+
import org.junit.runners.Parameterized.Parameters;
3336

3437
/**
3538
* Unit tests for the <code>DeferredFileOutputStream</code> class.
3639
*
3740
* @version $Id$
3841
*/
42+
@RunWith(value=Parameterized.class)
3943
public class DeferredFileOutputStreamTest
4044
{
4145

46+
@Parameters(name = "initialBufferSize = {0}")
47+
public static Iterable<Object[]> data() {
48+
return Arrays.asList(new Object[][] { { 1 }, { 2 }, { 4 }, { 8 }, { 16 }, { 32 }, { 64 }, { 128 }, { 256 },
49+
{ 512 }, { 1024 }, { 2048 }, { 4096 } });
50+
}
51+
52+
public DeferredFileOutputStreamTest(final int initialBufferSize) {
53+
this.initialBufferSize = initialBufferSize;
54+
}
4255
/**
4356
* The test data as a string (which is the simplest form).
4457
*/
@@ -48,6 +61,8 @@ public class DeferredFileOutputStreamTest
4861
* The test data as a byte array, derived from the string.
4962
*/
5063
private final byte[] testBytes = testString.getBytes();
64+
65+
private final int initialBufferSize;
5166

5267
/**
5368
* Tests the case where the amount of data falls below the threshold, and
@@ -57,7 +72,7 @@ public class DeferredFileOutputStreamTest
5772
public void testBelowThreshold()
5873
{
5974
final DeferredFileOutputStream dfos =
60-
new DeferredFileOutputStream(testBytes.length + 42, null);
75+
new DeferredFileOutputStream(testBytes.length + 42, initialBufferSize, null);
6176
try
6277
{
6378
dfos.write(testBytes, 0, testBytes.length);
@@ -81,7 +96,7 @@ public void testBelowThreshold()
8196
@Test
8297
public void testAtThreshold() {
8398
final DeferredFileOutputStream dfos =
84-
new DeferredFileOutputStream(testBytes.length, null);
99+
new DeferredFileOutputStream(testBytes.length, initialBufferSize, null);
85100
try
86101
{
87102
dfos.write(testBytes, 0, testBytes.length);
@@ -110,7 +125,7 @@ public void testAboveThreshold() {
110125
testFile.delete();
111126

112127
final DeferredFileOutputStream dfos =
113-
new DeferredFileOutputStream(testBytes.length - 5, testFile);
128+
new DeferredFileOutputStream(testBytes.length - 5, initialBufferSize, testFile);
114129
try
115130
{
116131
dfos.write(testBytes, 0, testBytes.length);
@@ -141,7 +156,7 @@ public void testThresholdReached() {
141156
testFile.delete();
142157

143158
final DeferredFileOutputStream dfos =
144-
new DeferredFileOutputStream(testBytes.length / 2, testFile);
159+
new DeferredFileOutputStream(testBytes.length / 2, initialBufferSize, testFile);
145160
final int chunkSize = testBytes.length / 3;
146161

147162
try
@@ -171,12 +186,12 @@ public void testThresholdReached() {
171186
@Test
172187
public void testWriteToSmall(){
173188
final File testFile = new File("testWriteToMem.dat");
174-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
189+
final ByteArrayOutputStream baos = new ByteArrayOutputStream(initialBufferSize);
175190
// Ensure that the test starts from a clean base.
176191
testFile.delete();
177192

178193
final DeferredFileOutputStream dfos =
179-
new DeferredFileOutputStream(testBytes.length *2, testFile);
194+
new DeferredFileOutputStream(testBytes.length *2, initialBufferSize, testFile);
180195
try{
181196
dfos.write(testBytes);
182197

@@ -207,7 +222,7 @@ public void testWriteToSmall(){
207222
@Test
208223
public void testWriteToLarge(){
209224
final File testFile = new File("testWriteToFile.dat");
210-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
225+
final ByteArrayOutputStream baos = new ByteArrayOutputStream(initialBufferSize);
211226
// Ensure that the test starts from a clean base.
212227
testFile.delete();
213228

@@ -247,7 +262,7 @@ public void testTempFileBelowThreshold() {
247262
final String suffix = ".out";
248263
final File tempDir = new File(".");
249264
final DeferredFileOutputStream dfos =
250-
new DeferredFileOutputStream(testBytes.length + 42, prefix, suffix, tempDir);
265+
new DeferredFileOutputStream(testBytes.length + 42, initialBufferSize, prefix, suffix, tempDir);
251266
assertNull("Check file is null-A", dfos.getFile());
252267
try
253268
{
@@ -271,7 +286,7 @@ public void testTempFileAboveThreshold() {
271286
final String suffix = ".out";
272287
final File tempDir = new File(".");
273288
final DeferredFileOutputStream dfos =
274-
new DeferredFileOutputStream(testBytes.length - 5, prefix, suffix, tempDir);
289+
new DeferredFileOutputStream(testBytes.length - 5, initialBufferSize, prefix, suffix, tempDir);
275290
assertNull("Check file is null-A", dfos.getFile());
276291
try
277292
{
@@ -305,7 +320,7 @@ public void testTempFileAboveThresholdPrefixOnly() {
305320
final String suffix = null;
306321
final File tempDir = null;
307322
final DeferredFileOutputStream dfos =
308-
new DeferredFileOutputStream(testBytes.length - 5, prefix, suffix, tempDir);
323+
new DeferredFileOutputStream(testBytes.length - 5, initialBufferSize, prefix, suffix, tempDir);
309324
assertNull("Check file is null-A", dfos.getFile());
310325
try
311326
{

0 commit comments

Comments
 (0)