blob: a1911061f12296adb1171dc3c4ea74e3c230de5a [file] [log] [blame] [raw]
package protocolsupport.protocol.pipeline.common;
import java.util.List;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.CorruptedFrameException;
import protocolsupport.protocol.pipeline.IPacketSplitter;
import protocolsupport.utils.netty.ChannelUtils;
public class VarIntFrameDecoder implements IPacketSplitter {
private final byte[] array = new byte[3];
@Override
public void split(ChannelHandlerContext ctx, ByteBuf input, List<Object> list) throws Exception {
input.markReaderIndex();
for (int i = 0; i < array.length; ++i) {
if (!input.isReadable()) {
input.resetReaderIndex();
return;
}
array[i] = input.readByte();
if (array[i] >= 0) {
int length = ChannelUtils.readVarInt(Unpooled.wrappedBuffer(array));
if (input.readableBytes() < length) {
input.resetReaderIndex();
return;
} else {
list.add(input.readBytes(length));
return;
}
}
}
throw new CorruptedFrameException("length wider than 21-bit");
}
}