Package com.helger.web.fileupload.io
Class AbstractThresholdingOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- com.helger.web.fileupload.io.AbstractThresholdingOutputStream
-
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
- Direct Known Subclasses:
DeferredFileOutputStream
public abstract class AbstractThresholdingOutputStream 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
OutputStreammethods. 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 736890 2009-01-23 02:02:22Z niallp $
- Author:
- Martin Cooper
-
-
Constructor Summary
Constructors Constructor Description AbstractThresholdingOutputStream(int nThreshold)Constructs an instance of this class which will trigger an event at the specified threshold.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidcheckThreshold(int nCount)Checks to see if writing the specified number of bytes would cause the configured threshold to be exceeded.voidclose()Closes this output stream and releases any system resources associated with this stream.voidflush()Flushes this output stream and forces any buffered output bytes to be written out.longgetByteCount()Returns the number of bytes that have been written to this output stream.protected abstract OutputStreamgetStream()Returns the underlying output stream, to which the correspondingOutputStreammethods in this class will ultimately delegate.intgetThreshold()Returns the threshold, in bytes, at which an event will be triggered.booleanisThresholdExceeded()Determines whether or not the configured threshold has been exceeded for this output stream.protected abstract voidonThresholdReached()Indicates that the configured threshold has been reached, and that a subclass should take whatever action necessary on this event.protected voidresetByteCount()Resets the byteCount to zero.voidwrite(byte[] b)Writesb.lengthbytes from the specified byte array to this output stream.voidwrite(byte[] b, int off, int len)Writeslenbytes from the specified byte array starting at offsetoffto this output stream.voidwrite(int b)Writes the specified byte to this output stream.-
Methods inherited from class java.io.OutputStream
nullOutputStream
-
-
-
-
Constructor Detail
-
AbstractThresholdingOutputStream
public AbstractThresholdingOutputStream(@Nonnegative int nThreshold)
Constructs an instance of this class which will trigger an event at the specified threshold.- Parameters:
nThreshold- The number of bytes at which to trigger an event.
-
-
Method Detail
-
write
public void write(int b) throws IOExceptionWrites the specified byte to this output stream.- Specified by:
writein classOutputStream- Parameters:
b- The byte to be written.- Throws:
IOException- if an error occurs.
-
write
public void write(@Nonnull byte[] b) throws IOException
Writesb.lengthbytes from the specified byte array to this output stream.- Overrides:
writein classOutputStream- Parameters:
b- The array of bytes to be written.- Throws:
IOException- if an error occurs.
-
write
public void write(@Nonnull byte[] b, @Nonnegative int off, @Nonnegative int len) throws IOException
Writeslenbytes from the specified byte array starting at offsetoffto this output stream.- Overrides:
writein classOutputStream- Parameters:
b- The byte array from which the data will be written.off- The start offset in the byte array.len- The number of bytes to write.- Throws:
IOException- if an error occurs.
-
flush
public void flush() throws IOExceptionFlushes this output stream and forces any buffered output bytes to be written out.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException- if an error occurs.
-
close
public void close() throws IOExceptionCloses this output stream and releases any system resources associated with this stream.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException- if an error occurs.
-
getThreshold
@Nonnegative public int getThreshold()
Returns the threshold, in bytes, at which an event will be triggered.- Returns:
- The threshold point, in bytes.
-
getByteCount
public long getByteCount()
Returns the number of bytes that have been written to this output stream.- Returns:
- The number of bytes written.
-
isThresholdExceeded
public boolean isThresholdExceeded()
Determines whether or not the configured threshold has been exceeded for this output stream.- Returns:
trueif the threshold has been reached;falseotherwise.
-
checkThreshold
protected void checkThreshold(int nCount) throws IOExceptionChecks 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- Parameters:
nCount- The number of bytes about to be written to the underlying output stream.- Throws:
IOException- if an error occurs.
-
resetByteCount
protected void resetByteCount()
Resets the byteCount to zero. You can call this fromonThresholdReached()if you want the event to be triggered again.
-
getStream
@Nonnull protected abstract OutputStream getStream() throws IOException
Returns the underlying output stream, to which the correspondingOutputStreammethods in this class will ultimately delegate.- Returns:
- The underlying output stream.
- Throws:
IOException- if an error occurs.
-
onThresholdReached
protected abstract void onThresholdReached() throws IOExceptionIndicates 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.- Throws:
IOException- if an error occurs.
-
-