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 | Field and Description |
|---|---|
| private OutputStream | currentOutputStream
The output stream to which data will be written at any given time. |
| private final File | 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 File | outputFile
The file to which output will be directed if the threshold is exceeded. |
| private final String | prefix
The temporary file prefix. |
| private final String | suffix
The temporary file suffix. |
| Access | Constructor and Description |
|---|---|
| public | DeferredFileOutputStream(final int
The number of bytes at which to trigger an event. threshold, final File The file to which data is saved beyond the threshold. outputFile)Constructs an instance of this class which will trigger an event at the specified threshold, and save data to a file beyond that point. |
| private | DeferredFileOutputStream(final int
The number of bytes at which to trigger an event. threshold, final File The file to which data is saved beyond the threshold. outputFile, final String Prefix to use for the temporary file. prefix, final String Suffix to use for the temporary file. suffix, final File Temporary file directory. directory, final int The initial size of the in memory buffer. 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. |
| Modifier and Type | Method and Description |
|---|---|
| public void | close()
Overrides org. Implements java. Closes underlying output stream, and mark this as closed |
| public byte[] | Returns: The data for this output stream, ornull if no such
data is available.Returns the data for this output stream as an array of bytes, assuming that the data has been retained in memory. |
| public File | Returns: The file for this output stream, ornull if no such
file exists.Returns either the output file specified in the constructor or the temporary file created or null. |
| protected OutputStream | Returns: The underlying output stream.Implements abstract org. Returns the current output stream. |
| public boolean | Returns: true if the data is available in memory;
false otherwise.Determines whether or not the data for this output stream has been retained in memory. |
| protected void | thresholdReached()
Implements abstract org. Switches the underlying output stream from a memory based stream to one that is backed by disk. |
| currentOutputStream | back to summary |
|---|---|
| private OutputStream currentOutputStream The output stream to which data will be written at any given time. This
will always be one of | |
| directory | back to summary |
|---|---|
| private final File directory The directory to use for temporary files. | |
| memoryOutputStream | back to summary |
|---|---|
| private ByteArrayOutputStream memoryOutputStream The output stream to which data will be written prior to the threshold being reached. | |
| outputFile | back to summary |
|---|---|
| private File outputFile The file to which output will be directed if the threshold is exceeded. | |
| prefix | back to summary |
|---|---|
| private final String prefix The temporary file prefix. | |
| suffix | back to summary |
|---|---|
| private final String suffix The temporary file suffix. | |
| DeferredFileOutputStream | back to summary |
|---|---|
| public DeferredFileOutputStream(final int threshold, final File outputFile) Constructs an instance of this class which will trigger an event at the specified threshold, and save data to a file beyond that point. The initial buffer size will default to 1024 bytes which is ByteArrayOutputStream's default buffer size.
| |
| DeferredFileOutputStream | back to summary |
|---|---|
| private DeferredFileOutputStream(final int threshold, final File outputFile, final String prefix, final String suffix, final File directory, final 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.
| |
| close | back to summary |
|---|---|
| public void close() throws IOException Overrides org. Implements java. Closes underlying output stream, and mark this as closed
| |
| getData | back to summary |
|---|---|
| public byte[] getData() Returns the data for this output stream as an array of bytes, assuming
that the data has been retained in memory. If the data was written to
disk, this method returns
| |
| getFile | back to summary |
|---|---|
| public File getFile() Returns either the output file specified in the constructor or the temporary file created or null. 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
| |
| getStream | back to summary |
|---|---|
| protected OutputStream getStream() throws IOException Implements abstract org. Returns the current output stream. This may be memory based or disk based, depending on the current state with respect to the threshold.
| |
| isInMemory | back to summary |
|---|---|
| public boolean isInMemory() Determines whether or not the data for this output stream has been retained in memory.
| |
| thresholdReached | back to summary |
|---|---|
| protected void thresholdReached() throws IOException Implements abstract org. Switches the underlying output stream from a memory based stream to one that is backed by disk. This is the point at which we realise that too much data is being written to keep in memory, so we elect to switch to disk-based storage.
| |