3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-03 14:50:30 +01:00

Minor improvement to var int writing

Dieser Commit ist enthalten in:
Nassim Jahnke 2022-03-26 18:12:12 +01:00
Ursprung d1699fbb8d
Commit 71944be482
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 6BE3B555EBC5982B

Datei anzeigen

@ -28,40 +28,37 @@ import io.netty.buffer.ByteBuf;
public class VarIntType extends Type<Integer> implements TypeConverter<Integer> { public class VarIntType extends Type<Integer> implements TypeConverter<Integer> {
private static final int CONTINUE_BIT = 0x80;
private static final int VALUE_BITS = 0x7F;
private static final int MULTI_BYTE_BITS = ~VALUE_BITS;
private static final int MAX_BYTES = 5;
public VarIntType() { public VarIntType() {
super("VarInt", Integer.class); super("VarInt", Integer.class);
} }
public int readPrimitive(ByteBuf buffer) { public int readPrimitive(ByteBuf buffer) {
int out = 0; int value = 0;
int bytes = 0; int bytes = 0;
byte in; byte in;
do { do {
in = buffer.readByte(); in = buffer.readByte();
value |= (in & VALUE_BITS) << (bytes++ * 7);
out |= (in & 0x7F) << (bytes++ * 7); if (bytes > MAX_BYTES) {
if (bytes > 5) { // 5 is maxBytes
throw new RuntimeException("VarInt too big"); throw new RuntimeException("VarInt too big");
} }
} while ((in & 0x80) == 0x80); } while ((in & CONTINUE_BIT) == CONTINUE_BIT);
return out; return value;
} }
public void writePrimitive(ByteBuf buffer, int object) { public void writePrimitive(ByteBuf buffer, int value) {
int part; while ((value & MULTI_BYTE_BITS) != 0) {
do { buffer.writeByte((value & VALUE_BITS) | CONTINUE_BIT);
part = object & 0x7F; value >>>= 7;
}
object >>>= 7; buffer.writeByte(value);
if (object != 0) {
part |= 0x80;
}
buffer.writeByte(part);
} while (object != 0);
} }
/** /**