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.frame;
17  
18  import org.jboss.netty.buffer.ChannelBuffer;
19  import org.jboss.netty.buffer.ChannelBufferFactory;
20  import org.jboss.netty.channel.Channel;
21  import org.jboss.netty.channel.ChannelHandlerContext;
22  
23  /**
24   * A decoder that splits the received {@link ChannelBuffer}s by the fixed number
25   * of bytes. For example, if you received the following four fragmented packets:
26   * <pre>
27   * +---+----+------+----+
28   * | A | BC | DEFG | HI |
29   * +---+----+------+----+
30   * </pre>
31   * A {@link FixedLengthFrameDecoder}{@code (3)} will decode them into the
32   * following three packets with the fixed length:
33   * <pre>
34   * +-----+-----+-----+
35   * | ABC | DEF | GHI |
36   * +-----+-----+-----+
37   * </pre>
38   */
39  public class FixedLengthFrameDecoder extends FrameDecoder {
40  
41      private final int frameLength;
42      private final boolean allocateFullBuffer;
43  
44      /**
45       * Calls {@link #FixedLengthFrameDecoder(int, boolean)} with {@code false}
46       */
47      public FixedLengthFrameDecoder(int frameLength) {
48          this(frameLength, false);
49      }
50  
51      /**
52       * Creates a new instance.
53       *
54       * @param frameLength
55       *        the length of the frame
56       * @param allocateFullBuffer
57       *        {@code true} if the cumulative {@link ChannelBuffer} should use the
58       *        {@link #frameLength} as its initial size
59       */
60      public FixedLengthFrameDecoder(int frameLength, boolean allocateFullBuffer) {
61          if (frameLength <= 0) {
62              throw new IllegalArgumentException(
63                      "frameLength must be a positive integer: " + frameLength);
64          }
65          this.frameLength = frameLength;
66          this.allocateFullBuffer = allocateFullBuffer;
67      }
68  
69      @Override
70      protected Object decode(
71              ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
72          if (buffer.readableBytes() < frameLength) {
73              return null;
74          } else {
75              ChannelBuffer frame = extractFrame(buffer, buffer.readerIndex(), frameLength);
76              buffer.skipBytes(frameLength);
77              return frame;
78          }
79      }
80  
81      @Override
82      protected ChannelBuffer newCumulationBuffer(ChannelHandlerContext ctx, int minimumCapacity) {
83          ChannelBufferFactory factory = ctx.getChannel().getConfig().getBufferFactory();
84          if (allocateFullBuffer) {
85              return factory.getBuffer(frameLength);
86          }
87          return super.newCumulationBuffer(ctx, minimumCapacity);
88      }
89  }