org.apache.commons.fileupload

Class ThresholdingOutputStream

Known Direct Subclasses:
DeferredFileOutputStream

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.

Version:
$Id: ThresholdingOutputStream.java,v 1.3 2003/05/31 22:31:08 martinc Exp $

Author:
Martin Cooper

Field Summary

private int
threshold
The threshold at which the event will be triggered.
private boolean
thresholdExceeded
Whether or not the configured threshold has been exceeded.
private long
written
The number of bytes written to the output stream.

Constructor Summary

ThresholdingOutputStream(int threshold)
Constructs an instance of this class which will trigger an event at the specified threshold.

Method Summary

protected void
checkThreshold(int count)
Checks to see if writing the specified number of bytes would cause the configured threshold to be exceeded.
void
close()
Closes this output stream and releases any system resources associated with this stream.
void
flush()
Flushes this output stream and forces any buffered output bytes to be written out.
long
getByteCount()
Returns the number of bytes that have been written to this output stream.
protected OutputStream
getStream()
Returns the underlying output stream, to which the corresponding OutputStream methods in this class will ultimately delegate.
int
getThreshold()
Returns the threshold, in bytes, at which an event will be triggered.
boolean
isThresholdExceeded()
Determines whether or not the configured threshold has been exceeded for this output stream.
protected void
thresholdReached()
Indicates that the configured threshold has been reached, and that a subclass should take whatever action necessary on this event.
void
write(b[] )
Writes b.length bytes from the specified byte array to this output stream.
void
write(b[] , int off, int len)
Writes len bytes from the specified byte array starting at offset off to this output stream.
void
write(int b)
Writes the specified byte to this output stream.

Field Details

threshold

private int threshold
The threshold at which the event will be triggered.


thresholdExceeded

private boolean thresholdExceeded
Whether or not the configured threshold has been exceeded.


written

private long written
The number of bytes written to the output stream.

Constructor Details

ThresholdingOutputStream

public ThresholdingOutputStream(int threshold)
Constructs an instance of this class which will trigger an event at the specified threshold.

Parameters:
threshold - The number of bytes at which to trigger an event.

Method Details

checkThreshold

protected void checkThreshold(int count)
            throws IOException
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.

Parameters:
count - The number of bytes about to be written to the underlying output stream.


close

public void close()
            throws IOException
Closes this output stream and releases any system resources associated with this stream.


flush

public void flush()
            throws IOException
Flushes this output stream and forces any buffered output bytes to be written out.


getByteCount

public long getByteCount()
Returns the number of bytes that have been written to this output stream.

Returns:
The number of bytes written.


getStream

protected OutputStream getStream()
            throws IOException
Returns the underlying output stream, to which the corresponding OutputStream methods in this class will ultimately delegate.

Returns:
The underlying output stream.


getThreshold

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

Returns:
The threshold point, in bytes.


isThresholdExceeded

public boolean isThresholdExceeded()
Determines whether or not the configured threshold has been exceeded for this output stream.

Returns:
true if the threshold has been reached; false otherwise.


thresholdReached

protected void thresholdReached()
            throws IOException
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.


write

public void write(b[] )
            throws IOException
Writes b.length bytes from the specified byte array to this output stream.

Parameters:


write

public void write(b[] ,
                  int off,
                  int len)
            throws IOException
Writes len bytes from the specified byte array starting at offset off to this output stream.

Parameters:
off - The start offset in the byte array.
len - The number of bytes to write.


write

public void write(int b)
            throws IOException
Writes the specified byte to this output stream.

Parameters:
b - The byte to be written.


Copyright © 2002-2003 Apache Software Foundation. All Rights Reserved.