java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
net.i2p.i2ptunnel.util.LimitOutputStream
- All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
- Direct Known Subclasses:
ByteLimitOutputStream, DechunkedOutputStream
Abstract output stream that limits writes and provides completion callback.
This class extends FilterOutputStream to provide a framework for implementing output streams with write limitations or size restrictions. It tracks completion state and calls a callback when the stream is finished, preventing multiple completion attempts.
Subclasses implement specific limiting behavior while inheriting callback functionality and completion state management. Useful for implementing streams that need to enforce size limits or provide notification when writing is complete.
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceCallback interface for notification when a limited output stream completes.Field Summary
FieldsModifier and TypeFieldDescriptionprivate final byte[]Single-byte buffer for the write(int) method.protected final LimitOutputStream.DoneCallbackInvoked once bysetDone()when the subclass has finished writing.protected booleanTrue once setDone() has run, so the callback cannot fire twice.Fields inherited from class FilterOutputStream
outConstructor Summary
ConstructorsConstructorDescriptionCreates a limiting stream wrapping the given target stream.Method Summary
Public MethodsModifier and TypeMethodDescriptionprotected booleanisDone()Whether setDone() has already run, so the done callback cannot fire twice.protected voidsetDone()flush(), call the callback, and set _isDone.voidwrite(byte[] buf, int off, int len) Subclasses MUST override the following method such that it calls done() when finished and throws EOFException if called again.voidwrite(int c) Buffers the single byte and writes it through the bulk method.Methods inherited from class FilterOutputStream
close, flush, writeMethods inherited from class OutputStream
nullOutputStream
Field Details
_buf1
private final byte[] _buf1Single-byte buffer for the write(int) method._callback
Invoked once bysetDone()when the subclass has finished writing._isDone
protected boolean _isDoneTrue once setDone() has run, so the callback cannot fire twice.
Constructor Details
LimitOutputStream
Creates a limiting stream wrapping the given target stream.- Parameters:
out- the stream writes are forwarded todone- non-null
Method Details
write
Buffers the single byte and writes it through the bulk method.- Overrides:
writein classFilterOutputStream- Throws:
IOException
write
Subclasses MUST override the following method such that it calls done() when finished and throws EOFException if called again.- Overrides:
writein classFilterOutputStream- Throws:
IOException
isDone
protected boolean isDone()Whether setDone() has already run, so the done callback cannot fire twice.- Returns:
- true if setDone() has already run
setDone
flush(), call the callback, and set _isDone.- Throws:
IOException- if the wrapped stream cannot be flushed