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    *   https://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 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      public ProtobufVarint32LengthFieldPrepender() {
44          super(ByteBuf.class);
45      }
46  
47      @Override
48      protected void encode(
49              ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
50          int bodyLen = msg.readableBytes();
51          int headerLen = computeRawVarint32Size(bodyLen);
52          out.ensureWritable(headerLen + bodyLen);
53          writeRawVarint32(out, bodyLen);
54          out.writeBytes(msg, msg.readerIndex(), bodyLen);
55      }
56  
57      /**
58       * Writes protobuf varint32 to (@link ByteBuf).
59       * @param out to be written to
60       * @param value to be written
61       */
62      static void writeRawVarint32(ByteBuf out, int value) {
63          while (true) {
64              if ((value & ~0x7F) == 0) {
65                  out.writeByte(value);
66                  return;
67              } else {
68                  out.writeByte((value & 0x7F) | 0x80);
69                  value >>>= 7;
70              }
71          }
72      }
73  
74      /**
75       * Computes size of protobuf varint32 after encoding.
76       * @param value which is to be encoded.
77       * @return size of value encoded as protobuf varint32.
78       */
79      static int computeRawVarint32Size(final int value) {
80          if ((value & (0xffffffff <<  7)) == 0) {
81              return 1;
82          }
83          if ((value & (0xffffffff << 14)) == 0) {
84              return 2;
85          }
86          if ((value & (0xffffffff << 21)) == 0) {
87              return 3;
88          }
89          if ((value & (0xffffffff << 28)) == 0) {
90              return 4;
91          }
92          return 5;
93      }
94  }