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 2023/07/09 16:07:33 UTC

[commons-io] branch master updated: Add DeferredFileOutputStream.Builder.setOutputFile(Path)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git


The following commit(s) were added to refs/heads/master by this push:
     new f9d59dcf Add DeferredFileOutputStream.Builder.setOutputFile(Path)
f9d59dcf is described below

commit f9d59dcfb42633c60ca0bd6f1b5e48aed6d0f59d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jul 9 12:07:29 2023 -0400

    Add DeferredFileOutputStream.Builder.setOutputFile(Path)
    
    Add DeferredFileOutputStream.Builder.setDirectory(Path)
---
 src/changes/changes.xml                            |  6 ++
 .../io/output/DeferredFileOutputStream.java        | 71 +++++++++++++++++++---
 .../io/output/DeferredFileOutputStreamTest.java    |  3 +-
 3 files changed, 70 insertions(+), 10 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c9f9e0e3..79aa9a53 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -101,6 +101,12 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add ThresholdingOutputStream.getOutputStream() and deprecate getStream().
       </action>
+      <action dev="ggregory" type="add" due-to="Gary Gregory">
+        Add DeferredFileOutputStream.Builder.setOutputFile(Path).
+      </action>
+      <action dev="ggregory" type="add" due-to="Gary Gregory">
+        Add DeferredFileOutputStream.Builder.setDirectory(Path).
+      </action>
       <!-- UPDATE -->
       <action dev="ggregory" type="update" due-to="Dependabot">
         Bump jimfs from 1.2 to 1.3.0 #465 (tests).
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 606bd5b7..d4dc19e9 100644
--- a/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java
+++ b/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java
@@ -48,7 +48,6 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
      * </p>
      * <pre>{@code
      * DeferredFileOutputStream s = DeferredFileOutputStream.builder()
-     *   .setPath(path)
      *   .setBufferSize(4096)
      *   .setDirectory(dir)
      *   .setOutputFile(outputFile)
@@ -57,15 +56,19 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
      *   .setThreshold(threshold)
      *   .get();}
      * </pre>
+     * <p>
+     * The only super's aspect used us buffer size.
+     * </p>
+     *
      * @since 2.12.0
      */
     public static class Builder extends AbstractStreamBuilder<DeferredFileOutputStream, Builder> {
 
         private int threshold;
-        private File outputFile;
+        private Path outputFile;
         private String prefix;
         private String suffix;
-        private File directory;
+        private Path directory;
 
         public Builder() {
             setBufferSizeDefault(AbstractByteArrayOutputStream.DEFAULT_SIZE);
@@ -92,7 +95,19 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
          * @return this
          */
         public Builder setDirectory(final File directory) {
-            this.directory = directory;
+            this.directory = toPath(directory, null);
+            return this;
+        }
+
+        /**
+         * Sets the temporary file directory.
+         *
+         * @param directory Temporary file directory.
+         * @return this
+         * @since 2.14.0
+         */
+        public Builder setDirectory(final Path directory) {
+            this.directory = toPath(directory, null);
             return this;
         }
 
@@ -103,7 +118,19 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
          * @return this
          */
         public Builder setOutputFile(final File outputFile) {
-            this.outputFile = outputFile;
+            this.outputFile = toPath(outputFile, null);
+            return this;
+        }
+
+        /**
+         * Sets the file to which data is saved beyond the threshold.
+         *
+         * @param outputFile The file to which data is saved beyond the threshold.
+         * @return this
+         * @since 2.14.0
+         */
+        public Builder setOutputFile(final Path outputFile) {
+            this.outputFile = toPath(outputFile, null);
             return this;
         }
 
@@ -159,6 +186,14 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
         return initialBufferSize;
     }
 
+    private static Path toPath(final File file, final Supplier<Path> defaultPathSupplier) {
+        return file != null ? file.toPath() : defaultPathSupplier == null ? null : defaultPathSupplier.get();
+    }
+
+    private static Path toPath(final Path file, final Supplier<Path> defaultPathSupplier) {
+        return file != null ? file : defaultPathSupplier == null ? null : defaultPathSupplier.get();
+    }
+
     /**
      * The output stream to which data will be written prior to the threshold being reached.
      */
@@ -229,6 +264,28 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
         this.currentOutputStream = memoryOutputStream;
     }
 
+    /**
+     * 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.
+     * @param outputFile        The file to which data is saved beyond the threshold.
+     * @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.
+     * @throws IllegalArgumentException if initialBufferSize &lt; 0.
+     */
+    private DeferredFileOutputStream(final int threshold, final Path outputFile, final String prefix, final String suffix, final Path directory,
+            final int initialBufferSize) {
+        super(threshold);
+        this.outputPath = toPath(outputFile, null);
+        this.prefix = prefix;
+        this.suffix = suffix;
+        this.directory = toPath(directory, PathUtils::getTempDirectory);
+        this.memoryOutputStream = new ByteArrayOutputStream(checkBufferSize(initialBufferSize));
+        this.currentOutputStream = memoryOutputStream;
+    }
+
     /**
      * Constructs an instance of this class which will trigger an event at the specified threshold, and save data to a file beyond that point.
      *
@@ -394,10 +451,6 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream {
         return Files.newInputStream(outputPath);
     }
 
-    private Path toPath(final File file, final Supplier<Path> defaultPathSupplier) {
-        return file != null ? file.toPath() : defaultPathSupplier == null ? null : defaultPathSupplier.get();
-    }
-
     /**
      * Writes the data from this output stream to the specified output stream, after it has been closed.
      *
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 3969e714..e23a40c0 100644
--- a/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java
@@ -203,6 +203,7 @@ public class DeferredFileOutputStreamTest {
                 .setBufferSize(initialBufferSize)
                 .setPrefix(prefix)
                 .setSuffix(suffix)
+                .setDirectory(tempDir)
                 .setDirectory(tempDir.toFile())
                 .get();
         // @formatter:on
@@ -235,7 +236,7 @@ public class DeferredFileOutputStreamTest {
 
         final String prefix = "commons-io-test";
         final String suffix = null;
-        final File tempDir = null;
+        final Path tempDir = null;
         // @formatter:off
         final DeferredFileOutputStream dfos = DeferredFileOutputStream.builder()
                 .setThreshold(testBytes.length - 5)