View Javadoc
1   /*
2    * Copyright 2015 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.protobuf;
17  
18  import com.google.protobuf.CodedOutputStream;
19  import com.google.protobuf.nano.CodedOutputByteBufferNano;
20  import io.netty.buffer.ByteBuf;
21  import io.netty.channel.ChannelHandler.Sharable;
22  import io.netty.channel.ChannelHandlerContext;
23  import io.netty.handler.codec.MessageToByteEncoder;
24  
25  /**
26   * An encoder that prepends the the Google Protocol Buffers
27   * <a href="https://developers.google.com/protocol-buffers/docs/encoding?csw=1#varints">Base
28   * 128 Varints</a> integer length field. For example:
29   * <pre>
30   * BEFORE ENCODE (300 bytes)       AFTER ENCODE (302 bytes)
31   * +---------------+               +--------+---------------+
32   * | Protobuf Data |-------------->| Length | Protobuf Data |
33   * |  (300 bytes)  |               | 0xAC02 |  (300 bytes)  |
34   * +---------------+               +--------+---------------+
35   * </pre> *
36   *
37   * @see CodedOutputStream
38   * @see CodedOutputByteBufferNano
39   */
40  @Sharable
41  public class ProtobufVarint32LengthFieldPrepender extends MessageToByteEncoder<ByteBuf> {
42  
43      @Override
44      protected void encode(
45              ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
46          int bodyLen = msg.readableBytes();
47          int headerLen = computeRawVarint32Size(bodyLen);
48          out.ensureWritable(headerLen + bodyLen);
49          writeRawVarint32(out, bodyLen);
50          out.writeBytes(msg, msg.readerIndex(), bodyLen);
51      }
52  
53      /**
54       * Writes protobuf varint32 to (@link ByteBuf).
55       * @param out to be written to
56       * @param value to be written
57       */
58      static void writeRawVarint32(ByteBuf out, int value) {
59          while (true) {
60              if ((value & ~0x7F) == 0) {
61                  out.writeByte(value);
62                  return;
63              } else {
64                  out.writeByte((value & 0x7F) | 0x80);
65                  value >>>= 7;
66              }
67          }
68      }
69  
70      /**
71       * Computes size of protobuf varint32 after encoding.
72       * @param value which is to be encoded.
73       * @return size of value encoded as protobuf varint32.
74       */
75      static int computeRawVarint32Size(final int value) {
76          if ((value & (0xffffffff <<  7)) == 0) {
77              return 1;
78          }
79          if ((value & (0xffffffff << 14)) == 0) {
80              return 2;
81          }
82          if ((value & (0xffffffff << 21)) == 0) {
83              return 3;
84          }
85          if ((value & (0xffffffff << 28)) == 0) {
86              return 4;
87          }
88          return 5;
89      }
90  }