This guide shows how to integrate FastProto with Netty to encode/decode objects from/to ByteBuf.
Add the Netty dependency in your application (FastProto itself declares it as provided):
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.x</version>
</dependency>FastProtoDecoder<T> converts ByteBuf to a target object.FastProtoEncoder converts an object to ByteBuf.@Sharable and reused across channels.Framing (fixed length, length-field, delimiter-based, etc.) should be handled by Netty.
ch.pipeline()
.addLast(new FixedLengthFrameDecoder(60))
.addLast(new FastProtoDecoder<>(MyPojo.class))
.addLast(new FastProtoEncoder());MyPojo is a FastProto-annotated class. Example 60-byte layout:
public class MyPojo {
@Int32Type(offset = 0) int id;
@Int64Type(offset = 4) long timestamp;
@StringType(offset = 12, length = 16) String name;
@BinaryType(offset = 28, length = 32) byte[] payload;
}FastProtoDecoder<>(Type.class) accordingly.Current implementation copies ByteBuf to byte[]. A zero-copy path can be added later to read directly from ByteBuf.
Use EmbeddedChannel for unit tests:
EmbeddedChannel ch = new EmbeddedChannel(
new FixedLengthFrameDecoder(60),
new FastProtoDecoder<>(MyPojo.class),
new FastProtoEncoder()
);