Class BufferingOutputStream

java.lang.Object
java.io.OutputStream
io.milton.common.BufferingOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class BufferingOutputStream extends OutputStream
An output stream which will buffer data, initially using memory up to maxMemorySize, and then overflowing to a temporary file. To use this class you will write to it, and then close it, and then call getInputStream to read the data. The temporary file, if it was created, will be deleted when the inputstream is closed.
  • Constructor Details

    • BufferingOutputStream

      public BufferingOutputStream(int maxMemorySize)
  • Method Details

    • getInputStream

      public InputStream getInputStream()
    • write

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

      public void write(int b) throws IOException
      Specified by:
      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
    • flush

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

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

      public long getSize()
    • setOnClose

      public void setOnClose(Runnable r)
    • isCompleteInMemory

      public boolean isCompleteInMemory()
      returns true if the data is completely held in memory
      Returns:
    • getInMemoryData

      public byte[] getInMemoryData()
      Gets the data currently held in memory
      Returns:
    • deleteTempFileIfExists

      public void deleteTempFileIfExists()
      If this is called before the inputstream is used, then the inputstream will fail to open (because it needs the file!!) So should only use in exception handlers