View Javadoc

1   /*
2    * Copyright 2012 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 io.netty.buffer.ByteBuf;
19  import io.netty.channel.ChannelHandlerContext;
20  import io.netty.handler.codec.ByteToMessageDecoder;
21  import io.netty.handler.codec.CorruptedFrameException;
22  
23  import java.util.List;
24  
25  import com.google.protobuf.CodedInputStream;
26  
27  /**
28   * A decoder that splits the received {@link ByteBuf}s dynamically by the
29   * value of the Google Protocol Buffers
30   * <a href="https://developers.google.com/protocol-buffers/docs/encoding#varints">Base
31   * 128 Varints</a> integer length field in the message.  For example:
32   * <pre>
33   * BEFORE DECODE (302 bytes)       AFTER DECODE (300 bytes)
34   * +--------+---------------+      +---------------+
35   * | Length | Protobuf Data |----->| Protobuf Data |
36   * | 0xAC02 |  (300 bytes)  |      |  (300 bytes)  |
37   * +--------+---------------+      +---------------+
38   * </pre>
39   *
40   * @see CodedInputStream
41   */
42  public class ProtobufVarint32FrameDecoder extends ByteToMessageDecoder {
43  
44      // TODO maxFrameLength + safe skip + fail-fast option
45      //      (just like LengthFieldBasedFrameDecoder)
46  
47      @Override
48      protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
49          in.markReaderIndex();
50          final byte[] buf = new byte[5];
51          for (int i = 0; i < buf.length; i ++) {
52              if (!in.isReadable()) {
53                  in.resetReaderIndex();
54                  return;
55              }
56  
57              buf[i] = in.readByte();
58              if (buf[i] >= 0) {
59                  int length = CodedInputStream.newInstance(buf, 0, i + 1).readRawVarint32();
60                  if (length < 0) {
61                      throw new CorruptedFrameException("negative length: " + length);
62                  }
63  
64                  if (in.readableBytes() < length) {
65                      in.resetReaderIndex();
66                      return;
67                  } else {
68                      out.add(in.readSlice(length).retain());
69                      return;
70                  }
71              }
72          }
73  
74          // Couldn't find the byte whose MSB is off.
75          throw new CorruptedFrameException("length wider than 32-bit");
76      }
77  }