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