Class FileBackedOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable

    @Beta
    @GwtIncompatible
    public final class FileBackedOutputStream
    extends java.io.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:
    1.0
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  FileBackedOutputStream.MemoryOutput
      ByteArrayOutputStream that exposes its internals.
    • Constructor Summary

      Constructors 
      Constructor Description
      FileBackedOutputStream​(int fileThreshold)
      Creates a new instance that uses the given file threshold, and does not reset the data when the ByteSource returned by asByteSource() is finalized.
      FileBackedOutputStream​(int fileThreshold, boolean resetOnFinalize)
      Creates a new instance that uses the given file threshold, and optionally resets the data when the ByteSource returned by asByteSource() is finalized.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      ByteSource asByteSource()
      Returns a readable ByteSource view of the data that has been written to this stream.
      void close()  
      void flush()  
      (package private) java.io.File getFile()
      Returns the file holding the data (possibly null).
      private java.io.InputStream openInputStream()  
      void reset()
      Calls close() if not already closed, and then resets this object back to its initial state, for reuse.
      private void update​(int len)
      Checks if writing len bytes would go over threshold, and switches to file buffering if so.
      void write​(byte[] b)  
      void write​(byte[] b, int off, int len)  
      void write​(int b)  
      • 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 Detail

      • fileThreshold

        private final int fileThreshold
      • resetOnFinalize

        private final boolean resetOnFinalize
      • out

        private java.io.OutputStream out
      • file

        private java.io.File file
    • Constructor Detail

      • FileBackedOutputStream

        public FileBackedOutputStream​(int fileThreshold)
        Creates a new instance that uses the given file threshold, and does not reset the data when the ByteSource returned by asByteSource() is finalized.
        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 ByteSource returned by asByteSource() 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 ByteSource returned by asByteSource() is finalized
    • Method Detail

      • getFile

        java.io.File getFile()
        Returns the file holding the data (possibly null).
      • asByteSource

        public ByteSource asByteSource()
        Returns a readable ByteSource view of the data that has been written to this stream.
        Since:
        15.0
      • openInputStream

        private java.io.InputStream openInputStream()
                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • reset

        public void reset()
                   throws java.io.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:
        java.io.IOException - if an I/O error occurred while deleting the file buffer
      • write

        public void write​(int b)
                   throws java.io.IOException
        Specified by:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • write

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

        public void write​(byte[] b,
                          int off,
                          int len)
                   throws java.io.IOException
        Overrides:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.OutputStream
        Throws:
        java.io.IOException
      • flush

        public void flush()
                   throws java.io.IOException
        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.OutputStream
        Throws:
        java.io.IOException
      • update

        private void update​(int len)
                     throws java.io.IOException
        Checks if writing len bytes would go over threshold, and switches to file buffering if so.
        Throws:
        java.io.IOException