public class ObjectDecoder extends LengthFieldBasedFrameDecoder
ByteBuf
s into Java
objects.
Please note that the serialized form this decoder expects is not
compatible with the standard ObjectOutputStream
. Please use
ObjectEncoder
or ObjectEncoderOutputStream
to ensure the
interoperability with this decoder.
ChannelHandler.Sharable
Constructor and Description |
---|
ObjectDecoder(ClassResolver classResolver)
Creates a new decoder whose maximum object size is
1048576
bytes. |
ObjectDecoder(int maxObjectSize,
ClassResolver classResolver)
Creates a new decoder with the specified maximum object size.
|
Modifier and Type | Method and Description |
---|---|
protected Object |
decode(ChannelHandlerContext ctx,
ByteBuf in)
Decode the from one
ByteBuf to an other. |
protected ByteBuf |
extractFrame(ByteBuf buffer,
int index,
int length)
Extract the sub-region of the specified buffer.
|
callDecode, channelInactive, channelReadSuspended, decodeLast, inboundBufferUpdated, isSingleDecode, setSingleDecode
discardInboundReadBytes, freeInboundBuffer, inboundBufferUpdated, newInboundBuffer
channelActive, channelRegistered, channelUnregistered, userEventTriggered
afterAdd, afterRemove, beforeAdd, beforeRemove, exceptionCaught
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
channelActive, channelRegistered, channelUnregistered, userEventTriggered
afterAdd, afterRemove, beforeAdd, beforeRemove, exceptionCaught
public ObjectDecoder(ClassResolver classResolver)
1048576
bytes. If the size of the received object is greater than
1048576
bytes, a StreamCorruptedException
will be
raised.classResolver
- the ClassResolver
to use for this decoderpublic ObjectDecoder(int maxObjectSize, ClassResolver classResolver)
maxObjectSize
- the maximum byte length of the serialized object.
if the length of the received object is greater
than this value, StreamCorruptedException
will be raised.classResolver
- the ClassResolver
which will load the class
of the serialized objectprotected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception
ByteToMessageDecoder
ByteBuf
to an other. This method will be called till either the input
ByteBuf
has nothing to read anymore, till nothing was read from the input ByteBuf
or till
this method returns null
.decode
in class LengthFieldBasedFrameDecoder
ctx
- the ChannelHandlerContext
which this ByteToByteDecoder
belongs toin
- the ByteBuf
from which to read dataByteBuf
was decoded, or null
if
there was not enough data left in the ByteBuf
to decode.Exception
- is thrown if an error accourprotected ByteBuf extractFrame(ByteBuf buffer, int index, int length)
LengthFieldBasedFrameDecoder
If you are sure that the frame and its content are not accessed after
the current LengthFieldBasedFrameDecoder.decode(ChannelHandlerContext, ByteBuf)
call returns, you can even avoid memory copy by returning the sliced
sub-region (i.e. return buffer.slice(index, length)).
It's often useful when you convert the extracted frame into an object.
Refer to the source code of ObjectDecoder
to see how this method
is overridden to avoid memory copy.
extractFrame
in class LengthFieldBasedFrameDecoder
Copyright © 2008-2013 The Netty Project. All Rights Reserved.