|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectjava.io.InputStream
com.threerings.io.FramedInputStream
public class FramedInputStream
The framed input stream reads input that was framed by a framing output stream. Framing in this case simply means writing the length of the frame followed by the data associated with the frame so that an entire frame can be loaded from the network layer before any higher layer attempts to process it. Additionally, any failure in decoding a frame won't result in the entire stream being skewed due to the remainder of the undecoded frame remaining in the input stream.
The framed input stream reads an entire frame worth of data into its
internal buffer when readFrame() is called. It then
behaves as if this is the only data available on the stream (meaning
that when the data in the frame is exhausted, it will behave as if the
end of the stream has been reached). The buffer can only contain a
single frame at a time, so any data left over from a previous frame
will disappear when readFrame() is called again.
Note: The framing input stream does not synchronize reads from its internal buffer. It is intended to only be accessed from a single thread.
Implementation note: maybe this should derive from
FilterInputStream and be tied to a single
InputStream for its lifetime.
| Field Summary | |
|---|---|
protected ByteBuffer |
_buffer
The buffer in which we maintain our frame data. |
protected int |
_have
The number of bytes total that we have in our buffer (these bytes may comprise more than one frame. |
protected int |
_length
The length of the current frame being read. |
protected static int |
HEADER_SIZE
The size of the frame header (a 32-bit integer). |
protected static int |
INITIAL_BUFFER_CAPACITY
The default initial size of the internal buffer. |
protected static int |
MAX_BUFFER_CAPACITY
No need to get out of hand. |
| Constructor Summary | |
|---|---|
FramedInputStream()
Creates a new framed input stream. |
|
| Method Summary | |
|---|---|
int |
available()
Returns the number of bytes that can be read from this input stream without blocking. |
protected boolean |
checkForCompleteFrame()
Returns true if a complete frame is in the buffer, false otherwise. |
protected int |
decodeLength()
Decodes and returns the length of the current frame from the buffer if possible. |
void |
mark(int readAheadLimit)
Does nothing, as marking is not supported. |
boolean |
markSupported()
Always returns false as framed input streams do not support marking. |
int |
read()
Reads the next byte of data from this input stream. |
int |
read(byte[] b,
int off,
int len)
Reads up to len bytes of data into an array of bytes
from this input stream. |
boolean |
readFrame(ReadableByteChannel source)
Reads a frame from the provided channel, appending to any partially read frame. |
void |
reset()
Resets the buffer to the beginning of the buffered frames. |
long |
skip(long n)
Skips n bytes of input from this input stream. |
| Methods inherited from class java.io.InputStream |
|---|
close, read |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected ByteBuffer _buffer
protected int _length
protected int _have
protected static final int HEADER_SIZE
protected static final int INITIAL_BUFFER_CAPACITY
protected static final int MAX_BUFFER_CAPACITY
| Constructor Detail |
|---|
public FramedInputStream()
| Method Detail |
|---|
public boolean readFrame(ReadableByteChannel source)
throws IOException
readFrame will return false, otherwise true.
Note: when this method returns true, it is required
that the caller read all of the frame data from the stream
before again calling readFrame(java.nio.channels.ReadableByteChannel) as the previous frame's
data will be elimitated upon the subsequent call.
IOExceptionprotected final int decodeLength()
protected final boolean checkForCompleteFrame()
InputStream interface.
public int read()
int in the range 0 to
255. If no byte is available because the end of the
stream has been reached, the value -1 is returned.
This read method cannot block.
read in class InputStream-1 if the end of the
stream has been reached.
public int read(byte[] b,
int off,
int len)
len bytes of data into an array of bytes
from this input stream. If pos equals
count, then -1 is returned to indicate
end of file. Otherwise, the number k of bytes read is
equal to the smaller of len and
count-pos. If k is positive, then bytes
buf[pos] through buf[pos+k-1] are copied
into b[off] through b[off+k-1] in the
manner performed by System.arraycopy. The value
k is added into pos and k is
returned.
This read method cannot block.
read in class InputStreamb - the buffer into which the data is read.off - the start offset of the data.len - the maximum number of bytes read.
-1 if there is no more data because the end of the
stream has been reached.public long skip(long n)
n bytes of input from this input stream. Fewer
bytes might be skipped if the end of the input stream is reached.
The actual number k of bytes to be skipped is equal to
the smaller of n and count-pos. The value
k is added into pos and k is
returned.
skip in class InputStreamn - the number of bytes to be skipped.
public int available()
available in class InputStreampublic boolean markSupported()
markSupported in class InputStreampublic void mark(int readAheadLimit)
mark in class InputStreampublic void reset()
reset in class InputStream
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||