Package com.google.common.io
Class FileBackedOutputStream
- java.lang.Object
- 
- java.io.OutputStream
- 
- com.google.common.io.FileBackedOutputStream
 
 
- 
- All Implemented Interfaces:
- java.io.Closeable,- java.io.Flushable,- java.lang.AutoCloseable
 
 @Beta @GwtIncompatible @J2ObjCIncompatible public final class FileBackedOutputStream extends java.io.OutputStream AnOutputStreamthat starts buffering to a byte array, but switches to file buffering once the data reaches a configurable size.When this stream creates a temporary file, it restricts the file's permissions to the current user or, in the case of Android, the current app. If that is not possible (as is the case under the very old Android Ice Cream Sandwich release), then this stream throws an exception instead of creating a file that would be more accessible. (This behavior is new in Guava 32.0.0. Previous versions would create a file that is more accessible, as discussed in Guava issue 2575. TODO: b/283778848 - Fill in CVE number once it's available.) 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
- Author:
- Chris Nokleberg
 
- 
- 
Constructor SummaryConstructors Constructor Description FileBackedOutputStream(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 SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description ByteSourceasByteSource()Returns 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)
 
- 
- 
- 
Constructor Detail- 
FileBackedOutputStreampublic 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
- Throws:
- java.lang.IllegalArgumentException- if- fileThresholdis negative
 
 - 
FileBackedOutputStreampublic 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 file
- resetOnFinalize- if true, the- reset()method will be called when the- ByteSourcereturned by- asByteSource()is finalized.
- Throws:
- java.lang.IllegalArgumentException- if- fileThresholdis negative
 
 
- 
 - 
Method Detail- 
asByteSourcepublic ByteSource asByteSource() Returns a readableByteSourceview of the data that has been written to this stream.- Since:
- 15.0
 
 - 
resetpublic void reset() throws java.io.IOException 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:
- java.io.IOException- if an I/O error occurred while deleting the file buffer
 
 - 
writepublic void write(int b) throws java.io.IOException - Specified by:
- writein class- java.io.OutputStream
- Throws:
- java.io.IOException
 
 - 
writepublic void write(byte[] b) throws java.io.IOException - Overrides:
- writein class- java.io.OutputStream
- Throws:
- java.io.IOException
 
 - 
writepublic void write(byte[] b, int off, int len) throws java.io.IOException - Overrides:
- writein class- java.io.OutputStream
- Throws:
- java.io.IOException
 
 - 
closepublic void close() throws java.io.IOException - Specified by:
- closein interface- java.lang.AutoCloseable
- Specified by:
- closein interface- java.io.Closeable
- Overrides:
- closein class- java.io.OutputStream
- Throws:
- java.io.IOException
 
 - 
flushpublic void flush() throws java.io.IOException - Specified by:
- flushin interface- java.io.Flushable
- Overrides:
- flushin class- java.io.OutputStream
- Throws:
- java.io.IOException
 
 
- 
 
-