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

net.dongliu.prettypb.rpc.coder.ProtobufEncoder Maven / Gradle / Ivy

There is a newer version: 0.3.5
Show newest version
package net.dongliu.prettypb.rpc.coder;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import net.dongliu.prettypb.runtime.ProtobufSerializer;

import java.util.List;

import static io.netty.buffer.Unpooled.wrappedBuffer;

/**
 * Encodes the requested protobuf message
 *
 * @author dongliu
 */
public class ProtobufEncoder extends MessageToMessageEncoder {

    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, List out)
            throws Exception {
        out.add(wrappedBuffer(ProtobufSerializer.toBytes(msg, (Class) msg.getClass())));
    }
}