public class DeferredFileOutputStream extends ThresholdingOutputStream
To build an instance, use DeferredFileOutputStream.Builder.
The caller is responsible for deleting the output file (getFile(), getPath()) created by a DeferredFileOutputStream when the caller only
configured a prefix.
The caller is responsible for deleting the output file passed to a constructor or builder through DeferredFileOutputStream.Builder.setOutputFile(File) or
DeferredFileOutputStream.Builder.setOutputFile(Path).
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).
DeferredFileOutputStream.Builder| Modifier and Type | Class and Description |
|---|---|
static class |
DeferredFileOutputStream.Builder
Builds a new
DeferredFileOutputStream. |
| Modifier and Type | Method and Description |
|---|---|
static DeferredFileOutputStream.Builder |
builder()
Constructs a new
DeferredFileOutputStream.Builder. |
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.
|
File |
getFile()
Gets either the output File specified in the constructor or the temporary File created or null.
|
Path |
getPath()
Gets either the output Path specified in the constructor or the temporary Path created or null.
|
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.
|
InputStream |
toInputStream()
Converts the current contents of this byte stream to an
InputStream. |
void |
writeTo(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, writepublic static DeferredFileOutputStream.Builder builder()
DeferredFileOutputStream.Builder.DeferredFileOutputStream.Builder.public void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableclose in class ThresholdingOutputStreamIOException - if an error occurs.public byte[] getData()
null.null if no such data is available.public 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 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.public boolean isInMemory()
true if the data is available in memory; false otherwise.protected void thresholdReached()
throws IOException
thresholdReached in class ThresholdingOutputStreamIOException - if an error occurs.public InputStream toInputStream() throws 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.IOException - if this stream is not yet closed or an error occurs.ByteArrayOutputStream.toInputStream()public void writeTo(OutputStream outputStream) throws IOException
outputStream - output stream to write to.NullPointerException - if the OutputStream is null.IOException - if this stream is not yet closed or an error occurs.