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

net.minecraft.server.PacketSplitter Maven / Gradle / Ivy

package net.minecraft.server;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.CorruptedFrameException;

import java.util.List;

public class PacketSplitter extends ByteToMessageDecoder {
	
	public PacketSplitter() {
	}
	
	protected void decode(ChannelHandlerContext channelhandlercontext, ByteBuf bytebuf, List list) throws Exception {
		bytebuf.markReaderIndex();
		byte[] abyte = new byte[3];
		
		for (int i = 0; i < abyte.length; ++i) {
			if (!bytebuf.isReadable()) {
				bytebuf.resetReaderIndex();
				return;
			}
			
			abyte[i] = bytebuf.readByte();
			if (abyte[i] >= 0) {
				PacketDataSerializer packetdataserializer = new PacketDataSerializer(Unpooled.wrappedBuffer(abyte));
				
				try {
					int j = packetdataserializer.e();
					
					if (bytebuf.readableBytes() >= j) {
						list.add(bytebuf.readBytes(j));
						return;
					}
					
					bytebuf.resetReaderIndex();
				} finally {
					packetdataserializer.release();
				}
				
				return;
			}
		}
		
		throw new CorruptedFrameException("length wider than 21-bit");
	}
}