Top Description Inners Fields Constructors Methods
com.google.common.io

public final Class FileBackedOutputStream

extends OutputStream
Class Inheritance
Annotations
@Beta
@J2ktIncompatible
@GwtIncompatible
@J2ObjCIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, java.util.Objects.requireNonNull

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:

This class is thread-safe.

Author
Chris Nokleberg
Since
1.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
FileBackedOutputStream.MemoryOutput

ByteArrayOutputStream that exposes its internals.

Field Summary

Modifier and TypeField and Description
private File
private final int
private FileBackedOutputStream.MemoryOutput
private OutputStream
private final boolean
private final ByteSource

Constructor Summary

AccessConstructor 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 ByteSource returned by asByteSource is finalized.

public
FileBackedOutputStream(int
the number of bytes before the stream should switch to buffering to a file
fileThreshold
,
boolean
if true, the reset method will be called when the ByteSource returned by asByteSource is finalized.
resetOnFinalize
)

Creates a new instance that uses the given file threshold, and optionally resets the data when the ByteSource returned by asByteSource is finalized.

Method Summary

Modifier and TypeMethod and Description
public ByteSource
asByteSource()

Returns a readable ByteSource view of the data that has been written to this stream.

public synchronized void
close()

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.close.

Closes this output stream and releases any system resources associated with this stream.
public synchronized void
flush()

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Flushes this output stream and forces any buffered output bytes to be written out.
pack-priv synchronized File
getFile()

Returns the file holding the data (possibly null).

private synchronized InputStream
public synchronized void
reset()

Calls close if not already closed, and then resets this object back to its initial state, for reuse.

private void
update(int len)

Checks if writing len bytes would go over threshold, and switches to file buffering if so.

public synchronized void
write(int
the byte.
b
)

Implements abstract java.io.OutputStream.write.

Writes the specified byte to this output stream.
public synchronized void
write(byte[]
the data.
b
)

Overrides java.io.OutputStream.write.

Writes 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.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream.
Inherited from java.io.OutputStream:
nullOutputStream

Field Detail

fileback to summary
private File file
Annotations
@GuardedBy:this
@CheckForNull
fileThresholdback to summary
private final int fileThreshold
memoryback to summary
private FileBackedOutputStream.MemoryOutput memory
Annotations
@GuardedBy:this
@CheckForNull
outback to summary
private OutputStream out
Annotations
@GuardedBy:this
resetOnFinalizeback to summary
private final boolean resetOnFinalize
sourceback to summary
private final ByteSource source

Constructor Detail

FileBackedOutputStreamback to summary
public 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.

Parameters
fileThreshold:int

the number of bytes before the stream should switch to buffering to a file

Exceptions
IllegalArgumentException:
if fileThreshold is negative
FileBackedOutputStreamback 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 ByteSource returned by asByteSource is finalized.

Parameters
fileThreshold:int

the number of bytes before the stream should switch to buffering to a file

resetOnFinalize:boolean

if true, the reset method will be called when the ByteSource returned by asByteSource is finalized.

Exceptions
IllegalArgumentException:
if fileThreshold is negative

Method Detail

asByteSourceback to summary
public ByteSource asByteSource()

Returns a readable ByteSource view of the data that has been written to this stream.

Since
15.0
closeback to summary
public synchronized void close() throws IOException

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.close.

Doc from java.io.OutputStream.close.

Closes this output stream and releases any system resources associated with this stream. The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
flushback to summary
public synchronized void flush() throws IOException

Overrides java.io.OutputStream.flush.

Implements java.io.Flushable.flush.

Doc from java.io.OutputStream.flush.

Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

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.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
getFileback to summary
pack-priv synchronized File getFile()

Returns the file holding the data (possibly null).

Annotations
@VisibleForTesting
@CheckForNull
openInputStreamback to summary
private synchronized InputStream openInputStream() throws IOException
resetback to summary
public synchronized void reset() throws IOException

Calls 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.

Exceptions
IOException:
if an I/O error occurred while deleting the file buffer
updateback to summary
private void update(int len) throws IOException

Checks if writing len bytes would go over threshold, and switches to file buffering if so.

Annotations
@GuardedBy:this
writeback to summary
public synchronized void write(int b) throws IOException

Implements abstract java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Parameters
b:int

the byte.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
writeback to summary
public synchronized void write(byte[] b) throws IOException

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes b.length bytes from the specified byte array to this output stream. The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length).

Parameters
b:byte[]

the data.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
writeback to summary
public synchronized void write(byte[] b, int off, int len) throws IOException

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation.

If b is null, a NullPointerException is thrown.

If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown.

Parameters
b:byte[]

the data.

off:int

the start offset in the data.

len:int

the number of bytes to write.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.
com.google.common.io back to summary

private Class FileBackedOutputStream.MemoryOutput

extends ByteArrayOutputStream
Class Inheritance

ByteArrayOutputStream that exposes its internals.

Field Summary

Inherited from java.io.ByteArrayOutputStream:
bufcount

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv byte[]
pack-priv int
Inherited from java.io.ByteArrayOutputStream:
closeresetsizetoByteArraytoStringtoStringtoStringtoStringwritewritewriteByteswriteTo

Constructor Detail

MemoryOutputback to summary
private MemoryOutput()

Method Detail

getBufferback to summary
pack-priv byte[] getBuffer()
getCountback to summary
pack-priv int getCount()