@@ -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 < 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 *
0 commit comments