alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (DatagramPacketDecoder.java)

This example Java source code file (DatagramPacketDecoder.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

datagrampacketdecoder, exception, list, messagetomessagedecoder, object, override, throwable, util

The DatagramPacketDecoder.java Java example source code

/*
 * Copyright 2016 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package io.netty.handler.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import static io.netty.util.internal.ObjectUtil.checkNotNull;

import java.util.List;

/**
 * A decoder that decodes the content of the received {@link DatagramPacket} using
 * the specified {@link ByteBuf} decoder. E.g.,
 *
 * <pre>
 * {@link ChannelPipeline} pipeline = ...;
 * pipeline.addLast("udpDecoder", new {@link DatagramPacketDecoder}(new {@link ProtobufDecoder}(...));
 * </code>
*/ public class DatagramPacketDecoder extends MessageToMessageDecoder<DatagramPacket> { private final MessageToMessageDecoder<ByteBuf> decoder; /** * Create a {@link DatagramPacket} decoder using the specified {@link ByteBuf} decoder. * * @param decoder the specified {@link ByteBuf} decoder */ public DatagramPacketDecoder(MessageToMessageDecoder<ByteBuf> decoder) { this.decoder = checkNotNull(decoder, "decoder"); } @Override public boolean acceptInboundMessage(Object msg) throws Exception { if (msg instanceof DatagramPacket) { return decoder.acceptInboundMessage(((DatagramPacket) msg).content()); } return false; } @Override protected void decode(ChannelHandlerContext ctx, DatagramPacket msg, List<Object> out) throws Exception { decoder.decode(ctx, msg.content(), out); } @Override public void channelRegistered(ChannelHandlerContext ctx) throws Exception { decoder.channelRegistered(ctx); } @Override public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { decoder.channelUnregistered(ctx); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { decoder.channelActive(ctx); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { decoder.channelInactive(ctx); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { decoder.channelReadComplete(ctx); } @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { decoder.userEventTriggered(ctx, evt); } @Override public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception { decoder.channelWritabilityChanged(ctx); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { decoder.exceptionCaught(ctx, cause); } @Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { decoder.handlerAdded(ctx); } @Override public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { decoder.handlerRemoved(ctx); } }

Other Java examples (source code examples)

Here is a short list of links related to this Java DatagramPacketDecoder.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.