public abstract class CumulativeProtocolDecoder extends ProtocolDecoderAdapter
ProtocolDecoder
that cumulates the content of received buffers to a
cumulative buffer to help users implement decoders.
If the received IoBuffer
is only a part of a message. decoders should
cumulate received buffers to make a message complete or to postpone decoding
until more buffers arrive.
Here is an example decoder that decodes CRLF terminated lines into
Command
objects:
public class CrLfTerminatedCommandLineDecoder extends CumulativeProtocolDecoder { private Command parseCommand(IoBuffer in) { // Convert the bytes in the specified buffer to a // Command object. ... } protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { // Remember the initial position. int start = in.position(); // Now find the first CRLF in the buffer. byte previous = 0; while (in.hasRemaining()) { byte current = in.get(); if (previous == '\r' && current == '\n') { // Remember the current position and limit. int position = in.position(); int limit = in.limit(); try { in.position(start); in.limit(position); // The bytes between in.position() and in.limit() // now contain a full CRLF terminated line. out.write(parseCommand(in.slice())); } finally { // Set the position to point right after the // detected line and set the limit to the old // one. in.position(position); in.limit(limit); } // Decoded one line; CumulativeProtocolDecoder will // call me again until I return false. So just // return true until there are no more lines in the // buffer. return true; } previous = current; } // Could not find CRLF in the buffer. Reset the initial // position to the one we recorded above. in.position(start); return false; } }
Please note that this decoder simply forward the call to
doDecode(IoSession, IoBuffer, ProtocolDecoderOutput)
if the
underlying transport doesn't have a packet fragmentation. Whether the
transport has fragmentation or not is determined by querying
TransportMetadata
.
限定符 | 构造器和说明 |
---|---|
protected |
CumulativeProtocolDecoder()
Creates a new instance.
|
限定符和类型 | 方法和说明 |
---|---|
void |
decode(IoSession session,
IoBuffer in,
ProtocolDecoderOutput out)
Cumulates content of in into internal buffer and forwards
decoding request to
doDecode(IoSession, IoBuffer, ProtocolDecoderOutput) . |
void |
dispose(IoSession session)
Releases the cumulative buffer used by the specified session.
|
protected abstract boolean |
doDecode(IoSession session,
IoBuffer in,
ProtocolDecoderOutput out)
Implement this method to consume the specified cumulative buffer and
decode its content into message(s).
|
finishDecode
public void decode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws java.lang.Exception
doDecode(IoSession, IoBuffer, ProtocolDecoderOutput)
.
doDecode() is invoked repeatedly until it returns false
and the cumulative buffer is compacted after decoding ends.session
- The current Sessionin
- the buffer to decodeout
- The ProtocolDecoderOutput
that will receive the decoded messagejava.lang.IllegalStateException
- if your doDecode() returned true not
consuming the cumulative buffer.java.lang.Exception
- if the read data violated protocol specificationprotected abstract boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws java.lang.Exception
session
- The current Sessionin
- the cumulative bufferout
- The ProtocolDecoderOutput
that will receive the decoded messagejava.lang.Exception
- if cannot decode in.public void dispose(IoSession session) throws java.lang.Exception
dispose
在接口中 ProtocolDecoder
dispose
在类中 ProtocolDecoderAdapter
session
- The current Sessionjava.lang.Exception
- if failed to dispose all resources