Package com.google.common.io
Class FileBackedOutputStream
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.
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 passingfalse
in the second parameter.
This class is thread-safe.
- Since:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
ByteArrayOutputStream that exposes its internals. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate File
private final int
private OutputStream
private final boolean
private final ByteSource
-
Constructor Summary
ConstructorsConstructorDescriptionFileBackedOutputStream
(int fileThreshold) Creates a new instance that uses the given file threshold, and does not reset the data when theByteSource
returned byasByteSource()
is finalized.FileBackedOutputStream
(int fileThreshold, boolean resetOnFinalize) Creates a new instance that uses the given file threshold, and optionally resets the data when theByteSource
returned byasByteSource()
is finalized. -
Method Summary
Modifier and TypeMethodDescriptionReturns a readableByteSource
view of the data that has been written to this stream.void
close()
void
flush()
(package private) File
getFile()
Returns the file holding the data (possibly null).private InputStream
void
reset()
Callsclose()
if not already closed, and then resets this object back to its initial state, for reuse.private void
update
(int len) Checks if writinglen
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
-
Field Details
-
fileThreshold
private final int fileThreshold -
resetOnFinalize
private final boolean resetOnFinalize -
source
-
out
-
memory
-
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 theByteSource
returned byasByteSource()
is finalized.- Parameters:
fileThreshold
- the number of bytes before the stream should switch to buffering to a file- Throws:
IllegalArgumentException
- iffileThreshold
is negative
-
FileBackedOutputStream
public FileBackedOutputStream(int fileThreshold, boolean resetOnFinalize) Creates a new instance that uses the given file threshold, and optionally resets the data when theByteSource
returned 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 theByteSource
returned byasByteSource()
is finalized.- Throws:
IllegalArgumentException
- iffileThreshold
is negative
-
-
Method Details
-
getFile
Returns the file holding the data (possibly null). -
asByteSource
Returns a readableByteSource
view of the data that has been written to this stream.- Since:
- 15.0
-
openInputStream
- Throws:
IOException
-
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:
write
in classOutputStream
- Throws:
IOException
-
write
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
write
- Overrides:
write
in classOutputStream
- Throws:
IOException
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
- Throws:
IOException
-
flush
- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classOutputStream
- Throws:
IOException
-
update
Checks if writinglen
bytes would go over threshold, and switches to file buffering if so.- Throws:
IOException
-