Package net.targetr.wtm3.io
Class LengthRestrictedOutputStream
java.lang.Object
java.io.OutputStream
net.targetr.wtm3.io.LengthRestrictedOutputStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
Prevents writing too much data to an output stream.
This class simplifies writing a predefined length of data to a stream, tracking bytes written and supporting two modes of operation when the limit is reached:
LengthRestrictedOutputStream.Mode.THROW_EXCEPTION: throws anIOExceptionif a write exceeds the limit.LengthRestrictedOutputStream.Mode.STOP_WRITING: stops writing and discards any subsequent data without throwing an exception.
- Author:
- Dr Michael Gardiner
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumSpecifies the behavior when the write limit is reached. -
Constructor Summary
ConstructorsConstructorDescriptionLengthRestrictedOutputStream(OutputStream out, long len) Decorates an output stream to limit how much data can be written to it, throwing anIOExceptionif the limit is exceeded.LengthRestrictedOutputStream(OutputStream out, long len, boolean throwException) Decorates an output stream to limit how much data can be written to it.LengthRestrictedOutputStream(OutputStream out, long len, LengthRestrictedOutputStream.Mode mode) Decorates an output stream to limit how much data can be written to it using the specified mode. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the underlying output stream.voidflush()Flushes the underlying output stream.longReturns the total number of bytes written to this stream so far.getMode()Returns the operational mode of this stream.Returns the underlying wrapped output stream.longReturns the remaining number of bytes that can be written before reaching the limit.longReturns the maximum number of bytes that can be written to this stream.Returns the underlying wrapped output stream.booleanChecks if the stream has reached the allowed write limit.booleanChecks if any write attempt exceeded the limit.booleanChecks if the write limit was reached.toString()Returns a string representation of this stream.voidwrite(byte[] buffer) Writes all bytes from the specified byte array to the output stream.voidwrite(byte[] buffer, int off, int len) Writes a portion of a byte array to the output stream.voidwrite(int b) Writes a single byte to the output stream.Methods inherited from class java.io.OutputStream
nullOutputStream
-
Constructor Details
-
LengthRestrictedOutputStream
public LengthRestrictedOutputStream(OutputStream out, long len, LengthRestrictedOutputStream.Mode mode) Decorates an output stream to limit how much data can be written to it using the specified mode.- Parameters:
out- The output stream to write to.len- The maximum number of bytes that may be written.mode- The mode of operation when the limit is reached.- Throws:
IllegalArgumentException- if len is negativeNullPointerException- if out or mode is null
-
LengthRestrictedOutputStream
Decorates an output stream to limit how much data can be written to it.- Parameters:
out- The output stream to write to.len- The maximum number of bytes that may be written.throwException- true to throw anIOExceptionwhen the limit is exceeded; false to stop writing after the limit is reached.- Throws:
IllegalArgumentException- if len is negativeNullPointerException- if out is null
-
LengthRestrictedOutputStream
Decorates an output stream to limit how much data can be written to it, throwing anIOExceptionif the limit is exceeded.- Parameters:
out- The output stream to write to.len- The maximum number of bytes that may be written.- Throws:
IllegalArgumentException- if len is negativeNullPointerException- if out is null
-
-
Method Details
-
write
Writes a single byte to the output stream.- Specified by:
writein classOutputStream- Parameters:
b- The byte to write (the 8 low-order bits).- Throws:
IOException- if writing exceeds the length limit inLengthRestrictedOutputStream.Mode.THROW_EXCEPTIONmode, or if an underlying I/O error occurs.
-
write
Writes all bytes from the specified byte array to the output stream.- Overrides:
writein classOutputStream- Parameters:
buffer- The buffer containing data to write.- Throws:
IOException- if writing exceeds the length limit inLengthRestrictedOutputStream.Mode.THROW_EXCEPTIONmode, or if an underlying I/O error occurs.NullPointerException- if buffer is null.
-
write
Writes a portion of a byte array to the output stream.- Overrides:
writein classOutputStream- Parameters:
buffer- The buffer containing data to write.off- The start offset in the data.len- The number of bytes to write.- Throws:
IOException- if writing exceeds the length limit inLengthRestrictedOutputStream.Mode.THROW_EXCEPTIONmode, or if an underlying I/O error occurs.IndexOutOfBoundsException- if off or len are invalid.NullPointerException- if buffer is null.
-
flush
Flushes the underlying output stream.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException- if an I/O error occurs.
-
close
Closes the underlying output stream.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException- if an I/O error occurs.
-
getBytesWritten
public long getBytesWritten()Returns the total number of bytes written to this stream so far.- Returns:
- the number of bytes written
-
getRestriction
public long getRestriction()Returns the maximum number of bytes that can be written to this stream.- Returns:
- the byte limit
-
getRemaining
public long getRemaining()Returns the remaining number of bytes that can be written before reaching the limit.- Returns:
- the remaining bytes allowed
-
isLimitReached
public boolean isLimitReached()Checks if the write limit was reached.- Returns:
- true if the limit was reached, false otherwise
-
isLimitExceeded
public boolean isLimitExceeded()Checks if any write attempt exceeded the limit.- Returns:
- true if an attempt was made to write past the limit, false otherwise
-
isFinished
public boolean isFinished()Checks if the stream has reached the allowed write limit.- Returns:
- true if the limit is reached, false otherwise
-
getMode
Returns the operational mode of this stream.- Returns:
- the operational mode
-
getOutputStream
Returns the underlying wrapped output stream.- Returns:
- the wrapped output stream
-
getWrappedOutputStream
Returns the underlying wrapped output stream.- Returns:
- the wrapped output stream
-
toString
Returns a string representation of this stream.
-