Class FileBackedOutputStream

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

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.

Temporary files created by this stream may live in the local filesystem until either:

  • reset() is called (removing the data in this stream and deleting the file), or...
  • this stream (or, more precisely, its asByteSource() view) is finalized during garbage collection, AND this stream was not constructed with the 1-arg constructor or the 2-arg constructor passing false in the second parameter.

This class is thread-safe.

Since:
1.0
  • Field Details

    • fileThreshold

      private final int fileThreshold
    • resetOnFinalize

      private final boolean resetOnFinalize
    • source

      private final ByteSource source
    • parentDirectory

      @CheckForNull private final File parentDirectory
    • out

      private OutputStream out
    • memory

      @CheckForNull private FileBackedOutputStream.MemoryOutput memory
    • file

      @CheckForNull private File file
  • Constructor Details

    • 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.
    • FileBackedOutputStream

      private FileBackedOutputStream(int fileThreshold, boolean resetOnFinalize, @CheckForNull File parentDirectory)
  • Method Details