Class DeferredFileOutputStream

All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class DeferredFileOutputStream extends ThresholdingOutputStream
This works like a ByteArrayOutputStream until a certain size is reached, then creates a temp file and acts like a buffered FileOutputStream. The data can be retreived afterwards by calling getInputStream(). When this object is closed, the temporary file is deleted. You can no longer call getInputStream().
Author:
Jeff Schnitzer
  • Field Details

  • Constructor Details

    • DeferredFileOutputStream

      public DeferredFileOutputStream(int transitionSize)
      Parameters:
      transitionSize - is the number of bytes at which to convert from a byte array to a real file.
  • Method Details

    • thresholdReached

      protected void thresholdReached(int current, int predicted) throws IOException
      Description copied from class: ThresholdingOutputStream
      Called when the threshold is about to be exceeded. This isn't exact; it's called whenever a write would occur that would cross the amount. Once it is called, it isn't called again.
      Specified by:
      thresholdReached in class ThresholdingOutputStream
      Parameters:
      current - is the current number of bytes that have been written
      predicted - is the total number after the write completes
      Throws:
      IOException
    • getInputStream

      public InputStream getInputStream() throws IOException
      Closes the output stream and creates an InputStream on the same data.
      Returns:
      either a BetterByteArrayOutputStream or buffered FileInputStream, depending on what state we are in.
      Throws:
      IOException
    • close

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