FileDocCategorySizeDatePackage
DeferredFileOutputStream.javaAPI DocApache Tomcat 6.0.145568Fri Jul 20 04:20:30 BST 2007org.apache.tomcat.util.http.fileupload

DeferredFileOutputStream

public class DeferredFileOutputStream extends ThresholdingOutputStream

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.

author
Martin Cooper
version
$Id: DeferredFileOutputStream.java 467222 2006-10-24 03:17:11Z markt $

Fields Summary
private ByteArrayOutputStream
memoryOutputStream
The output stream to which data will be written prior to the theshold being reached.
private FileOutputStream
diskOutputStream
The output stream to which data will be written after the theshold is reached.
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.
private File
outputFile
The file to which output will be directed if the threshold is exceeded.
Constructors 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.

param
threshold The number of bytes at which to trigger an event.
param
outputFile The file to which data is saved beyond the threshold.

        super(threshold);
        this.outputFile = outputFile;

        memoryOutputStream = new ByteArrayOutputStream(threshold);
        currentOutputStream = memoryOutputStream;
    
Methods 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.

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

        if (memoryOutputStream != null)
        {
            return memoryOutputStream.toByteArray();
        }
        return null;
    
public java.io.FilegetFile()
Returns the data for this output stream as a File, assuming that the data was written to disk. If the data was retained in memory, this method returns null.

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

        return outputFile;
    
protected java.io.OutputStreamgetStream()
Returns the current output stream. This may be memory based or disk based, depending on the current state with respect to the threshold.

return
The underlying output stream.
exception
IOException if an error occurs.

        return currentOutputStream;
    
public booleanisInMemory()
Determines whether or not the data for this output stream has been retained in memory.

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

        return (!isThresholdExceeded());
    
protected voidthresholdReached()
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.

exception
IOException if an error occurs.

        byte[] data = memoryOutputStream.toByteArray();
        FileOutputStream fos = new FileOutputStream(outputFile);
        fos.write(data);
        diskOutputStream = fos;
        currentOutputStream = fos;
        memoryOutputStream = null;