com.datastax.oss.driver.shaded.netty.handler.codec.DatagramPacketDecoder Maven / Gradle / Ivy
The newest version!
/*
* 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:
*
* https://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 com.datastax.oss.driver.shaded.netty.handler.codec;
import com.datastax.oss.driver.shaded.netty.buffer.ByteBuf;
import com.datastax.oss.driver.shaded.netty.channel.ChannelHandlerContext;
import com.datastax.oss.driver.shaded.netty.channel.ChannelPipeline;
import com.datastax.oss.driver.shaded.netty.channel.socket.DatagramPacket;
import com.datastax.oss.driver.shaded.netty.handler.codec.protobuf.ProtobufDecoder;
import static com.datastax.oss.driver.shaded.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.,
*
*
* {@link ChannelPipeline} pipeline = ...;
* pipeline.addLast("udpDecoder", new {@link DatagramPacketDecoder}(new {@link ProtobufDecoder}(...));
*
*/
public class DatagramPacketDecoder extends MessageToMessageDecoder {
private final MessageToMessageDecoder decoder;
/**
* Create a {@link DatagramPacket} decoder using the specified {@link ByteBuf} decoder.
*
* @param decoder the specified {@link ByteBuf} decoder
*/
public DatagramPacketDecoder(MessageToMessageDecoder 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