public class DeferredFileOutputStream extends ThresholdingOutputStream
To build an instance, see DeferredFileOutputStream.Builder
.
This class originated in FileUpload processing. In this use case, you do not know in advance the size of the file being uploaded. If the file is small you want to store it in memory (for speed), but if the file is large you want to store it to file (to avoid memory issues).
Modifier and Type | Class and Description |
---|---|
static class |
DeferredFileOutputStream.Builder
Builds a new
DeferredFileOutputStream instance. |
Modifier and Type | Field and Description |
---|---|
private boolean |
closed
True when close() has been called successfully.
|
private java.io.OutputStream |
currentOutputStream
The output stream to which data will be written at any given time.
|
private java.nio.file.Path |
directory
The directory to use for temporary files.
|
private ByteArrayOutputStream |
memoryOutputStream
The output stream to which data will be written prior to the threshold being reached.
|
private java.nio.file.Path |
outputPath
The file to which output will be directed if the threshold is exceeded.
|
private java.lang.String |
prefix
The temporary file prefix.
|
private java.lang.String |
suffix
The temporary file suffix.
|
Modifier | Constructor and Description |
---|---|
|
DeferredFileOutputStream(int threshold,
java.io.File outputFile)
Deprecated.
|
private |
DeferredFileOutputStream(int threshold,
java.io.File outputFile,
java.lang.String prefix,
java.lang.String suffix,
java.io.File directory,
int initialBufferSize)
Constructs an instance of this class which will trigger an event at the specified threshold, and save data either to a file beyond that point.
|
|
DeferredFileOutputStream(int threshold,
int initialBufferSize,
java.io.File outputFile)
Deprecated.
|
|
DeferredFileOutputStream(int threshold,
int initialBufferSize,
java.lang.String prefix,
java.lang.String suffix,
java.io.File directory)
Deprecated.
|
private |
DeferredFileOutputStream(int threshold,
java.nio.file.Path outputFile,
java.lang.String prefix,
java.lang.String suffix,
java.nio.file.Path directory,
int initialBufferSize)
Constructs an instance of this class which will trigger an event at the specified threshold, and save data either to a file beyond that point.
|
|
DeferredFileOutputStream(int threshold,
java.lang.String prefix,
java.lang.String suffix,
java.io.File directory)
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
static DeferredFileOutputStream.Builder |
builder()
Constructs a new
DeferredFileOutputStream.Builder . |
private static int |
checkBufferSize(int initialBufferSize) |
void |
close()
Closes underlying output stream, and mark this as closed
|
byte[] |
getData()
Gets the data for this output stream as an array of bytes, assuming that the data has been retained in memory.
|
java.io.File |
getFile()
Gets either the output File specified in the constructor or the temporary File created or null.
|
java.nio.file.Path |
getPath()
Gets either the output Path specified in the constructor or the temporary Path created or null.
|
protected java.io.OutputStream |
getStream()
Gets the current output stream.
|
boolean |
isInMemory()
Tests whether or not the data for this output stream has been retained in memory.
|
protected void |
thresholdReached()
Switches the underlying output stream from a memory based stream to one that is backed by disk.
|
java.io.InputStream |
toInputStream()
Converts the current contents of this byte stream to an
InputStream . |
private static java.nio.file.Path |
toPath(java.io.File file,
java.util.function.Supplier<java.nio.file.Path> defaultPathSupplier) |
private static java.nio.file.Path |
toPath(java.nio.file.Path file,
java.util.function.Supplier<java.nio.file.Path> defaultPathSupplier) |
void |
writeTo(java.io.OutputStream outputStream)
Writes the data from this output stream to the specified output stream, after it has been closed.
|
checkThreshold, flush, getByteCount, getOutputStream, getThreshold, isThresholdExceeded, resetByteCount, setByteCount, write, write, write
private ByteArrayOutputStream memoryOutputStream
private java.io.OutputStream currentOutputStream
memoryOutputStream
or diskOutputStream
.private java.nio.file.Path outputPath
private final java.lang.String prefix
private final java.lang.String suffix
private final java.nio.file.Path directory
private boolean closed
@Deprecated public DeferredFileOutputStream(int threshold, java.io.File outputFile)
builder()
, DeferredFileOutputStream.Builder
, and DeferredFileOutputStream.Builder.get()
threshold
- The number of bytes at which to trigger an event.outputFile
- The file to which data is saved beyond the threshold.private DeferredFileOutputStream(int threshold, java.io.File outputFile, java.lang.String prefix, java.lang.String suffix, java.io.File directory, int initialBufferSize)
threshold
- The number of bytes at which to trigger an event.outputFile
- The file to which data is saved beyond the threshold.prefix
- Prefix to use for the temporary file.suffix
- Suffix to use for the temporary file.directory
- Temporary file directory.initialBufferSize
- The initial size of the in memory buffer.java.lang.IllegalArgumentException
- if initialBufferSize < 0.@Deprecated public DeferredFileOutputStream(int threshold, int initialBufferSize, java.io.File outputFile)
builder()
, DeferredFileOutputStream.Builder
, and DeferredFileOutputStream.Builder.get()
threshold
- The number of bytes at which to trigger an event.initialBufferSize
- The initial size of the in memory buffer.outputFile
- The file to which data is saved beyond the threshold.@Deprecated public DeferredFileOutputStream(int threshold, int initialBufferSize, java.lang.String prefix, java.lang.String suffix, java.io.File directory)
builder()
, DeferredFileOutputStream.Builder
, and DeferredFileOutputStream.Builder.get()
threshold
- The number of bytes at which to trigger an event.initialBufferSize
- The initial size of the in memory buffer.prefix
- Prefix to use for the temporary file.suffix
- Suffix to use for the temporary file.directory
- Temporary file directory.private DeferredFileOutputStream(int threshold, java.nio.file.Path outputFile, java.lang.String prefix, java.lang.String suffix, java.nio.file.Path directory, int initialBufferSize)
threshold
- The number of bytes at which to trigger an event.outputFile
- The file to which data is saved beyond the threshold.prefix
- Prefix to use for the temporary file.suffix
- Suffix to use for the temporary file.directory
- Temporary file directory.initialBufferSize
- The initial size of the in memory buffer.java.lang.IllegalArgumentException
- if initialBufferSize < 0.@Deprecated public DeferredFileOutputStream(int threshold, java.lang.String prefix, java.lang.String suffix, java.io.File directory)
builder()
, DeferredFileOutputStream.Builder
, and DeferredFileOutputStream.Builder.get()
threshold
- The number of bytes at which to trigger an event.prefix
- Prefix to use for the temporary file.suffix
- Suffix to use for the temporary file.directory
- Temporary file directory.public static DeferredFileOutputStream.Builder builder()
DeferredFileOutputStream.Builder
.DeferredFileOutputStream.Builder
.private static int checkBufferSize(int initialBufferSize)
private static java.nio.file.Path toPath(java.io.File file, java.util.function.Supplier<java.nio.file.Path> defaultPathSupplier)
private static java.nio.file.Path toPath(java.nio.file.Path file, java.util.function.Supplier<java.nio.file.Path> defaultPathSupplier)
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class ThresholdingOutputStream
java.io.IOException
- if an error occurs.public byte[] getData()
null
.null
if no such data is available.public java.io.File getFile()
If the constructor specifying the File is used then it returns that same output File, even when threshold has not been reached.
If constructor specifying a temporary File prefix/suffix is used then the temporary File created once the threshold is reached is returned if the
threshold was not reached then null
is returned.
null
if no such File exists.public java.nio.file.Path getPath()
If the constructor specifying the file is used then it returns that same output Path, even when threshold has not been reached.
If constructor specifying a temporary Path prefix/suffix is used then the temporary Path created once the threshold is reached is returned if the
threshold was not reached then null
is returned.
null
if no such Path exists.protected java.io.OutputStream getStream() throws java.io.IOException
getStream
in class ThresholdingOutputStream
java.io.IOException
- if an error occurs.public boolean isInMemory()
true
if the data is available in memory; false
otherwise.protected void thresholdReached() throws java.io.IOException
thresholdReached
in class ThresholdingOutputStream
java.io.IOException
- if an error occurs.public java.io.InputStream toInputStream() throws java.io.IOException
InputStream
. If the data for this output stream has been retained in memory, the returned
stream is backed by buffers of this
stream, avoiding memory allocation and copy, thus saving space and time.java.io.IOException
- if this stream is not yet closed or an error occurs.ByteArrayOutputStream.toInputStream()
public void writeTo(java.io.OutputStream outputStream) throws java.io.IOException
outputStream
- output stream to write to.java.lang.NullPointerException
- if the OutputStream is null
.java.io.IOException
- if this stream is not yet closed or an error occurs.