java.lang.Object
java.io.OutputStream
com.google.common.io.FileBackedOutputStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
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 passingfalsein the second parameter.
This class is thread-safe.
- Since:
- 1.0
-
Constructor Summary
ConstructorsConstructorDescriptionFileBackedOutputStream(int fileThreshold) Creates a new instance that uses the given file threshold, and does not reset the data when theByteSourcereturned byasByteSource()is finalized.FileBackedOutputStream(int fileThreshold, boolean resetOnFinalize) Creates a new instance that uses the given file threshold, and optionally resets the data when theByteSourcereturned byasByteSource()is finalized. -
Method Summary
Modifier and TypeMethodDescriptionReturns a readableByteSourceview of the data that has been written to this stream.voidclose()voidflush()voidreset()Callsclose()if not already closed, and then resets this object back to its initial state, for reuse.voidwrite(byte[] b) voidwrite(byte[] b, int off, int len) voidwrite(int b) Methods inherited from class java.io.OutputStream
nullOutputStream
-
Constructor Details
-
FileBackedOutputStream
public FileBackedOutputStream(int fileThreshold) Creates a new instance that uses the given file threshold, and does not reset the data when theByteSourcereturned byasByteSource()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 theByteSourcereturned byasByteSource()is finalized.- Parameters:
fileThreshold- the number of bytes before the stream should switch to buffering to a fileresetOnFinalize- if true, thereset()method will be called when theByteSourcereturned byasByteSource()is finalized.
-
-
Method Details
-
asByteSource
Returns a readableByteSourceview of the data that has been written to this stream.- Since:
- 15.0
-
reset
Callsclose()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:
IOException- if an I/O error occurred while deleting the file buffer
-
write
- Specified by:
writein classOutputStream- Throws:
IOException
-
write
- Overrides:
writein classOutputStream- Throws:
IOException
-
write
- Overrides:
writein classOutputStream- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException
-
flush
- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException
-