ProtobufVarint32FrameDecoder Class — netty Architecture
Architecture documentation for the ProtobufVarint32FrameDecoder class in ProtobufVarint32FrameDecoder.java from the netty codebase.
Entity Profile
Dependency Diagram
graph TD f7621757_c495_a067_dbd2_54254d637829["ProtobufVarint32FrameDecoder"] cb2dade4_e776_d050_de35_06efd0c26254["ProtobufVarint32FrameDecoder.java"] f7621757_c495_a067_dbd2_54254d637829 -->|defined in| cb2dade4_e776_d050_de35_06efd0c26254 c1590bf2_98a5_65af_00fb_ead73b9ba31d["decode()"] f7621757_c495_a067_dbd2_54254d637829 -->|method| c1590bf2_98a5_65af_00fb_ead73b9ba31d bf8c8c02_e669_17c5_7fe4_cb9b825ddb69["readRawVarint32()"] f7621757_c495_a067_dbd2_54254d637829 -->|method| bf8c8c02_e669_17c5_7fe4_cb9b825ddb69 b8dac422_5bdd_baf1_49bd_e0bbf8d399fa["readRawVarint40()"] f7621757_c495_a067_dbd2_54254d637829 -->|method| b8dac422_5bdd_baf1_49bd_e0bbf8d399fa e9cf2283_6346_e220_ecc4_5e58d76b3f5a["readRawVarint24()"] f7621757_c495_a067_dbd2_54254d637829 -->|method| e9cf2283_6346_e220_ecc4_5e58d76b3f5a
Relationship Graph
Source Code
codec-protobuf/src/main/java/io/netty/handler/codec/protobuf/ProtobufVarint32FrameDecoder.java lines 43–142
public class ProtobufVarint32FrameDecoder extends ByteToMessageDecoder {
// TODO maxFrameLength + safe skip + fail-fast option
// (just like LengthFieldBasedFrameDecoder)
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
throws Exception {
in.markReaderIndex();
int preIndex = in.readerIndex();
int length = readRawVarint32(in);
if (preIndex == in.readerIndex()) {
return;
}
if (length < 0) {
throw new CorruptedFrameException("negative length: " + length);
}
if (in.readableBytes() < length) {
in.resetReaderIndex();
} else {
out.add(in.readRetainedSlice(length));
}
}
/**
* Reads variable length 32bit int from buffer
*
* @return decoded int if buffers readerIndex has been forwarded else nonsense value
*/
static int readRawVarint32(ByteBuf buffer) {
if (buffer.readableBytes() < 4) {
return readRawVarint24(buffer);
}
int wholeOrMore = buffer.getIntLE(buffer.readerIndex());
int firstOneOnStop = ~wholeOrMore & 0x80808080;
if (firstOneOnStop == 0) {
return readRawVarint40(buffer, wholeOrMore);
}
int bitsToKeep = Integer.numberOfTrailingZeros(firstOneOnStop) + 1;
buffer.skipBytes(bitsToKeep >> 3);
int thisVarintMask = firstOneOnStop ^ (firstOneOnStop - 1);
int wholeWithContinuations = wholeOrMore & thisVarintMask;
// mix them up as per varint spec while dropping the continuation bits:
// 0x7F007F isolate the first byte and the third byte dropping the continuation bits
// 0x7F007F00 isolate the second byte and the fourth byte dropping the continuation bits
// the second and fourth byte are shifted to the right by 1, filling the gaps left by the first and third byte
// it means that the first and second bytes now occupy the first 14 bits (7 bits each)
// and the third and fourth bytes occupy the next 14 bits (7 bits each), with a gap between the 2s of 2 bytes
// and another gap of 2 bytes after the forth and third.
wholeWithContinuations = (wholeWithContinuations & 0x7F007F) | ((wholeWithContinuations & 0x7F007F00) >> 1);
// 0x3FFF isolate the first 14 bits i.e. the first and second bytes
// 0x3FFF0000 isolate the next 14 bits i.e. the third and forth bytes
// the third and forth bytes are shifted to the right by 2, filling the gaps left by the first and second bytes
return (wholeWithContinuations & 0x3FFF) | ((wholeWithContinuations & 0x3FFF0000) >> 2);
}
private static int readRawVarint40(ByteBuf buffer, int wholeOrMore) {
byte lastByte;
if (buffer.readableBytes() == 4 || (lastByte = buffer.getByte(buffer.readerIndex() + 4)) < 0) {
throw new CorruptedFrameException("malformed varint.");
}
buffer.skipBytes(5);
// add it to wholeOrMore
return wholeOrMore & 0x7F |
(((wholeOrMore >> 8) & 0x7F) << 7) |
(((wholeOrMore >> 16) & 0x7F) << 14) |
(((wholeOrMore >> 24) & 0x7F) << 21) |
(lastByte << 28);
}
private static int readRawVarint24(ByteBuf buffer) {
if (!buffer.isReadable()) {
return 0;
}
buffer.markReaderIndex();
byte tmp = buffer.readByte();
if (tmp >= 0) {
return tmp;
}
Defined In
Source
Frequently Asked Questions
What is the ProtobufVarint32FrameDecoder class?
ProtobufVarint32FrameDecoder is a class in the netty codebase, defined in codec-protobuf/src/main/java/io/netty/handler/codec/protobuf/ProtobufVarint32FrameDecoder.java.
Where is ProtobufVarint32FrameDecoder defined?
ProtobufVarint32FrameDecoder is defined in codec-protobuf/src/main/java/io/netty/handler/codec/protobuf/ProtobufVarint32FrameDecoder.java at line 43.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free