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 boolean | closed
True when close() has been called successfully. |
| private OutputStream | currentOutputStream
The output stream to which data will be written at any given time. |
| private ByteArrayOutputStream | memoryOutputStream
The output stream to which data will be written prior to the theshold being reached. |
| private File | outputFile
The file to which output will be directed if the threshold is exceeded. |
| Access | Constructor and Description |
|---|---|
| public | DeferredFileOutputStream(int
The number of bytes at which to trigger an event. threshold, 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. |
| Modifier and Type | Method and Description |
|---|---|
| public void | close()
Overrides org. Implements java. |
| 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 the same output file specified in the constructor, even when threashold has not been reached. |
| protected OutputStream | Returns: The underlying output stream.Implements abstract org. |
| 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. |
| public void | writeTo(OutputStream
output stream to write to. out)Writes the data from this output stream to the specified output stream, after it has been closed. |
| closed | back to summary |
|---|---|
| private boolean closed True when close() has been called successfully. | |
| 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 | |
| memoryOutputStream | back to summary |
|---|---|
| private ByteArrayOutputStream memoryOutputStream The output stream to which data will be written prior to the theshold being reached. | |
| outputFile | back to summary |
|---|---|
| private File outputFile The file to which output will be directed if the threshold is exceeded. | |
| DeferredFileOutputStream | back to summary |
|---|---|
| public DeferredFileOutputStream(int threshold, 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.
| |
| 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 the same output file specified in the constructor, even when threashold has not been reached.
| |
| 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.
| |
| writeTo | back to summary |
|---|---|
| public void writeTo(OutputStream out) throws IOException Writes the data from this output stream to the specified output stream, after it has been closed.
| |