class MessageOutputStream extends OutputStream
MessageOutputStream.DataReceiver in chunks upon buffer full or flush.
Provides throttling and blocking behavior according to the data receiver's needs.
This class maintains an internal buffer with configurable size limits,
supports resizing the buffer size during operation (only effective when buffer is empty),
and allows passive flushes after inactivity delays to avoid excessive buffering.
MessageOutputStream -> ConnectionDataReceiver -> Connection -> PacketQueue -> I2PSession
Thread safety:
- Write operations synchronize on an internal data lock when manipulating the buffer.
- Waiting for buffer acceptance and completion is done outside synchronization to avoid blocking other threads.
Performance:
- Uses a shared ByteCache for buffer byte arrays to reduce allocations.
- Supports a passive flush mechanism to flush buffered data after inactivity.
- Minimizes locking time by doing blocking waits outside of synchronized blocks.
Note:
- Buffer resizing only affects the max size used to determine flush boundaries,
buffer reallocation does not occur dynamically.
- Stream errors are captured and thrown upon subsequent I/O calls.
- Interruptions during wait throw InterruptedIOException accordingly.
- Close() flushes any remaining data and releases internal buffers.| Modifier and Type | Class and Description |
|---|---|
static interface |
MessageOutputStream.DataReceiver
Interface for receiving data flushed from this stream.
|
static interface |
MessageOutputStream.WriteStatus
Interface to detect status of an asynchronous write operation.
|
| Constructor and Description |
|---|
MessageOutputStream(I2PAppContext ctx,
SimpleTimer2 timer,
MessageOutputStream.DataReceiver receiver,
int bufSize,
int initBufSize)
Constructs the stream with default passive flush delay.
|
MessageOutputStream(I2PAppContext ctx,
SimpleTimer2 timer,
MessageOutputStream.DataReceiver receiver,
int bufSize,
int initBufSize,
int passiveFlushDelay)
Constructs the stream with specified passive flush delay.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes the stream, flushing all buffered data and waiting
for any CLOSE packet acknowledgments.
|
(package private) void |
closeInternal()
Non-blocking internal close used within the package.
|
(package private) void |
destroy()
Releases resources without flushing.
|
void |
flush()
Flushes buffered data, blocking until accepted into the send queue.
|
(package private) void |
flushAvailable(MessageOutputStream.DataReceiver target)
Convenience overload variant with blocking flush.
|
(package private) void |
flushAvailable(MessageOutputStream.DataReceiver target,
boolean blocking)
Attempts to flush buffered data to the target DataReceiver,
optionally blocking until accepted.
|
boolean |
getClosed()
Returns true if the stream is closed.
|
int |
getWriteTimeout()
Gets the current write timeout.
|
void |
setBufferSize(int size)
Requests resizing the buffer to the specified size.
|
void |
setWriteTimeout(int ms)
Sets the write timeout in milliseconds for blocking write operations.
|
(package private) void |
streamErrorOccurred(IOException ioe)
Records a stream error and clears buffered data.
|
void |
write(byte[] b)
Writes the specified byte array to the stream, may block waiting for acceptance.
|
void |
write(byte[] b,
int off,
int len)
Writes len bytes from the specified byte array starting at offset off.
|
void |
write(int b)
Writes a single byte to the stream.
|
public MessageOutputStream(I2PAppContext ctx, SimpleTimer2 timer, MessageOutputStream.DataReceiver receiver, int bufSize, int initBufSize)
ctx - Application context for logging and utilitiestimer - Timer used for scheduling passive flush eventsreceiver - DataReceiver that handles actual data dispatchbufSize - Maximum buffer size allowed (will be clamped to preset max values)initBufSize - Initial buffer size to use for buffering (<= bufSize)public MessageOutputStream(I2PAppContext ctx, SimpleTimer2 timer, MessageOutputStream.DataReceiver receiver, int bufSize, int initBufSize, int passiveFlushDelay)
ctx - Application context for logging and utilitiestimer - Timer used for scheduling passive flush eventsreceiver - DataReceiver that handles actual data dispatchbufSize - Maximum buffer size allowed (will be clamped for safety)initBufSize - Initial buffer size to use for buffering (<= bufSize)passiveFlushDelay - Delay in ms before passive flush if data is buffered but not flushedpublic void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableclose in class OutputStreamIOException - if closing encounters an errorvoid closeInternal()
void destroy()
public void flush()
throws IOException
flush in interface Flushableflush in class OutputStreamIOException - if flushing encounters an errorvoid flushAvailable(MessageOutputStream.DataReceiver target) throws IOException
target - DataReceiver to flush toIOException - if flush fails or interruptedvoid flushAvailable(MessageOutputStream.DataReceiver target, boolean blocking) throws IOException
target - the destination DataReceiverblocking - whether the flush should block until acceptedIOException - if the flush fails or is interruptedpublic boolean getClosed()
public int getWriteTimeout()
public void setBufferSize(int size)
size - New desired buffer size, must be positive and no greater than original buffer size.public void setWriteTimeout(int ms)
ms - Timeout in milliseconds, -1 for infinite wait.void streamErrorOccurred(IOException ioe)
ioe - The IOException to recordpublic void write(byte[] b)
throws IOException
write in class OutputStreamb - byte array to writeIOException - if the stream is closed or write failspublic void write(byte[] b,
int off,
int len)
throws IOException
write in class OutputStreamb - byte array sourceoff - start offset in arraylen - number of bytes to writeIOException - if the stream is closed or write failspublic void write(int b)
throws IOException
write in class OutputStreamb - byte to write (only least significant 8 bits used)IOException - if the stream is closed or write fails