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;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.ChannelHandlerContext;
20  
21  import java.util.List;
22  
23  /**
24   * A decoder that splits the received {@link ByteBuf}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 ByteToMessageDecoder {
40  
41      private final int frameLength;
42  
43      /**
44       * Creates a new instance.
45       *
46       * @param frameLength the length of the frame
47       */
48      public FixedLengthFrameDecoder(int frameLength) {
49          if (frameLength <= 0) {
50              throw new IllegalArgumentException(
51                      "frameLength must be a positive integer: " + frameLength);
52          }
53          this.frameLength = frameLength;
54      }
55  
56      @Override
57      protected final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
58          Object decoded = decode(ctx, in);
59          if (decoded != null) {
60              out.add(decoded);
61          }
62      }
63  
64      /**
65       * Create a frame out of the {@link ByteBuf} and return it.
66       *
67       * @param   ctx             the {@link ChannelHandlerContext} which this {@link ByteToMessageDecoder} belongs to
68       * @param   in              the {@link ByteBuf} from which to read data
69       * @return  frame           the {@link ByteBuf} which represent the frame or {@code null} if no frame could
70       *                          be created.
71       */
72      protected Object decode(
73              @SuppressWarnings("UnusedParameters") ChannelHandlerContext ctx, ByteBuf in) throws Exception {
74          if (in.readableBytes() < frameLength) {
75              return null;
76          } else {
77              return in.readRetainedSlice(frameLength);
78          }
79      }
80  }