Top Description Fields Constructors Methods
io.netty.handler.codec.protobuf

public Class ProtobufDecoder

extends MessageToMessageDecoder<ByteBuf>
Class Inheritance
Annotations
@Sharable
Imports
com.google.protobuf.ExtensionRegistry, .ExtensionRegistryLite, .Message, .MessageLite, io.netty.buffer.ByteBuf, .ByteBufUtil, io.netty.channel.ChannelHandler.Sharable, .ChannelHandlerContext, .ChannelPipeline, io.netty.handler.codec.ByteToMessageDecoder, .LengthFieldBasedFrameDecoder, .LengthFieldPrepender, .MessageToMessageDecoder, io.netty.util.internal.ObjectUtil, java.util.List

Decodes a received ByteBuf into a Google Protocol Buffers Message and MessageLite. Please note that this decoder must be used with a proper ByteToMessageDecoder such as ProtobufVarint32FrameDecoder or LengthFieldBasedFrameDecoder if you are using a stream-based transport such as TCP/IP. A typical setup for TCP/IP would be:
ChannelPipeline pipeline = ...;

// Decoders
pipeline.addLast("frameDecoder",
                 new LengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4));
pipeline.addLast("protobufDecoder",
                 new ProtobufDecoder(MyMessage.getDefaultInstance()));

// Encoder
pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
pipeline.addLast("protobufEncoder", new ProtobufEncoder());
and then you can use a MyMessage instead of a ByteBuf as a message:
void channelRead(ChannelHandlerContext ctx, Object msg) {
    MyMessage req = (MyMessage) msg;
    MyMessage res = MyMessage.newBuilder().setText(
                              "Did you say '" + req.getText() + "'?").build();
    ch.write(res);
}

Field Summary

Modifier and TypeField and Description
private final ExtensionRegistryLite
private static final boolean
private final MessageLite

Constructor Summary

AccessConstructor and Description
public
ProtobufDecoder(MessageLite prototype)

Creates a new instance.

public
ProtobufDecoder(MessageLite prototype, ExtensionRegistry extensionRegistry)

public
ProtobufDecoder(MessageLite prototype, ExtensionRegistryLite extensionRegistry)

Method Summary

Modifier and TypeMethod and Description
protected void
decode(ChannelHandlerContext ctx, ByteBuf
the message to decode to an other one
msg
,
List<Object>
the List to which decoded messages should be added
out
)

Implements abstract io.netty.handler.codec.MessageToMessageDecoder.decode.

Decode from one message to an other.
Inherited from io.netty.handler.codec.MessageToMessageDecoder:
acceptInboundMessagechannelRead

Field Detail

extensionRegistryback to summary
private final ExtensionRegistryLite extensionRegistry
HAS_PARSERback to summary
private static final boolean HAS_PARSER
prototypeback to summary
private final MessageLite prototype

Constructor Detail

ProtobufDecoderback to summary
public ProtobufDecoder(MessageLite prototype)

Creates a new instance.

ProtobufDecoderback to summary
public ProtobufDecoder(MessageLite prototype, ExtensionRegistry extensionRegistry)
ProtobufDecoderback to summary
public ProtobufDecoder(MessageLite prototype, ExtensionRegistryLite extensionRegistry)

Method Detail

decodeback to summary
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception

Implements abstract io.netty.handler.codec.MessageToMessageDecoder.decode.

Doc from io.netty.handler.codec.MessageToMessageDecoder.decode.

Decode from one message to an other. This method will be called for each written message that can be handled by this decoder.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this MessageToMessageDecoder belongs to

msg:ByteBuf

the message to decode to an other one

out:List<Object>

the List to which decoded messages should be added

Annotations
@Override
Exceptions
Exception:
is thrown if an error occurs