@Beta @GwtIncompatible public final class FileBackedOutputStream extends OutputStream
OutputStream that 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.)
This class is thread-safe.
| Constructor and 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. |
| Modifier and Type | Method and Description |
|---|---|
ByteSource |
asByteSource()
Returns a readable
ByteSource view of the data that has been written to this stream. |
void |
close() |
void |
flush() |
void |
reset()
Calls
close() if not already closed, and then resets this object back to its initial
state, for reuse. |
void |
write(byte[] b) |
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
public FileBackedOutputStream(int fileThreshold)
ByteSource returned by asByteSource() is finalized.fileThreshold - the number of bytes before the stream should switch to buffering to a fileIllegalArgumentException - if fileThreshold is negativepublic FileBackedOutputStream(int fileThreshold,
boolean resetOnFinalize)
ByteSource returned by asByteSource() is finalized.fileThreshold - the number of bytes before the stream should switch to buffering to a fileresetOnFinalize - if true, the reset() method will be called when the
ByteSource returned by asByteSource() is finalizedIllegalArgumentException - if fileThreshold is negativepublic ByteSource asByteSource()
ByteSource view of the data that has been written to this stream.public void reset()
throws IOException
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.IOException - if an I/O error occurred while deleting the file bufferpublic void write(int b)
throws IOException
write in class OutputStreamIOExceptionpublic void write(byte[] b)
throws IOException
write in class OutputStreamIOExceptionpublic void write(byte[] b,
int off,
int len)
throws IOException
write in class OutputStreamIOExceptionpublic void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableclose in class OutputStreamIOExceptionpublic void flush()
throws IOException
flush in interface Flushableflush in class OutputStreamIOExceptionCopyright © 2010–2025. All rights reserved.