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.netty5.handler.codec;
17  
18  import io.netty5.buffer.api.Buffer;
19  import io.netty5.channel.ChannelHandlerContext;
20  import io.netty5.channel.ChannelPipeline;
21  import io.netty5.channel.socket.DatagramPacket;
22  import io.netty5.handler.codec.bytes.ByteArrayDecoder;
23  
24  import static java.util.Objects.requireNonNull;
25  
26  /**
27   * A decoder that decodes the content of the received {@link DatagramPacket} using
28   * the specified {@link Buffer} decoder. E.g.,
29   *
30   * <pre><code>
31   * {@link ChannelPipeline} pipeline = ...;
32   * pipeline.addLast("udpDecoder", new {@link DatagramPacketDecoder}(new {@link ByteArrayDecoder}(...));
33   * </code></pre>
34   */
35  public class DatagramPacketDecoder extends MessageToMessageDecoder<DatagramPacket> {
36  
37      private final MessageToMessageDecoder<Buffer> decoder;
38  
39      /**
40       * Create a {@link DatagramPacket} decoder using the specified {@link Buffer} decoder.
41       *
42       * @param decoder the specified {@link Buffer} decoder
43       */
44      public DatagramPacketDecoder(MessageToMessageDecoder<Buffer> decoder) {
45          this.decoder = requireNonNull(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) throws Exception {
58          decoder.decode(ctx, msg.content());
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 channelInboundEvent(ChannelHandlerContext ctx, Object evt) throws Exception {
88          decoder.channelInboundEvent(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 channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
98          decoder.channelExceptionCaught(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 }