You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ni...@apache.org on 2007/10/20 07:12:24 UTC

svn commit: r586668 - in /commons/proper/io/trunk: RELEASE-NOTES.txt src/java/org/apache/commons/io/output/DeferredFileOutputStream.java src/test/org/apache/commons/io/output/DeferredFileOutputStreamTest.java

Author: niallp
Date: Fri Oct 19 22:12:22 2007
New Revision: 586668

URL: http://svn.apache.org/viewvc?rev=586668&view=rev
Log:
IO-130 Add support for temporary files to DeferredFileOutputStream

Modified:
    commons/proper/io/trunk/RELEASE-NOTES.txt
    commons/proper/io/trunk/src/java/org/apache/commons/io/output/DeferredFileOutputStream.java
    commons/proper/io/trunk/src/test/org/apache/commons/io/output/DeferredFileOutputStreamTest.java

Modified: commons/proper/io/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/RELEASE-NOTES.txt?rev=586668&r1=586667&r2=586668&view=diff
==============================================================================
--- commons/proper/io/trunk/RELEASE-NOTES.txt (original)
+++ commons/proper/io/trunk/RELEASE-NOTES.txt Fri Oct 19 22:12:22 2007
@@ -51,6 +51,8 @@
 - TeeInputStream [IO-129]
   - Add new Tee input stream implementation
 
+- DeferredFileOutputStream [IO-130]
+  - Add support for temporary files
 
 Feedback
 --------

Modified: commons/proper/io/trunk/src/java/org/apache/commons/io/output/DeferredFileOutputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/output/DeferredFileOutputStream.java?rev=586668&r1=586667&r2=586668&view=diff
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/output/DeferredFileOutputStream.java (original)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/output/DeferredFileOutputStream.java Fri Oct 19 22:12:22 2007
@@ -67,6 +67,21 @@
      */
     private File outputFile;
 
+    /**
+     * The temporary file prefix.
+     */
+    private String prefix;
+
+    /**
+     * The temporary file suffix.
+     */
+    private String suffix;
+
+    /**
+     * The directory to use for temporary files.
+     */
+    private File directory;
+
     
     /**
      * True when close() has been called successfully.
@@ -93,6 +108,29 @@
     }
 
 
+    /**
+     * 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 prefix Prefix to use for the temporary file.
+     * @param suffix Suffix to use for the temporary file.
+     * @param directory Temporary file directory.
+     *
+     * @since Commons IO 1.4
+     */
+    public DeferredFileOutputStream(int threshold, String prefix, String suffix, File directory)
+    {
+        this(threshold, (File)null);
+        if (prefix == null) {
+            throw new IllegalArgumentException("Temporary file prefix is missing");
+        }
+        this.prefix = prefix;
+        this.suffix = suffix;
+        this.directory = directory;
+    }
+
+
     // --------------------------------------- ThresholdingOutputStream methods
 
 
