public class ByteArrayEncoder extends MessageToMessageEncoder<byte[],ByteBuf>
ByteBuf
.
A typical setup for TCP/IP would be:
and then you can use an array of bytes instead of aChannelPipeline
pipeline = ...; // Decoders pipeline.addLast("frameDecoder", newLengthFieldBasedFrameDecoder
(1048576, 0, 4, 0, 4)); pipeline.addLast("bytesDecoder", newByteArrayDecoder
()); // Encoder pipeline.addLast("frameEncoder", newLengthFieldPrepender
(4)); pipeline.addLast("bytesEncoder", newByteArrayEncoder
());
ByteBuf
as a message:
void messageReceived(ChannelHandlerContext
ctx,MessageEvent
e) { byte[] bytes = (byte[]) e.getMessage(); ... }
ChannelHandler.Sharable
Constructor and Description |
---|
ByteArrayEncoder() |
Modifier and Type | Method and Description |
---|---|
ByteBuf |
encode(ChannelHandlerContext ctx,
byte[] msg) |
boolean |
isEncodable(Object msg)
Returns
true if and only if the specified message can be encoded by this encoder. |
MessageBuf<byte[]> |
newOutboundBuffer(ChannelHandlerContext ctx) |
flush
afterAdd, afterRemove, beforeAdd, beforeRemove, bind, close, connect, deregister, disconnect, exceptionCaught, userEventTriggered
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
bind, close, connect, deregister, disconnect
afterAdd, afterRemove, beforeAdd, beforeRemove, exceptionCaught, userEventTriggered
public MessageBuf<byte[]> newOutboundBuffer(ChannelHandlerContext ctx) throws Exception
newOutboundBuffer
in interface ChannelOutboundHandler
newOutboundBuffer
in interface ChannelOutboundMessageHandler<byte[]>
newOutboundBuffer
in class ChannelOutboundMessageHandlerAdapter<byte[]>
Exception
public boolean isEncodable(Object msg) throws Exception
MessageToMessageEncoder
true
if and only if the specified message can be encoded by this encoder.isEncodable
in class MessageToMessageEncoder<byte[],ByteBuf>
msg
- the messageException
public ByteBuf encode(ChannelHandlerContext ctx, byte[] msg) throws Exception
encode
in class MessageToMessageEncoder<byte[],ByteBuf>
Exception
Copyright © 2008-2012 The Netty Project. All Rights Reserved.