50 lines
		
	
	
	
		
			994 B
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			50 lines
		
	
	
	
		
			994 B
		
	
	
	
		
			Java
		
	
	
	
	
	
| package net.minecraft.network;
 | |
| 
 | |
| import io.netty.buffer.ByteBuf;
 | |
| 
 | |
| public class VarInt {
 | |
| 	private static final int MAX_VARINT_SIZE = 5;
 | |
| 	private static final int DATA_BITS_MASK = 127;
 | |
| 	private static final int CONTINUATION_BIT_MASK = 128;
 | |
| 	private static final int DATA_BITS_PER_BYTE = 7;
 | |
| 
 | |
| 	public static int getByteSize(int data) {
 | |
| 		for (int i = 1; i < 5; i++) {
 | |
| 			if ((data & -1 << i * 7) == 0) {
 | |
| 				return i;
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		return 5;
 | |
| 	}
 | |
| 
 | |
| 	public static boolean hasContinuationBit(byte data) {
 | |
| 		return (data & 128) == 128;
 | |
| 	}
 | |
| 
 | |
| 	public static int read(ByteBuf buffer) {
 | |
| 		int i = 0;
 | |
| 		int j = 0;
 | |
| 
 | |
| 		byte b;
 | |
| 		do {
 | |
| 			b = buffer.readByte();
 | |
| 			i |= (b & 127) << j++ * 7;
 | |
| 			if (j > 5) {
 | |
| 				throw new RuntimeException("VarInt too big");
 | |
| 			}
 | |
| 		} while (hasContinuationBit(b));
 | |
| 
 | |
| 		return i;
 | |
| 	}
 | |
| 
 | |
| 	public static ByteBuf write(ByteBuf buffer, int value) {
 | |
| 		while ((value & -128) != 0) {
 | |
| 			buffer.writeByte(value & 127 | 128);
 | |
| 			value >>>= 7;
 | |
| 		}
 | |
| 
 | |
| 		buffer.writeByte(value);
 | |
| 		return buffer;
 | |
| 	}
 | |
| }
 |