Skip to content

Commit f9d59dc

Browse files
committed
Add DeferredFileOutputStream.Builder.setOutputFile(Path)
Add DeferredFileOutputStream.Builder.setDirectory(Path)
1 parent 0f2c3bc commit f9d59dc

3 files changed

Lines changed: 70 additions & 10 deletions

File tree

src/changes/changes.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ The <action> type attribute can be add,update,fix,remove.
101101
<action dev="ggregory" type="add" due-to="Gary Gregory">
102102
Add ThresholdingOutputStream.getOutputStream() and deprecate getStream().
103103
</action>
104+
<action dev="ggregory" type="add" due-to="Gary Gregory">
105+
Add DeferredFileOutputStream.Builder.setOutputFile(Path).
106+
</action>
107+
<action dev="ggregory" type="add" due-to="Gary Gregory">
108+
Add DeferredFileOutputStream.Builder.setDirectory(Path).
109+
</action>
104110
<!-- UPDATE -->
105111
<action dev="ggregory" type="update" due-to="Dependabot">
106112
Bump jimfs from 1.2 to 1.3.0 #465 (tests).

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

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
4848
* </p>
4949
* <pre>{@code
5050
* DeferredFileOutputStream s = DeferredFileOutputStream.builder()
51-
* .setPath(path)
5251
* .setBufferSize(4096)
5352
* .setDirectory(dir)
5453
* .setOutputFile(outputFile)
@@ -57,15 +56,19 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
5756
* .setThreshold(threshold)
5857
* .get();}
5958
* </pre>
59+
* <p>
60+
* The only super's aspect used us buffer size.
61+
* </p>
62+
*
6063
* @since 2.12.0
6164
*/
6265
public static class Builder extends AbstractStreamBuilder<DeferredFileOutputStream, Builder> {
6366

6467
private int threshold;
65-
private File outputFile;
68+
private Path outputFile;
6669
private String prefix;
6770
private String suffix;
68-
private File directory;
71+
private Path directory;
6972

7073
public Builder() {
7174
setBufferSizeDefault(AbstractByteArrayOutputStream.DEFAULT_SIZE);
@@ -92,7 +95,19 @@ public DeferredFileOutputStream get() {
9295
* @return this
9396
*/
9497
public Builder setDirectory(final File directory) {
95-
this.directory = directory;
98+
this.directory = toPath(directory, null);
99+
return this;
100+
}
101+
102+
/**
103+
* Sets the temporary file directory.
104+
*
105+
* @param directory Temporary file directory.
106+
* @return this
107+
* @since 2.14.0
108+
*/
109+
public Builder setDirectory(final Path directory) {
110+
this.directory = toPath(directory, null);
96111
return this;
97112
}
98113

@@ -103,7 +118,19 @@ public Builder setDirectory(final File directory) {
103118
* @return this
104119
*/
105120
public Builder setOutputFile(final File outputFile) {
106-
this.outputFile = outputFile;
121+
this.outputFile = toPath(outputFile, null);
122+
return this;
123+
}
124+
125+
/**
126+
* Sets the file to which data is saved beyond the threshold.
127+
*
128+
* @param outputFile The file to which data is saved beyond the threshold.
129+
* @return this
130+
* @since 2.14.0
131+
*/
132+
public Builder setOutputFile(final Path outputFile) {
133+
this.outputFile = toPath(outputFile, null);
107134
return this;
108135
}
109136

@@ -159,6 +186,14 @@ private static int checkBufferSize(final int initialBufferSize) {
159186
return initialBufferSize;
160187
}
161188

189+
private static Path toPath(final File file, final Supplier<Path> defaultPathSupplier) {
190+
return file != null ? file.toPath() : defaultPathSupplier == null ? null : defaultPathSupplier.get();
191+
}
192+
193+
private static Path toPath(final Path file, final Supplier<Path> defaultPathSupplier) {
194+
return file != null ? file : defaultPathSupplier == null ? null : defaultPathSupplier.get();
195+
}
196+
162197
/**
163198
* The output stream to which data will be written prior to the threshold being reached.
164199
*/
@@ -229,6 +264,28 @@ private DeferredFileOutputStream(final int threshold, final File outputFile, fin
229264
this.currentOutputStream = memoryOutputStream;
230265
}
231266

267+
/**
268+
* 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.
269+
*
270+
* @param threshold The number of bytes at which to trigger an event.
271+
* @param outputFile The file to which data is saved beyond the threshold.
272+
* @param prefix Prefix to use for the temporary file.
273+
* @param suffix Suffix to use for the temporary file.
274+
* @param directory Temporary file directory.
275+
* @param initialBufferSize The initial size of the in memory buffer.
276+
* @throws IllegalArgumentException if initialBufferSize &lt; 0.
277+
*/
278+
private DeferredFileOutputStream(final int threshold, final Path outputFile, final String prefix, final String suffix, final Path directory,
279+
final int initialBufferSize) {
280+
super(threshold);
281+
this.outputPath = toPath(outputFile, null);
282+
this.prefix = prefix;
283+
this.suffix = suffix;
284+
this.directory = toPath(directory, PathUtils::getTempDirectory);
285+
this.memoryOutputStream = new ByteArrayOutputStream(checkBufferSize(initialBufferSize));
286+
this.currentOutputStream = memoryOutputStream;
287+
}
288+
232289
/**
233290
* Constructs an instance of this class which will trigger an event at the specified threshold, and save data to a file beyond that point.
234291
*
@@ -394,10 +451,6 @@ public InputStream toInputStream() throws IOException {
394451
return Files.newInputStream(outputPath);
395452
}
396453

397-
private Path toPath(final File file, final Supplier<Path> defaultPathSupplier) {
398-
return file != null ? file.toPath() : defaultPathSupplier == null ? null : defaultPathSupplier.get();
399-
}
400-
401454
/**
402455
* Writes the data from this output stream to the specified output stream, after it has been closed.
403456
*

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ public void testTempFileAboveThreshold(final int initialBufferSize) throws IOExc
203203
.setBufferSize(initialBufferSize)
204204
.setPrefix(prefix)
205205
.setSuffix(suffix)
206+
.setDirectory(tempDir)
206207
.setDirectory(tempDir.toFile())
207208
.get();
208209
// @formatter:on
@@ -235,7 +236,7 @@ public void testTempFileAboveThresholdPrefixOnly(final int initialBufferSize) th
235236

236237
final String prefix = "commons-io-test";
237238
final String suffix = null;
238-
final File tempDir = null;
239+
final Path tempDir = null;
239240
// @formatter:off
240241
final DeferredFileOutputStream dfos = DeferredFileOutputStream.builder()
241242
.setThreshold(testBytes.length - 5)

0 commit comments

Comments
 (0)