PicoLimbo/src/main/java/ru/nanit/limbo/connection/pipeline/VarIntLengthEncoder.java
2020-11-29 15:56:17 +02:00

25 lines
843 B
Java

package ru.nanit.limbo.connection.pipeline;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import ru.nanit.limbo.protocol.ByteMessage;
@ChannelHandler.Sharable
public class VarIntLengthEncoder extends MessageToByteEncoder<ByteBuf> {
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf buf, ByteBuf out) {
ByteMessage msg = new ByteMessage(out);
msg.writeVarInt(buf.readableBytes());
msg.writeBytes(buf);
}
@Override
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect) {
int anticipatedRequiredCapacity = 5 + msg.readableBytes();
return ctx.alloc().heapBuffer(anticipatedRequiredCapacity);
}
}