Class DeferredFileOutputStream

All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class DeferredFileOutputStream extends ThresholdingOutputStream
An output stream which will retain data in memory until a specified threshold is reached, and only then commit it to disk. If the stream is closed before the threshold is reached, the data will not be written to disk at all.

This class originated in FileUpload processing. In this use case, you do not know in advance the size of the file being uploaded. If the file is small you want to store it in memory (for speed), but if the file is large you want to store it to file (to avoid memory issues).

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private boolean
    True when close() has been called successfully.
    private OutputStream
    The output stream to which data will be written at any given time.
    private final File
    The directory to use for temporary files.
    The output stream to which data will be written prior to the threshold being reached.
    private File
    The file to which output will be directed if the threshold is exceeded.
    private final String
    The temporary file prefix.
    private final String
    The temporary file suffix.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
     
    DeferredFileOutputStream(int threshold, int initialBufferSize, File outputFile)
    Constructs an instance of this class which will trigger an event at the specified threshold, and save data to a file beyond that point.
     
    DeferredFileOutputStream(int threshold, int initialBufferSize, String prefix, String suffix, File directory)
    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.
     
    DeferredFileOutputStream(int threshold, File outputFile)
    Constructs an instance of this class which will trigger an event at the specified threshold, and save data to a file beyond that point.
    private
    DeferredFileOutputStream(int threshold, File outputFile, String prefix, String suffix, File directory, int initialBufferSize)
    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.
     
    DeferredFileOutputStream(int threshold, String prefix, String suffix, File directory)
    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.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes underlying output stream, and mark this as closed
    byte[]
    Returns the data for this output stream as an array of bytes, assuming that the data has been retained in memory.
    Returns either the output file specified in the constructor or the temporary file created or null.
    protected OutputStream
    Returns the current output stream.
    boolean
    Determines whether or not the data for this output stream has been retained in memory.
    protected void
    Switches the underlying output stream from a memory based stream to one that is backed by disk.
    void
    Writes the data from this output stream to the specified output stream, after it has been closed.

    Methods inherited from class java.io.OutputStream

    nullOutputStream

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • memoryOutputStream

      private ByteArrayOutputStream memoryOutputStream
      The output stream to which data will be written prior to the threshold being reached.
    • currentOutputStream

      private OutputStream currentOutputStream
      The output stream to which data will be written at any given time. This will always be one of memoryOutputStream or diskOutputStream.
    • outputFile

      private File outputFile
      The file to which output will be directed if the threshold is exceeded.
    • prefix

      private final String prefix
      The temporary file prefix.
    • suffix

      private final String suffix
      The temporary file suffix.
    • directory

      private final File directory
      The directory to use for temporary files.
    • closed

      private boolean closed
      True when close() has been called successfully.
  • Constructor Details

    • DeferredFileOutputStream

      public DeferredFileOutputStream(int threshold, File outputFile)
      Constructs an instance of this class which will trigger an event at the specified threshold, and save data to a file beyond that point. The initial buffer size will default to 1024 bytes which is ByteArrayOutputStream's default buffer size.
      Parameters:
      threshold - The number of bytes at which to trigger an event.
      outputFile - The file to which data is saved beyond the threshold.
    • DeferredFileOutputStream

      public DeferredFileOutputStream(int threshold, int initialBufferSize, File outputFile)
      Constructs an instance of this class which will trigger an event at the specified threshold, and save data to a file beyond that point.
      Parameters:
      threshold - The number of bytes at which to trigger an event.
      initialBufferSize - The initial size of the in memory buffer.
      outputFile - The file to which data is saved beyond the threshold.
      Since:
      2.5
    • DeferredFileOutputStream

      public DeferredFileOutputStream(int threshold, String prefix, String suffix, File directory)
      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. The initial buffer size will default to 32 bytes which is ByteArrayOutputStream's default buffer size.
      Parameters:
      threshold - The number of bytes at which to trigger an event.
      prefix - Prefix to use for the temporary file.
      suffix - Suffix to use for the temporary file.
      directory - Temporary file directory.
      Since:
      1.4
    • DeferredFileOutputStream

      public DeferredFileOutputStream(int threshold, int initialBufferSize, String prefix, String suffix, File directory)
      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.
      Parameters:
      threshold - The number of bytes at which to trigger an event.
      initialBufferSize - The initial size of the in memory buffer.
      prefix - Prefix to use for the temporary file.
      suffix - Suffix to use for the temporary file.
      directory - Temporary file directory.
      Since:
      2.5
    • DeferredFileOutputStream

      private DeferredFileOutputStream(int threshold, File outputFile, String prefix, String suffix, File directory, int initialBufferSize)
      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.
      Parameters:
      threshold - The number of bytes at which to trigger an event.
      outputFile - The file to which data is saved beyond the threshold.
      prefix - Prefix to use for the temporary file.
      suffix - Suffix to use for the temporary file.
      directory - Temporary file directory.
      initialBufferSize - The initial size of the in memory buffer.
  • Method Details

    • getStream

      protected OutputStream getStream() throws IOException
      Returns the current output stream. This may be memory based or disk based, depending on the current state with respect to the threshold.
      Specified by:
      getStream in class ThresholdingOutputStream
      Returns:
      The underlying output stream.
      Throws:
      IOException - if an error occurs.
    • thresholdReached

      protected void thresholdReached() throws IOException
      Switches the underlying output stream from a memory based stream to one that is backed by disk. This is the point at which we realise that too much data is being written to keep in memory, so we elect to switch to disk-based storage.
      Specified by:
      thresholdReached in class ThresholdingOutputStream
      Throws:
      IOException - if an error occurs.
    • isInMemory

      public boolean isInMemory()
      Determines whether or not the data for this output stream has been retained in memory.
      Returns:
      true if the data is available in memory; false otherwise.
    • getData

      public byte[] getData()
      Returns the data for this output stream as an array of bytes, assuming that the data has been retained in memory. If the data was written to disk, this method returns null.
      Returns:
      The data for this output stream, or null if no such data is available.
    • getFile

      public File getFile()
      Returns either the output file specified in the constructor or the temporary file created or null.

      If the constructor specifying the file is used then it returns that same output file, even when threshold has not been reached.

      If constructor specifying a temporary file prefix/suffix is used then the temporary file created once the threshold is reached is returned If the threshold was not reached then null is returned.

      Returns:
      The file for this output stream, or null if no such file exists.
    • close

      public void close() throws IOException
      Closes underlying output stream, and mark this as closed
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class ThresholdingOutputStream
      Throws:
      IOException - if an error occurs.
    • writeTo

      public void writeTo(OutputStream out) throws IOException
      Writes the data from this output stream to the specified output stream, after it has been closed.
      Parameters:
      out - output stream to write to.
      Throws:
      IOException - if this stream is not yet closed or an error occurs.