@@ -120,6 +158,9 @@
      */
     protected void thresholdReached() throws IOException
     {
+        if (prefix != null) {
+            outputFile = File.createTempFile(prefix, suffix, directory);
+        }
         FileOutputStream fos = new FileOutputStream(outputFile);
         memoryOutputStream.writeTo(fos);
         currentOutputStream = fos;
@@ -162,8 +203,15 @@
 
 
     /**
-     * Returns the same output file specified in the constructor, even when
-     * threashold has not been reached.
+     * Returns either the output file specified in the constructor or
+     * the temporary file created or null.
+     * <p>
+     * If the constructor specifying the file is used then it returns that
+     * same output file, even when threashold has not been reached.
+     * <p>
+     * If constructor specifying a temporary file prefix/suffix is used
+     * then the temporary file created once the threashold is reached is returned
+     * If the threshold was not reached then <code>null</code> is returned.
      *
      * @return The file for this output stream, or <code>null</code> if no such
      *         file exists.

Modified: commons/proper/io/trunk/src/test/org/apache/commons/io/output/DeferredFileOutputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/output/DeferredFileOutputStreamTest.java?rev=586668&r1=586667&r2=586668&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/output/DeferredFileOutputStreamTest.java (original)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/output/DeferredFileOutputStreamTest.java Fri Oct 19 22:12:22 2007
@@ -238,6 +238,113 @@
     }
 
     /**
+     * Test specifying a temporary file and the threshold not reached.
+     */
+    public void testTempFileBelowThreshold() {
+
+        String prefix = "commons-io-test";
+        String suffix = ".out";
+        File tempDir  = new File(".");
+        DeferredFileOutputStream dfos =
+                new DeferredFileOutputStream(testBytes.length + 42, prefix, suffix, tempDir);
+        assertNull("Check file is null-A", dfos.getFile());
+        try
+        {
+            dfos.write(testBytes, 0, testBytes.length);
+            dfos.close();
+        }
+        catch (IOException e) {
+            fail("Unexpected IOException");
+        }
+        assertTrue(dfos.isInMemory());
+        assertNull("Check file is null-B", dfos.getFile());
+    }
+
+    /**
+     * Test specifying a temporary file and the threshold is reached.
+     */
+    public void testTempFileAboveThreshold() {
+
+        String prefix = "commons-io-test";
+        String suffix = ".out";
+        File tempDir  = new File(".");
+        DeferredFileOutputStream dfos =
+                new DeferredFileOutputStream(testBytes.length - 5, prefix, suffix, tempDir);
+        assertNull("Check file is null-A", dfos.getFile());
+        try
+        {
+            dfos.write(testBytes, 0, testBytes.length);
+            dfos.close();
+        }
+        catch (IOException e) {
+            fail("Unexpected IOException");
+        }
+        assertFalse(dfos.isInMemory());
+        assertNull(dfos.getData());
+        assertNotNull("Check file not null", dfos.getFile());
+        assertTrue("Check file exists", dfos.getFile().exists());
+        assertTrue("Check prefix", dfos.getFile().getName().startsWith(prefix));
+        assertTrue("Check suffix", dfos.getFile().getName().endsWith(suffix));
+        assertEquals("Check dir", tempDir.getPath(), dfos.getFile().getParent());
+
+        verifyResultFile(dfos.getFile());
+
+        // Delete the temporary file.
+        dfos.getFile().delete();
+    }
+
+    /**
+     * Test specifying a temporary file and the threshold is reached.
+     */
+    public void testTempFileAboveThresholdPrefixOnly() {
+
+        String prefix = "commons-io-test";
+        String suffix = null;
+        File tempDir  = null;
+        DeferredFileOutputStream dfos =
+                new DeferredFileOutputStream(testBytes.length - 5, prefix, suffix, tempDir);
+        assertNull("Check file is null-A", dfos.getFile());
+        try
+        {
+            dfos.write(testBytes, 0, testBytes.length);
+            dfos.close();
+        }
+        catch (IOException e) {
+            fail("Unexpected IOException");
+        }
+        assertFalse(dfos.isInMemory());
+        assertNull(dfos.getData());
+        assertNotNull("Check file not null", dfos.getFile());
+        assertTrue("Check file exists", dfos.getFile().exists());
+        assertTrue("Check prefix", dfos.getFile().getName().startsWith(prefix));
+        assertTrue("Check suffix", dfos.getFile().getName().endsWith(".tmp")); // ".tmp" is default
+
+        verifyResultFile(dfos.getFile());
+
+        // Delete the temporary file.
+        dfos.getFile().delete();
+    }
+
+    /**
+     * Test specifying a temporary file and the threshold is reached.
+     */
+    public void testTempFileError() {
+
+        String prefix = null;
+        String suffix = ".out";
+        File tempDir  = new File(".");
+        try
+        {
+            DeferredFileOutputStream dfos =
+                new DeferredFileOutputStream(testBytes.length - 5, prefix, suffix, tempDir);
+            fail("Expected IllegalArgumentException ");
+        }
+        catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+    /**
      * Verifies that the specified file contains the same data as the original
      * test data.
      *