public class ByteArrayDecoder extends MessageToMessageDecoder<ByteBuf>
ByteBuf解码为字节数组。
TCP / IP的典型设置是:
ChannelPipeline pipeline = ...;
// Decoders
pipeline.addLast("frameDecoder",
new LengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4));
pipeline.addLast("bytesDecoder",
new ByteArrayDecoder());
// Encoder
pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
pipeline.addLast("bytesEncoder", new ByteArrayEncoder());
然后您可以使用一个字节数组而不是ByteBuf作为消息:
void channelRead(ChannelHandlerContext ctx, byte[] bytes) {
...
}
ChannelHandler.Sharable| Constructor and Description |
|---|
ByteArrayDecoder() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
decode(ChannelHandlerContext ctx, ByteBuf msg, java.util.List<java.lang.Object> out)
从一条消息解码到另一条消息。
|
acceptInboundMessage, channelReadchannelActive, channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggeredensureNotSharable, handlerAdded, handlerRemoved, isSharableclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waithandlerAdded, handlerRemovedprotected void decode(ChannelHandlerContext ctx, ByteBuf msg, java.util.List<java.lang.Object> out) throws java.lang.Exception
MessageToMessageDecoder
decode
MessageToMessageDecoder<ByteBuf>
ctx - MessageToMessageDecoder属于的ChannelHandlerContext
msg - 解码到另一个的消息
out - 应该添加解码消息的
List
java.lang.Exception - 发生错误时抛出
Copyright © 2008–2018 The Netty Project. All rights reserved.