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...
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.
| Modifier and Type | Class and Description |
|---|---|
| private static class | FileBackedOutputStream.
ByteArrayOutputStream that exposes its internals. |
| Modifier and Type | Field and Description |
|---|---|
| private File | |
| private final int | |
| private FileBackedOutputStream. | |
| private OutputStream | |
| private final boolean | |
| private final ByteSource |
| Access | Constructor and Description |
|---|---|
| public | FileBackedOutputStream(int
the number of bytes before the stream should switch to buffering to a file fileThreshold)Creates a new instance that uses the given file threshold, and does not reset the data when the
|
| public | FileBackedOutputStream(int
the number of bytes before the stream should switch to buffering to a file fileThreshold, boolean resetOnFinalize)Creates a new instance that uses the given file threshold, and optionally resets the data when
the |
| Modifier and Type | Method and Description |
|---|---|
| public ByteSource | |
| public synchronized void | close()
Overrides java. Implements java. |
| public synchronized void | flush()
Overrides java. Implements java. |
| pack-priv synchronized File | |
| private synchronized InputStream | |
| public synchronized void | |
| private void | update(int len)
Checks if writing |
| public synchronized void | write(int
the b)byte.Implements abstract java. |
| public synchronized void | write(byte[]
the data. b)Overrides java. b.length bytes from the specified byte array
to this output stream.
|
| public synchronized void | write(byte[]
the data. b, int the start offset in the data. off, int the number of bytes to write. len)Overrides java. len bytes from the specified byte array
starting at offset off to this output stream.
|
| file | back to summary |
|---|---|
private File file
| |
| fileThreshold | back to summary |
|---|---|
| private final int fileThreshold | |
| memory | back to summary |
|---|---|
private FileBackedOutputStream.
| |
| out | back to summary |
|---|---|
private OutputStream out
| |
| resetOnFinalize | back to summary |
|---|---|
| private final boolean resetOnFinalize | |
| source | back to summary |
|---|---|
| private final ByteSource source | |
| FileBackedOutputStream | back to summary |
|---|---|
| public FileBackedOutputStream(int fileThreshold) Creates a new instance that uses the given file threshold, and does not reset the data when the
| |
| FileBackedOutputStream | back to summary |
|---|---|
| public FileBackedOutputStream(int fileThreshold, boolean resetOnFinalize) Creates a new instance that uses the given file threshold, and optionally resets the data when
the
| |
| asByteSource | back to summary |
|---|---|
| public ByteSource asByteSource() Returns a readable
| |
| close | back to summary |
|---|---|
| public synchronized void close() throws IOException Overrides java. Implements java. Doc from java. Closes this output stream and releases any system resources
associated with this stream. The general contract of
| |
| flush | back to summary |
|---|---|
| public synchronized void flush() throws IOException Overrides java. Implements java. Doc from java. Flushes this output stream and forces any buffered output bytes
to be written out. The general contract of If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.
| |
| getFile | back to summary |
|---|---|
| pack-priv synchronized File getFile() Returns the file holding the data (possibly null).
| |
| openInputStream | back to summary |
|---|---|
| private synchronized InputStream openInputStream() throws IOException | |
| reset | back to summary |
|---|---|
| public synchronized void reset() throws IOException Calls
| |
| update | back to summary |
|---|---|
| private void update(int len) throws IOException Checks if writing
| |
| write | back to summary |
|---|---|
| public synchronized void write(int b) throws IOException Implements abstract java. Doc from java. Writes the specified byte to this output stream. The general
contract for
| |
| write | back to summary |
|---|---|
| public synchronized void write(byte[] b) throws IOException Overrides java. Doc from java. Writes
| |
| write | back to summary |
|---|---|
| public synchronized void write(byte[] b, int off, int len) throws IOException Overrides java. Doc from java. Writes
If
If
| |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv byte[] | |
| pack-priv int |
| MemoryOutput | back to summary |
|---|---|
| private MemoryOutput() | |
| getBuffer | back to summary |
|---|---|
| pack-priv byte[] getBuffer() | |
| getCount | back to summary |
|---|---|
| pack-priv int getCount() | |