@Beta @GwtIncompatible public final class FileBackedOutputStream extends java.io.OutputStream
OutputStream that starts buffering to a byte array, but switches to file buffering
once the data reaches a configurable size.
This class is thread-safe.
| Modifier and Type | Class and Description |
|---|---|
private static class |
FileBackedOutputStream.MemoryOutput
ByteArrayOutputStream that exposes its internals.
|
| Modifier and Type | Field and Description |
|---|---|
private java.io.File |
file |
private int |
fileThreshold |
private FileBackedOutputStream.MemoryOutput |
memory |
private java.io.OutputStream |
out |
private boolean |
resetOnFinalize |
private ByteSource |
source |
| 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() |
(package private) java.io.File |
getFile()
Returns the file holding the data (possibly null).
|
private java.io.InputStream |
openInputStream() |
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. |
void |
write(byte[] b) |
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
private final int fileThreshold
private final boolean resetOnFinalize
private final ByteSource source
private java.io.OutputStream out
private FileBackedOutputStream.MemoryOutput memory
private java.io.File file
public FileBackedOutputStream(int fileThreshold)
ByteSource returned by asByteSource() is finalized.fileThreshold - the number of bytes before the stream should switch to buffering to a filepublic 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 finalizedjava.io.File getFile()
public ByteSource asByteSource()
ByteSource view of the data that has been written to this stream.private java.io.InputStream openInputStream()
throws java.io.IOException
java.io.IOExceptionpublic void reset()
throws java.io.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.java.io.IOException - if an I/O error occurred while deleting the file bufferpublic void write(int b)
throws java.io.IOException
write in class java.io.OutputStreamjava.io.IOExceptionpublic void write(byte[] b)
throws java.io.IOException
write in class java.io.OutputStreamjava.io.IOExceptionpublic void write(byte[] b,
int off,
int len)
throws java.io.IOException
write in class java.io.OutputStreamjava.io.IOExceptionpublic void close()
throws java.io.IOException
close in interface java.io.Closeableclose in interface java.lang.AutoCloseableclose in class java.io.OutputStreamjava.io.IOExceptionpublic void flush()
throws java.io.IOException
flush in interface java.io.Flushableflush in class java.io.OutputStreamjava.io.IOExceptionprivate void update(int len)
throws java.io.IOException
len bytes would go over threshold, and switches to file buffering if
so.java.io.IOException