FileDocCategorySizeDatePackage
ThresholdingOutputStream.javaAPI DocAndroid 1.5 API7494Wed May 06 22:42:46 BST 2009org.apache.commons.io.output

ThresholdingOutputStream

public abstract class ThresholdingOutputStream extends OutputStream
An output stream which triggers an event when a specified number of bytes of data have been written to it. The event can be used, for example, to throw an exception if a maximum has been reached, or to switch the underlying stream type when the threshold is exceeded.

This class overrides all OutputStream methods. However, these overrides ultimately call the corresponding methods in the underlying output stream implementation.

NOTE: This implementation may trigger the event before the threshold is actually reached, since it triggers when a pending write operation would cause the threshold to be exceeded.

author
Martin Cooper
version
$Id: ThresholdingOutputStream.java 540714 2007-05-22 19:39:44Z niallp $

Fields Summary
private int
threshold
The threshold at which the event will be triggered.
private long
written
The number of bytes written to the output stream.
private boolean
thresholdExceeded
Whether or not the configured threshold has been exceeded.
Constructors Summary
public ThresholdingOutputStream(int threshold)
Constructs an instance of this class which will trigger an event at the specified threshold.

param
threshold The number of bytes at which to trigger an event.

        this.threshold = threshold;
    
Methods Summary
protected voidcheckThreshold(int count)
Checks to see if writing the specified number of bytes would cause the configured threshold to be exceeded. If so, triggers an event to allow a concrete implementation to take action on this.

param
count The number of bytes about to be written to the underlying output stream.
exception
IOException if an error occurs.

        if (!thresholdExceeded && (written + count > threshold))
        {
            thresholdExceeded = true;
            thresholdReached();
        }
    
public voidclose()
Closes this output stream and releases any system resources associated with this stream.

exception
IOException if an error occurs.

        try
        {
            flush();
        }
        catch (IOException ignored)
        {
            // ignore
        }
        getStream().close();
    
public voidflush()
Flushes this output stream and forces any buffered output bytes to be written out.

exception
IOException if an error occurs.

        getStream().flush();
    
public longgetByteCount()
Returns the number of bytes that have been written to this output stream.

return
The number of bytes written.

        return written;
    
protected abstract java.io.OutputStreamgetStream()
Returns the underlying output stream, to which the corresponding OutputStream methods in this class will ultimately delegate.

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

public intgetThreshold()
Returns the threshold, in bytes, at which an event will be triggered.

return
The threshold point, in bytes.

        return threshold;
    
public booleanisThresholdExceeded()
Determines whether or not the configured threshold has been exceeded for this output stream.

return
true if the threshold has been reached; false otherwise.

        return (written > threshold);
    
protected voidresetByteCount()
Resets the byteCount to zero. You can call this from {@link #thresholdReached()} if you want the event to be triggered again.

        this.thresholdExceeded = false;
        this.written = 0;
    
protected abstract voidthresholdReached()
Indicates that the configured threshold has been reached, and that a subclass should take whatever action necessary on this event. This may include changing the underlying output stream.

exception
IOException if an error occurs.

public voidwrite(int b)
Writes the specified byte to this output stream.

param
b The byte to be written.
exception
IOException if an error occurs.

        checkThreshold(1);
        getStream().write(b);
        written++;
    
public voidwrite(byte[] b)
Writes b.length bytes from the specified byte array to this output stream.

param
b The array of bytes to be written.
exception
IOException if an error occurs.

        checkThreshold(b.length);
        getStream().write(b);
        written += b.length;
    
public voidwrite(byte[] b, int off, int len)
Writes len bytes from the specified byte array starting at offset off to this output stream.

param
b The byte array from which the data will be written.
param
off The start offset in the byte array.
param
len The number of bytes to write.
exception
IOException if an error occurs.

        checkThreshold(len);
        getStream().write(b, off, len);
        written += len;