mirror of
https://github.com/Nan1t/NanoLimbo.git
synced 2025-07-22 16:52:29 +02:00
25 lines
843 B
Java
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);
|
|
}
|
|
}
|