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
public final class FileBackedOutputStream extends java.io.OutputStreamAnOutputStreamthat starts buffering to a byte array, but switches to file buffering once the data reaches a configurable size.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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classFileBackedOutputStream.MemoryOutputByteArrayOutputStream that exposes its internals.
-
Field Summary
Fields Modifier and Type Field Description private java.io.Filefileprivate intfileThresholdprivate FileBackedOutputStream.MemoryOutputmemoryprivate java.io.OutputStreamoutprivate java.io.FileparentDirectoryprivate booleanresetOnFinalizeprivate ByteSourcesource
-
Constructor Summary
Constructors Modifier 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.privateFileBackedOutputStream(int fileThreshold, boolean resetOnFinalize, java.io.File parentDirectory)
-
Method Summary
All 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()(package private) java.io.FilegetFile()Returns the file holding the data (possibly null).private java.io.InputStreamopenInputStream()voidreset()Callsclose()if not already closed, and then resets this object back to its initial state, for reuse.private voidupdate(int len)Checks if writinglenbytes would go over threshold, and switches to file buffering if so.voidwrite(byte[] b)voidwrite(byte[] b, int off, int len)voidwrite(int b)
-
-
-
Field Detail
-
fileThreshold
private final int fileThreshold
-
resetOnFinalize
private final boolean resetOnFinalize
-
source
private final ByteSource source
-
parentDirectory
private final java.io.File parentDirectory
-
out
private java.io.OutputStream out
-
memory
private FileBackedOutputStream.MemoryOutput memory
-
file
private java.io.File file
-
-
Constructor Detail
-
FileBackedOutputStream
public 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
-
FileBackedOutputStream
public 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 fileresetOnFinalize- if true, thereset()method will be called when theByteSourcereturned byasByteSource()is finalized.
-
FileBackedOutputStream
private FileBackedOutputStream(int fileThreshold, boolean resetOnFinalize, java.io.File parentDirectory)
-
-
Method Detail
-
getFile
java.io.File getFile()
Returns the file holding the data (possibly null).
-
asByteSource
public ByteSource asByteSource()
Returns a readableByteSourceview of the data that has been written to this stream.- Since:
- 15.0
-
openInputStream
private java.io.InputStream openInputStream() throws java.io.IOException- Throws:
java.io.IOException
-
reset
public void reset() throws java.io.IOExceptionCallsclose()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
-
write
public void write(int b) throws java.io.IOException- Specified by:
writein classjava.io.OutputStream- Throws:
java.io.IOException
-
write
public void write(byte[] b) throws java.io.IOException- Overrides:
writein classjava.io.OutputStream- Throws:
java.io.IOException
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException- Overrides:
writein classjava.io.OutputStream- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.OutputStream- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOException- Specified by:
flushin interfacejava.io.Flushable- Overrides:
flushin classjava.io.OutputStream- Throws:
java.io.IOException
-
update
private void update(int len) throws java.io.IOExceptionChecks if writinglenbytes would go over threshold, and switches to file buffering if so.- Throws:
java.io.IOException
-
-