Package net.i2p.client.streaming.impl
Class MessageInputStream
java.lang.Object
java.io.InputStream
net.i2p.client.streaming.impl.MessageInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
class MessageInputStream extends InputStream
Stream that can be given messages out of order
yet present them in order.
I2PSession -> MessageHandler -> PacketHandler -> ConnectionPacketHandler -> MessageInputStream
This buffers unlimited data via messageReceived() - limiting / blocking is done in ConnectionPacketHandler.receivePacket().
-
Constructor Summary
Constructors Constructor Description MessageInputStream(I2PAppContext ctx, int maxMessageSize, int maxWindowSize, int maxBufferSize) -
Method Summary
Modifier and Type Method Description intavailable()booleancanAccept(long messageId, int payloadSize)Determine if this packet will fit in our buffering limits.voidclose()voidcloseReceived()There is no more data coming from the I2P side.longgetHighestBlockId()longgetHighestReadyBlockId()What is the highest block ID we've completely received through?long[]getNacks()Retrieve the message IDs that are holes in our sequence - ones past the highest ready ID and below the highest received message ID.intgetReadTimeout()how long a read() call should block (if less than 0, block indefinitely, but if it is 0, do not block at all)intgetTotalReadySize()Same as available() but doesn't throw IOEbooleanisLocallyClosed()booleanmessageReceived(long messageId, ByteArray payload)A new message has arrived - toss it on the appropriate queue (moving previously pending messages to the ready queue if it fills the gap, etc).voidnotifyActivity()intread()On a read timeout, this throws a SocketTimeoutException as of 0.9.36.intread(byte[] target)On a read timeout, this throws a SocketTimeoutException as of 0.9.36.intread(byte[] target, int offset, int length)On a read timeout, this throws a SocketTimeoutException as of 0.9.36.voidsetReadTimeout(int timeout)how long a read() call should block (if less than 0, block indefinitely, but if it is 0, do not block at all)(package private) voidstreamErrorOccurred(IOException ioe)Stream b0rked, die with the given errorvoidupdateAcks(PacketLocal packet)Adds the ack-through and nack fields to a packet we are building for transmissionMethods inherited from class java.io.InputStream
mark, markSupported, nullInputStream, readAllBytes, readNBytes, readNBytes, reset, skip, skipNBytes, transferTo
-
Constructor Details
-
MessageInputStream
public MessageInputStream(I2PAppContext ctx, int maxMessageSize, int maxWindowSize, int maxBufferSize)
-
-
Method Details
-
getHighestReadyBlockId
public long getHighestReadyBlockId()What is the highest block ID we've completely received through?- Returns:
- highest data block ID completely received or -1 for none
-
getHighestBlockId
public long getHighestBlockId()- Returns:
- highest data block ID received or -1 for none
-
isLocallyClosed
public boolean isLocallyClosed()- Returns:
- true if this has been closed on the read side with close()
-
canAccept
public boolean canAccept(long messageId, int payloadSize)Determine if this packet will fit in our buffering limits. Always returns true for zero payloadSize and dups, even if locally closed. Returns false if there is no room, OR it's not a dup and the stream has been closed on the read side with close(). If this returns false, you probably want to call isLocallyClosed() to find out why.- Returns:
- true if we have room. If false, do not call messageReceived()
- Since:
- 0.9.20 moved from ConnectionPacketHandler.receivePacket() so it can all be under one lock, and we can efficiently do several checks
-
getNacks
public long[] getNacks()Retrieve the message IDs that are holes in our sequence - ones past the highest ready ID and below the highest received message ID. This may return null if there are no such IDs.- Returns:
- array of message ID holes, or null if none
-
updateAcks
Adds the ack-through and nack fields to a packet we are building for transmission -
getReadTimeout
public int getReadTimeout()how long a read() call should block (if less than 0, block indefinitely, but if it is 0, do not block at all)- Returns:
- how long read calls should block, 0 for nonblocking, negative to indefinitely block
-
setReadTimeout
public void setReadTimeout(int timeout)how long a read() call should block (if less than 0, block indefinitely, but if it is 0, do not block at all)- Parameters:
timeout- how long read calls should block, 0 for nonblocking, negative to indefinitely block
-
closeReceived
public void closeReceived()There is no more data coming from the I2P side. Does NOT clear pending data. messageReceived() MUST have been called previously with the messageId of the CLOSE packet. -
notifyActivity
public void notifyActivity() -
messageReceived
A new message has arrived - toss it on the appropriate queue (moving previously pending messages to the ready queue if it fills the gap, etc). This does no limiting of pending data - see canAccept() for limiting. Warning - returns true if locally closed.- Parameters:
messageId- ID of the messagepayload- message payload, may be null or have null or zero-length data- Returns:
- true if this is a new packet, false if it is a dup
-
read
On a read timeout, this throws a SocketTimeoutException as of 0.9.36. Prior to that, returned -1.- Specified by:
readin classInputStream- Throws:
IOException
-
read
On a read timeout, this throws a SocketTimeoutException as of 0.9.36. Prior to that, returned 0.- Overrides:
readin classInputStream- Throws:
IOException
-
read
On a read timeout, this throws a SocketTimeoutException as of 0.9.36. Prior to that, returned 0.- Overrides:
readin classInputStream- Throws:
IOException
-
available
- Overrides:
availablein classInputStream- Throws:
IOException
-
getTotalReadySize
public int getTotalReadySize()Same as available() but doesn't throw IOE -
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream
-
streamErrorOccurred
Stream b0rked, die with the given error
-