Package org.apache.commons.io.output
Class ThresholdingOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- org.apache.commons.io.output.ThresholdingOutputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
- Direct Known Subclasses:
DeferredFileOutputStream
public class ThresholdingOutputStream extends java.io.OutputStream
An output stream which triggers an event on the first write that causes the total number of bytes written to the stream to exceed a configured threshold, and every subsequent write. The event can be used, for example, to throw an exception if a maximum has been reached, or to switch the underlying stream 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.
See also the subclass
DeferredFileOutputStream.- See Also:
DeferredFileOutputStream
-
-
Constructor Summary
Constructors Constructor Description ThresholdingOutputStream(int threshold)Constructs an instance of this class which will trigger an event at the specified threshold.ThresholdingOutputStream(int threshold, IOConsumer<ThresholdingOutputStream> thresholdConsumer, IOFunction<ThresholdingOutputStream,java.io.OutputStream> outputStreamGetter)Constructs an instance of this class which will trigger an event at the specified threshold.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected voidcheckThreshold(int count)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()Gets the number of bytes that have been written to this output stream.protected java.io.OutputStreamgetOutputStream()Gets the underlying output stream, to which the correspondingOutputStreammethods in this class will ultimately delegate.protected java.io.OutputStreamgetStream()Deprecated.UsegetOutputStream().intgetThreshold()Gets the threshold, in bytes, at which an event will be triggered.booleanisThresholdExceeded()Tests whether or not the configured threshold has been exceeded for this output stream.protected voidresetByteCount()Resets the byteCount to zero.protected voidsetByteCount(long count)Sets the byteCount to count.protected voidthresholdReached()Indicates that the configured threshold has been reached, and that a subclass should take whatever action necessary on this event.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.
-
-
-
Constructor Detail
-
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.
-
ThresholdingOutputStream
public ThresholdingOutputStream(int threshold, IOConsumer<ThresholdingOutputStream> thresholdConsumer, IOFunction<ThresholdingOutputStream,java.io.OutputStream> outputStreamGetter)
Constructs an instance of this class which will trigger an event at the specified threshold. A negative threshold has no meaning and will be treated as 0- Parameters:
threshold- The number of bytes at which to trigger an event.thresholdConsumer- Accepts reaching the threshold.outputStreamGetter- Gets the output stream.- Since:
- 2.9.0
-
-
Method Detail
-
checkThreshold
protected void checkThreshold(int count) throws java.io.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.- Throws:
java.io.IOException- if an error occurs.
-
close
public void close() throws java.io.IOException
Closes this output stream and releases any system resources associated with this stream.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.OutputStream- Throws:
java.io.IOException- if an error occurs.
-
flush
public void flush() throws java.io.IOException
Flushes this output stream and forces any buffered output bytes to be written out.- Specified by:
flushin interfacejava.io.Flushable- Overrides:
flushin classjava.io.OutputStream- Throws:
java.io.IOException- if an error occurs.
-
getByteCount
public long getByteCount()
Gets the number of bytes that have been written to this output stream.- Returns:
- The number of bytes written.
-
getOutputStream
protected java.io.OutputStream getOutputStream() throws java.io.IOException
Gets the underlying output stream, to which the correspondingOutputStreammethods in this class will ultimately delegate.- Returns:
- The underlying output stream.
- Throws:
java.io.IOException- if an error occurs.- Since:
- 2.14.0
-
getStream
@Deprecated protected java.io.OutputStream getStream() throws java.io.IOException
Deprecated.UsegetOutputStream().Gets the underlying output stream, to which the correspondingOutputStreammethods in this class will ultimately delegate.- Returns:
- The underlying output stream.
- Throws:
java.io.IOException- if an error occurs.
-
getThreshold
public int getThreshold()
Gets the threshold, in bytes, at which an event will be triggered.- Returns:
- The threshold point, in bytes.
-
isThresholdExceeded
public boolean isThresholdExceeded()
Tests whether or not the configured threshold has been exceeded for this output stream.- Returns:
trueif the threshold has been reached;falseotherwise.
-
resetByteCount
protected void resetByteCount()
Resets the byteCount to zero. You can call this fromthresholdReached()if you want the event to be triggered again.
-
setByteCount
protected void setByteCount(long count)
Sets the byteCount to count. Useful for re-opening an output stream that has previously been written to.- Parameters:
count- The number of bytes that have already been written to the output stream.- Since:
- 2.5
-
thresholdReached
protected void thresholdReached() throws java.io.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.- Throws:
java.io.IOException- if an error occurs.
-
write
public void write(byte[] b) throws java.io.IOException
Writesb.lengthbytes from the specified byte array to this output stream.- Overrides:
writein classjava.io.OutputStream- Parameters:
b- The array of bytes to be written.- Throws:
java.lang.NullPointerException- if the byte array isnull.java.io.IOException- if an error occurs.
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException
Writeslenbytes from the specified byte array starting at offsetoffto this output stream.- Overrides:
writein classjava.io.OutputStream- 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:
java.lang.NullPointerException- if the byte array isnull.java.lang.IndexOutOfBoundsException- ifofforlenare negative, or ifoff + lenis greater thanb.length.java.io.IOException- if an error occurs.
-
write
public void write(int b) throws java.io.IOException
Writes the specified byte to this output stream.- Specified by:
writein classjava.io.OutputStream- Parameters:
b- The byte to be written.- Throws:
java.io.IOException- if an error occurs.
-
-