All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.netty.handler.codec.MessageToMessageDecoder Maven / Gradle / Ivy

There is a newer version: 5.0.0.Alpha2
Show newest version
/*
 * Copyright 2012 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.MessageBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundMessageHandler;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;

/**
 * {@link ChannelInboundMessageHandler} which decodes from one message to an other message
 *
 * For example here is an implementation which decodes a {@link String} to an {@link Integer} which represent
 * the length of the {@link String}.
 *
 * 
 *     public class StringToIntegerDecoder extends
 *             {@link MessageToMessageDecoder}<{@link String}> {
 *         public StringToIntegerDecoder() {
 *             super(String.class);
 *         }
 *
 *         {@code @Override}
 *         public {@link Object} decode({@link ChannelHandlerContext} ctx, {@link String} message)
 *                 throws {@link Exception} {
 *             return message.length());
 *         }
 *     }
 * 
* */ public abstract class MessageToMessageDecoder extends ChannelInboundMessageHandlerAdapter { @Override public final void messageReceived(ChannelHandlerContext ctx, I msg) throws Exception { ctx.nextInboundMessageBuffer().unfoldAndAdd(decode(ctx, msg)); } /** * Decode from one message to an other. This method will be called till either the {@link MessageBuf} has * nothing left or till this method returns {@code null}. * * @param ctx the {@link ChannelHandlerContext} which this {@link MessageToMessageDecoder} belongs to * @param msg the message to decode to an other one * @return message the decoded message or {@code null} if more messages are needed be cause the implementation * needs to do some kind of aggragation * @throws Exception is thrown if an error accour */ protected abstract Object decode(ChannelHandlerContext ctx, I msg) throws Exception; }