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 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.
-
-
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 Modifier and Type Method Description 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.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.void
write(byte[] b)
Writesb.length
bytes from the specified byte array to this output stream.void
write(byte[] b, int off, int len)
Writeslen
bytes from the specified byte array starting at offsetoff
to this output stream.void
write(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.- 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
-
close
public void close() throws java.io.IOException
Closes this output stream and releases any system resources associated with this stream.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in 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:
flush
in interfacejava.io.Flushable
- Overrides:
flush
in classjava.io.OutputStream
- Throws:
java.io.IOException
- if an error occurs.
-
getByteCount
public long getByteCount()
Returns the number of bytes that have been written to this output stream.- Returns:
- The number of bytes written.
-
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.
-
write
public void write(byte[] b) throws java.io.IOException
Writesb.length
bytes from the specified byte array to this output stream.- Overrides:
write
in classjava.io.OutputStream
- Parameters:
b
- The array of bytes to be written.- Throws:
java.io.IOException
- if an error occurs.
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException
Writeslen
bytes from the specified byte array starting at offsetoff
to this output stream.- Overrides:
write
in 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.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:
write
in classjava.io.OutputStream
- Parameters:
b
- The byte to be written.- Throws:
java.io.IOException
- if an error occurs.
-
-