View Javadoc
1   /*
2    * Copyright 2016 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    *   https://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  import io.netty.channel.ChannelPipeline;
21  import io.netty.channel.socket.DatagramPacket;
22  import static io.netty.util.internal.ObjectUtil.checkNotNull;
23  
24  import java.util.List;
25  
26  /**
27   * A decoder that decodes the content of the received {@link DatagramPacket} using
28   * the specified {@link ByteBuf} decoder. E.g.,
29   *
30   * <pre><code>
31   * {@link ChannelPipeline} pipeline = ...;
32   * pipeline.addLast("udpDecoder", new {@link DatagramPacketDecoder}(new {@code ProtobufDecoder}(...));
33   * </code></pre>
34   */
35  public class DatagramPacketDecoder extends MessageToMessageDecoder<DatagramPacket> {
36  
37      private final MessageToMessageDecoder<ByteBuf> decoder;
38  
39      /**
40       * Create a {@link DatagramPacket} decoder using the specified {@link ByteBuf} decoder.
41       *
42       * @param decoder the specified {@link ByteBuf} decoder
43       */
44      public DatagramPacketDecoder(MessageToMessageDecoder<ByteBuf> decoder) {
45          this.decoder = checkNotNull(decoder, "decoder");
46      }
47  
48      @Override
49      public boolean acceptInboundMessage(Object msg) throws Exception {
50          if (msg instanceof DatagramPacket) {
51              return decoder.acceptInboundMessage(((DatagramPacket) msg).content());
52          }
53          return false;
54      }
55  
56      @Override
57      protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception {
58          decoder.decode(ctx, msg.content(), out);
59      }
60  
61      @Override
62      public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
63          decoder.channelRegistered(ctx);
64      }
65  
66      @Override
67      public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
68          decoder.channelUnregistered(ctx);
69      }
70  
71      @Override
72      public void channelActive(ChannelHandlerContext ctx) throws Exception {
73          decoder.channelActive(ctx);
74      }
75  
76      @Override
77      public void channelInactive(ChannelHandlerContext ctx) throws Exception {
78          decoder.channelInactive(ctx);
79      }
80  
81      @Override
82      public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
83          decoder.channelReadComplete(ctx);
84      }
85  
86      @Override
87      public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
88          decoder.userEventTriggered(ctx, evt);
89      }
90  
91      @Override
92      public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
93          decoder.channelWritabilityChanged(ctx);
94      }
95  
96      @Override
97      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
98          decoder.exceptionCaught(ctx, cause);
99      }
100 
101     @Override
102     public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
103         decoder.handlerAdded(ctx);
104     }
105 
106     @Override
107     public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
108         decoder.handlerRemoved(ctx);
109     }
110 
111     @Override
112     public boolean isSharable() {
113         return decoder.isSharable();
114     }
115 }