package common.network;

import common.net.buffer.ByteBuf;
import common.net.channel.ChannelHandlerContext;
import common.net.handler.codec.MessageToByteEncoder;

public class PacketPrepender extends MessageToByteEncoder<ByteBuf> {
	protected void encode(ChannelHandlerContext context, ByteBuf buffer, ByteBuf output) throws Exception {
		int len = buffer.readableBytes();
		int vs = PacketBuffer.getVarIntSize(len);
		if(vs > 3)
			throw new IllegalArgumentException("unable to fit " + len + " into " + 3);
		PacketBuffer pbuf = new PacketBuffer(output);
		pbuf.ensureWritable(vs + len);
		pbuf.writeVarInt(len);
		pbuf.writeBuffer(buffer, buffer.readerIndex(), len);
	}
}
