Package com.helger.web.fileupload.io
Class DeferredFileOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- com.helger.web.fileupload.io.AbstractThresholdingOutputStream
-
- com.helger.web.fileupload.io.DeferredFileOutputStream
-
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
public class DeferredFileOutputStream extends AbstractThresholdingOutputStream
An output stream which will retain data in memory until a specified threshold is reached, and only then commit it to disk. If the stream is closed before the threshold is reached, the data will not be written to disk at all.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).
- Version:
- $Id: DeferredFileOutputStream.java 736890 2009-01-23 02:02:22Z niallp $
- Author:
- Martin Cooper, gaxzerow
-
-
Constructor Summary
Constructors Constructor Description DeferredFileOutputStream(int nThreshold, File aOutputFile)Constructs an instance of this class which will trigger an event at the specified threshold, and save data to a file beyond that point.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes underlying output stream, and mark this as closedbyte[]getData()Returns the data for this output stream as an array of bytes, assuming that the data has been retained in memory.intgetDataLength()Returns the length of the data for this output stream as number of bytes, assuming that the data has been retained in memory.FilegetFile()Returns either the output file specified in the constructor or the temporary file created or null.com.helger.commons.io.stream.NonBlockingByteArrayOutputStreamgetMemoryOS()protected OutputStreamgetStream()Returns the current output stream.booleanisInMemory()Determines whether or not the data for this output stream has been retained in memory.protected voidonThresholdReached()Switches the underlying output stream from a memory based stream to one that is backed by disk.voidwriteTo(OutputStream aOS)Writes the data from this output stream to the specified output stream, after it has been closed.-
Methods inherited from class com.helger.web.fileupload.io.AbstractThresholdingOutputStream
checkThreshold, flush, getByteCount, getThreshold, isThresholdExceeded, resetByteCount, write, write, write
-
Methods inherited from class java.io.OutputStream
nullOutputStream
-
-
-
-
Constructor Detail
-
DeferredFileOutputStream
public DeferredFileOutputStream(@Nonnegative int nThreshold, @Nonnull File aOutputFile)
Constructs an instance of this class which will trigger an event at the specified threshold, and save data to a file beyond that point.- Parameters:
nThreshold- The number of bytes at which to trigger an event.aOutputFile- The file to which data is saved beyond the threshold.
-
-
Method Detail
-
getStream
protected OutputStream getStream() throws IOException
Returns the current output stream. This may be memory based or disk based, depending on the current state with respect to the threshold.- Specified by:
getStreamin classAbstractThresholdingOutputStream- Returns:
- The underlying output stream.
- Throws:
IOException- if an error occurs.
-
onThresholdReached
protected void onThresholdReached() throws IOExceptionSwitches 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.- Specified by:
onThresholdReachedin classAbstractThresholdingOutputStream- Throws:
IOException- if an error occurs.
-
getMemoryOS
@Nullable public final com.helger.commons.io.stream.NonBlockingByteArrayOutputStream getMemoryOS()
-
isInMemory
public boolean isInMemory()
Determines whether or not the data for this output stream has been retained in memory.- Returns:
trueif the data is available in memory;falseotherwise.
-
getData
@Nullable @ReturnsMutableCopy 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 returnsnull.- Returns:
- The data for this output stream, or
nullif no such data is available. - See Also:
isInMemory()
-
getDataLength
@Nonnegative public int getDataLength()
Returns the length of the data for this output stream as number of bytes, assuming that the data has been retained in memory. If the data was written to disk, this method returns0.- Returns:
- The length of the data for this output stream, or
0if no such data is available. - See Also:
isInMemory()
-
getFile
@Nonnull 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
nullis returned.- Returns:
- The file for this output stream, or
nullif no such file exists.
-
close
public void close() throws IOExceptionCloses underlying output stream, and mark this as closed- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classAbstractThresholdingOutputStream- Throws:
IOException- if an error occurs.
-
writeTo
public void writeTo(@Nonnull @WillNotClose OutputStream aOS) throws IOException
Writes the data from this output stream to the specified output stream, after it has been closed.- Parameters:
aOS- output stream to write to.- Throws:
IOException- if this stream is not yet closed or an error occurs.
-
-