Top Description Fields Constructors Methods
org.apache.tomcat.util.http.fileupload

public Class DeferredFileOutputStream

extends ThresholdingOutputStream
Class Inheritance
Imports
java.io.File, .FileOutputStream, .IOException, .OutputStream

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).

Field Summary

Modifier and TypeField 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.

Constructor Summary

AccessConstructor 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.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides org.apache.tomcat.util.http.fileupload.ThresholdingOutputStream.close.

Implements java.io.Closeable.close.

Closes underlying output stream, and mark this as closed

public byte[]

Returns:

The data for this output stream, or null if no such data is available.
getData
()

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, or null if no such file exists.
getFile
()

Returns either the output file specified in the constructor or the temporary file created or null.

protected OutputStream

Returns:

The underlying output stream.
getStream
()

Implements abstract org.apache.tomcat.util.http.fileupload.ThresholdingOutputStream.getStream.

Returns the current output stream.

public boolean

Returns:

true if the data is available in memory; false otherwise.
isInMemory
()

Determines whether or not the data for this output stream has been retained in memory.

protected void
thresholdReached()

Implements abstract org.apache.tomcat.util.http.fileupload.ThresholdingOutputStream.thresholdReached.

Switches the underlying output stream from a memory based stream to one that is backed by disk.

Inherited from org.apache.tomcat.util.http.fileupload.ThresholdingOutputStream:
checkThresholdflushisThresholdExceededwritewritewrite

Field Detail

currentOutputStreamback 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 or diskOutputStream.

directoryback to summary
private final File directory

The directory to use for temporary files.

memoryOutputStreamback to summary
private ByteArrayOutputStream memoryOutputStream

The output stream to which data will be written prior to the threshold being reached.

outputFileback to summary
private File outputFile

The file to which output will be directed if the threshold is exceeded.

prefixback to summary
private final String prefix

The temporary file prefix.

suffixback to summary
private final String suffix

The temporary file suffix.

Constructor Detail

DeferredFileOutputStreamback 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.

Parameters
threshold:int

The number of bytes at which to trigger an event.

outputFile:File

The file to which data is saved beyond the threshold.

DeferredFileOutputStreamback 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.

Parameters
threshold:int

The number of bytes at which to trigger an event.

outputFile:File

The file to which data is saved beyond the threshold.

prefix:String

Prefix to use for the temporary file.

suffix:String

Suffix to use for the temporary file.

directory:File

Temporary file directory.

initialBufferSize:int

The initial size of the in memory buffer.

Method Detail

closeback to summary
public void close() throws IOException

Overrides org.apache.tomcat.util.http.fileupload.ThresholdingOutputStream.close.

Implements java.io.Closeable.close.

Closes underlying output stream, and mark this as closed

Annotations
@Override
Exceptions
IOException:
if an error occurs.
getDataback 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 null.

Returns:byte[]

The data for this output stream, or null if no such data is available.

getFileback 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 null is returned.

Returns:File

The file for this output stream, or null if no such file exists.

getStreamback to summary
protected OutputStream getStream() throws IOException

Implements abstract org.apache.tomcat.util.http.fileupload.ThresholdingOutputStream.getStream.

Returns the current output stream. This may be memory based or disk based, depending on the current state with respect to the threshold.

Returns:OutputStream

The underlying output stream.

Annotations
@Override
Exceptions
IOException:
if an error occurs.
isInMemoryback to summary
public boolean isInMemory()

Determines whether or not the data for this output stream has been retained in memory.

Returns:boolean

true if the data is available in memory; false otherwise.

thresholdReachedback to summary
protected void thresholdReached() throws IOException

Implements abstract org.apache.tomcat.util.http.fileupload.ThresholdingOutputStream.thresholdReached.

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.

Annotations
@Override
Exceptions
IOException:
if an error occurs.