You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2005/09/10 15:19:24 UTC
svn commit: r280004 - in /jakarta/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: scolebourne
Date: Sat Sep 10 06:19:17 2005
New Revision: 280004
URL: http://svn.apache.org/viewcvs?rev=280004&view=rev
Log:
DeferredFileOutputStream.writeTo to allow current contents to be written to a stream
rfe 34173, from gaxzerow
Modified:
jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt
jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/output/DeferredFileOutputStream.java
jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/output/DeferredFileOutputStreamTest.java
Modified: jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt?rev=280004&r1=280003&r2=280004&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt (original)
+++ jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt Sat Sep 10 06:19:17 2005
@@ -52,9 +52,6 @@
- CountingInputStream - skip(long)
Bytes from calls to this method were not previously counted
-- DeferredFileOutputStream
- ???
-
Enhancements from 1.0
---------------------
@@ -126,6 +123,12 @@
- CountingInputStream,CountingOutputStream - resetCount()
Adds the ability to reset the count part way through reading/writing the stream
+
+- DeferredFileOutputStream - writeTo(OutputStream)
+ New method to allow current contents to be written to a stream [34173]
+
+- DeferredFileOutputStream
+ Performance optimizations avoiding double buffering [34142]
Feedback
Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/output/DeferredFileOutputStream.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/output/DeferredFileOutputStream.java?rev=280004&r1=280003&r2=280004&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/output/DeferredFileOutputStream.java (original)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/output/DeferredFileOutputStream.java Sat Sep 10 06:19:17 2005
@@ -16,9 +16,12 @@
package org.apache.commons.io.output;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
+import org.apache.commons.io.IOUtils;
+
/**
* <p>An output stream which will retain data in memory until a specified
@@ -32,6 +35,7 @@
* to store it to file (to avoid memory issues).</p>
*
* @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
+ * @author gaxzerow
*
* @version $Id$
*/
@@ -62,6 +66,11 @@
*/
private File outputFile;
+
+ /**
+ * True when close() has been called successfully.
+ */
+ private boolean closed = false;
// ----------------------------------------------------------- Constructors
@@ -152,9 +161,8 @@
/**
- * Returns the data for this output stream as a <code>File</code>, assuming
- * that the data was written to disk. If the data was retained in memory,
- * this method returns <code>null</code>.
+ * Returns the same output file specified in the constructor, even when
+ * threashold has not been reached.
*
* @return The file for this output stream, or <code>null</code> if no such
* file exists.
@@ -162,5 +170,50 @@
public File getFile()
{
return outputFile;
+ }
+
+
+ /**
+ * Closes underlying output stream, and mark this as closed
+ *
+ * @exception IOException if an error occurs.
+ */
+ public void close() throws IOException
+ {
+ super.close();
+ closed = true;
+ }
+
+
+ /**
+ * Writes the data from this output stream to the specified output stream,
+ * after it has been closed.
+ *
+ * @param out output stream to write to.
+ * @exception IOException if this stream is not yet closed or an error occurs.
+ */
+ public void writeTo(OutputStream out) throws IOException
+ {
+ // we may only need to check if this is closed if we are working with a file
+ // but we should force the habit of closing wether we are working with
+ // a file or memory.
+ if (!closed)
+ {
+ throw new IOException("Stream not closed");
+ }
+
+ if(isInMemory())
+ {
+ memoryOutputStream.writeTo(out);
+ }
+ else
+ {
+ FileInputStream fis = new FileInputStream(outputFile);
+ try {
+ IOUtils.copy(fis, out);
+ } finally {
+ IOUtils.closeQuietly(fis);
+ }
+ }
}
}
Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/output/DeferredFileOutputStreamTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/output/DeferredFileOutputStreamTest.java?rev=280004&r1=280003&r2=280004&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/output/DeferredFileOutputStreamTest.java (original)
+++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/output/DeferredFileOutputStreamTest.java Sat Sep 10 06:19:17 2005
@@ -166,6 +166,77 @@
// Ensure that the test starts from a clean base.
testFile.delete();
}
+
+
+ /**
+ * Test wether writeTo() properly writes small content.
+ */
+ public void testWriteToSmall(){
+ File testFile = new File("testWriteToMem.dat");
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ // Ensure that the test starts from a clean base.
+ testFile.delete();
+
+ DeferredFileOutputStream dfos =
+ new DeferredFileOutputStream(testBytes.length *2, testFile);
+ try{
+ dfos.write(testBytes);
+
+ assertFalse(testFile.exists());
+ assertTrue(dfos.isInMemory());
+
+ try {
+ dfos.writeTo(baos);
+ fail("Should not have been able to write before closing");
+ } catch (IOException ioe) {
+ // ok, as expected
+ }
+
+ dfos.close();
+ dfos.writeTo(baos);
+ } catch (IOException ioe) {
+ fail("Unexpected IOException");
+ }
+ byte[] copiedBytes = baos.toByteArray();
+ assertTrue(Arrays.equals(testBytes, copiedBytes));
+
+ testFile.delete();
+ }
+
+ /**
+ * Test wether writeTo() properly writes large content.
+ */
+ public void testWriteToLarge(){
+ File testFile = new File("testWriteToFile.dat");
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ // Ensure that the test starts from a clean base.
+ testFile.delete();
+
+ DeferredFileOutputStream dfos =
+ new DeferredFileOutputStream(testBytes.length /2, testFile);
+ try{
+ dfos.write(testBytes);
+
+ assertTrue(testFile.exists());
+ assertFalse(dfos.isInMemory());
+
+ try {
+ dfos.writeTo(baos);
+ fail("Should not have been able to write before closeing");
+ } catch (IOException ioe) {
+ // ok, as expected
+ }
+
+ dfos.close();
+ dfos.writeTo(baos);
+ } catch (IOException ioe) {
+ fail("Unexpected IOException");
+ }
+ byte[] copiedBytes = baos.toByteArray();
+ assertTrue(Arrays.equals(testBytes, copiedBytes));
+ verifyResultFile(testFile);
+ testFile.delete();
+ }
/**
* Verifies that the specified file contains the same data as the original
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org