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      @Override
47      protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
48          buffer.markReaderIndex();
49          final byte[] buf = new byte[5];
50          for (int i = 0; i < buf.length; i ++) {
51              if (!buffer.readable()) {
52                  buffer.resetReaderIndex();
53                  return null;
54              }
55  
56              buf[i] = buffer.readByte();
57              if (buf[i] >= 0) {
58                  int length = CodedInputStream.newInstance(buf, 0, i + 1).readRawVarint32();
59                  if (length < 0) {
60                      throw new CorruptedFrameException("negative length: " + length);
61                  }
62  
63                  if (buffer.readableBytes() < length) {
64                      buffer.resetReaderIndex();
65                      return null;
66                  } else {
67                      return buffer.readBytes(length);
68                  }
69              }
70          }
71  
72          // Couldn't find the byte whose MSB is off.
73          throw new CorruptedFrameException("length wider than 32-bit");
74      }
75  }