com.google.common.io
Class FileBackedOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by com.google.common.io.FileBackedOutputStream
All Implemented Interfaces:
Closeable, Flushable

public final class FileBackedOutputStream
extends OutputStream

An OutputStream that starts buffering to a byte array, but switches to file buffering once the data reaches a configurable size.

This class is thread-safe.

Since:
2009.09.15 tentative
Author:
Chris Nokleberg

Constructor Summary
FileBackedOutputStream(int fileThreshold)
          Creates a new instance that uses the given file threshold.
FileBackedOutputStream(int fileThreshold, boolean resetOnFinalize)
          Creates a new instance that uses the given file threshold, and optionally resets the data when the InputSupplier returned by getSupplier() is finalized.
 
Method Summary
 void close()
           
 void flush()
           
 InputSupplier<InputStream> getSupplier()
          Returns a supplier that may be used to retrieve the data buffered by this stream.
 void reset()
          Calls close() if not already closed, and then resets this object back to its initial state, for reuse.
 void write(byte[] b)
           
 void write(byte[] b, int off, int len)
           
 void write(int b)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileBackedOutputStream

public FileBackedOutputStream(int fileThreshold)
Creates a new instance that uses the given file threshold. Equivalent to ThresholdOutputStream(fileThreshold, false).

Parameters:
fileThreshold - the number of bytes before the stream should switch to buffering to a file

FileBackedOutputStream

public FileBackedOutputStream(int fileThreshold,
                              boolean resetOnFinalize)
Creates a new instance that uses the given file threshold, and optionally resets the data when the InputSupplier returned by getSupplier() is finalized.

Parameters:
fileThreshold - the number of bytes before the stream should switch to buffering to a file
resetOnFinalize - if true, the reset() method will be called when the InputSupplier returned by getSupplier() is finalized
Method Detail

getSupplier

public InputSupplier<InputStream> getSupplier()
Returns a supplier that may be used to retrieve the data buffered by this stream.


reset

public void reset()
           throws IOException
Calls close() if not already closed, and then resets this object back to its initial state, for reuse. If data was buffered to a file, it will be deleted.

Throws:
IOException - if an I/O error occurred while deleting the file buffer

write

public void write(int b)
           throws IOException
Specified by:
write in class OutputStream
Throws:
IOException

write

public void write(byte[] b)
           throws IOException
Overrides:
write in class OutputStream
Throws:
IOException

write

public void write(byte[] b,
                  int off,
                  int len)
           throws IOException
Overrides:
write in class OutputStream
Throws:
IOException

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Overrides:
close in class OutputStream
Throws:
IOException

flush

public void flush()
           throws IOException
Specified by:
flush in interface Flushable
Overrides:
flush in class OutputStream
Throws:
IOException