cn.nukkit.utils.VarInt Java Examples

The following examples show how to use cn.nukkit.utils.VarInt. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VarIntTest.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@DisplayName("ZigZag")
@Test
void testZigZag() {
	assertAll(
			() -> assertEquals(0x2468acf0, VarInt.encodeZigZag32(0x12345678)),
			() -> assertEquals(0x2b826b1d, VarInt.encodeZigZag32(0xea3eca71)),
			() -> assertEquals(0x12345678, VarInt.decodeZigZag32(0x2468acf0)),
			() -> assertEquals(0xea3eca71, VarInt.decodeZigZag32(0x2b826b1d)),
			() -> assertEquals(2623536930346282224L, VarInt.encodeZigZag64(0x1234567812345678L)),
			() -> assertEquals(3135186066796324391L, VarInt.encodeZigZag64(0xea3eca710becececL)),
			() -> assertEquals(0x1234567812345678L, VarInt.decodeZigZag64(2623536930346282224L)),
			() -> assertEquals(0xea3eca710becececL, VarInt.decodeZigZag64(3135186066796324391L))
	);
}
 
Example #2
Source File: VarIntTest.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@DisplayName("Reading")
@Test
void testRead() {
	assertAll(
			() -> assertEquals(2412, VarInt.readUnsignedVarInt(wrapBinaryStream("EC123EC456"))),
			() -> assertEquals(583868, VarInt.readUnsignedVarInt(wrapBinaryStream("BCD123EFA0"))),
			() -> assertEquals(1206, VarInt.readVarInt(wrapBinaryStream("EC123EC456"))),
			() -> assertEquals(291934, VarInt.readVarInt(wrapBinaryStream("BCD123EFA0"))),
			() -> assertEquals(6015, VarInt.readUnsignedVarLong(wrapBinaryStream("FF2EC456EC789EC012EC"))),
			() -> assertEquals(3694, VarInt.readUnsignedVarLong(wrapBinaryStream("EE1CD34BCD56BCD78BCD"))),
			() -> assertEquals(-3008, VarInt.readVarLong(wrapBinaryStream("FF2EC456EC789EC012EC"))),
			() -> assertEquals(1847, VarInt.readVarLong(wrapBinaryStream("EE1CD34BCD56BCD78BCD")))
	);
}
 
Example #3
Source File: VarIntTest.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@DisplayName("Writing")
@Test
void testWrite() throws IOException {
	BinaryStream bs = new BinaryStream();
	VarInt.writeUnsignedVarInt(bs, 237356812);
	VarInt.writeVarInt(bs, 0xea3eca71);
	VarInt.writeUnsignedVarLong(bs, 0x1234567812345678L);
	VarInt.writeVarLong(bs, 0xea3eca710becececL);
	assertAll(
			() -> assertEquals(237356812, VarInt.readUnsignedVarInt(bs)),
			() -> assertEquals(0xea3eca71, VarInt.readVarInt(bs)),
			() -> assertEquals(0x1234567812345678L, VarInt.readUnsignedVarLong(bs)),
			() -> assertEquals(0xea3eca710becececL, VarInt.readVarLong(bs))
	);
	ByteArrayOutputStream os = new ByteArrayOutputStream();
	VarInt.writeUnsignedVarInt(os, 237356812);
	VarInt.writeVarInt(os, 0xea3eca71);
	VarInt.writeUnsignedVarLong(os, 0x1234567812345678L);
	VarInt.writeVarLong(os, 0xea3eca710becececL);
	ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
	assertAll(
			() -> assertEquals(237356812, VarInt.readUnsignedVarInt(is)),
			() -> assertEquals(0xea3eca71, VarInt.readVarInt(is)),
			() -> assertEquals(0x1234567812345678L, VarInt.readUnsignedVarLong(is)),
			() -> assertEquals(0xea3eca710becececL, VarInt.readVarLong(is))
	);
}
 
Example #4
Source File: VarIntTest.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@DisplayName("ZigZag")
@Test
void testZigZag() {
	assertAll(
			() -> assertEquals(0x2468acf0, VarInt.encodeZigZag32(0x12345678)),
			() -> assertEquals(0x2b826b1d, VarInt.encodeZigZag32(0xea3eca71)),
			() -> assertEquals(0x12345678, VarInt.decodeZigZag32(0x2468acf0)),
			() -> assertEquals(0xea3eca71, VarInt.decodeZigZag32(0x2b826b1d)),
			() -> assertEquals(2623536930346282224L, VarInt.encodeZigZag64(0x1234567812345678L)),
			() -> assertEquals(3135186066796324391L, VarInt.encodeZigZag64(0xea3eca710becececL)),
			() -> assertEquals(0x1234567812345678L, VarInt.decodeZigZag64(2623536930346282224L)),
			() -> assertEquals(0xea3eca710becececL, VarInt.decodeZigZag64(3135186066796324391L))
	);
}
 
Example #5
Source File: NBTInputStream.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String readUTF() throws IOException {
    int length = (int) (network ? VarInt.readUnsignedVarInt(stream) : this.readUnsignedShort());
    byte[] bytes = new byte[length];
    this.stream.read(bytes);
    return new String(bytes, StandardCharsets.UTF_8);
}
 
Example #6
Source File: NBTInputStream.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public long readLong() throws IOException {
    if (network) {
        return VarInt.readVarLong(this.stream);
    }
    long l = this.stream.readLong();
    if (endianness == ByteOrder.LITTLE_ENDIAN) {
        l = Long.reverseBytes(l);
    }
    return l;
}
 
Example #7
Source File: NBTInputStream.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int readInt() throws IOException {
    if (network) {
        return VarInt.readVarInt(this.stream);
    }
    int i = this.stream.readInt();
    if (endianness == ByteOrder.LITTLE_ENDIAN) {
        i = Integer.reverseBytes(i);
    }
    return i;
}
 
Example #8
Source File: NBTOutputStream.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void writeUTF(String s) throws IOException {
    byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
    if (network) {
        VarInt.writeUnsignedVarInt(stream, bytes.length);
    } else {
        this.writeShort(bytes.length);
    }
    this.stream.write(bytes);
}
 
Example #9
Source File: NBTOutputStream.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void writeLong(long v) throws IOException {
    if (network) {
        VarInt.writeVarLong(this.stream, v);
    } else {
        if (endianness == ByteOrder.LITTLE_ENDIAN) {
            v = Long.reverseBytes(v);
        }
        this.stream.writeLong(v);
    }
}
 
Example #10
Source File: NBTOutputStream.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void writeInt(int v) throws IOException {
    if (network) {
        VarInt.writeVarInt(this.stream, v);
    } else {
        if (endianness == ByteOrder.LITTLE_ENDIAN) {
            v = Integer.reverseBytes(v);
        }
        this.stream.writeInt(v);
    }
}
 
Example #11
Source File: VarIntTest.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@DisplayName("Reading")
@Test
void testRead() {
	assertAll(
			() -> assertEquals(2412, VarInt.readUnsignedVarInt(wrapBinaryStream("EC123EC456"))),
			() -> assertEquals(583868, VarInt.readUnsignedVarInt(wrapBinaryStream("BCD123EFA0"))),
			() -> assertEquals(1206, VarInt.readVarInt(wrapBinaryStream("EC123EC456"))),
			() -> assertEquals(291934, VarInt.readVarInt(wrapBinaryStream("BCD123EFA0"))),
			() -> assertEquals(6015, VarInt.readUnsignedVarLong(wrapBinaryStream("FF2EC456EC789EC012EC"))),
			() -> assertEquals(3694, VarInt.readUnsignedVarLong(wrapBinaryStream("EE1CD34BCD56BCD78BCD"))),
			() -> assertEquals(-3008, VarInt.readVarLong(wrapBinaryStream("FF2EC456EC789EC012EC"))),
			() -> assertEquals(1847, VarInt.readVarLong(wrapBinaryStream("EE1CD34BCD56BCD78BCD")))
	);
}
 
Example #12
Source File: VarIntTest.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@DisplayName("Write Sizes")
@Test
void testSizes() throws IOException {
	sizeTest(w -> VarInt.writeVarLong(w, 0x7FFFFFFF /* -1 >>> 1 */), 5);
	sizeTest(w -> VarInt.writeVarInt(w, 0x7FFFFFFF /* -1 >>> 1 */), 5);
	sizeTest(w -> VarInt.writeVarLong(w, -1), 1);
	sizeTest(w -> VarInt.writeVarInt(w, -1), 1);
}
 
Example #13
Source File: VarIntTest.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@DisplayName("Writing")
@Test
void testWrite() throws IOException {
	BinaryStream bs = new BinaryStream();
	VarInt.writeUnsignedVarInt(bs, 237356812);
	VarInt.writeVarInt(bs, 0xea3eca71);
	VarInt.writeUnsignedVarLong(bs, 0x1234567812345678L);
	VarInt.writeVarLong(bs, 0xea3eca710becececL);
	assertAll(
			() -> assertEquals(237356812, VarInt.readUnsignedVarInt(bs)),
			() -> assertEquals(0xea3eca71, VarInt.readVarInt(bs)),
			() -> assertEquals(0x1234567812345678L, VarInt.readUnsignedVarLong(bs)),
			() -> assertEquals(0xea3eca710becececL, VarInt.readVarLong(bs))
	);
	ByteArrayOutputStream os = new ByteArrayOutputStream();
	VarInt.writeUnsignedVarInt(os, 237356812);
	VarInt.writeVarInt(os, 0xea3eca71);
	VarInt.writeUnsignedVarLong(os, 0x1234567812345678L);
	VarInt.writeVarLong(os, 0xea3eca710becececL);
	VarInt.writeVarInt(os, 0x7FFFFFFF);
	VarInt.writeVarInt(os, -1);
	ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
	assertAll(
			() -> assertEquals(237356812, VarInt.readUnsignedVarInt(is)),
			() -> assertEquals(0xea3eca71, VarInt.readVarInt(is)),
			() -> assertEquals(0x1234567812345678L, VarInt.readUnsignedVarLong(is)),
			() -> assertEquals(0xea3eca710becececL, VarInt.readVarLong(is)),
			() -> assertEquals(0x7FFFFFFF, VarInt.readVarInt(is)),
			() -> assertEquals(-1, VarInt.readVarInt(is))
	);
}
 
Example #14
Source File: NBTOutputStream.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void writeInt(int v) throws IOException {
    if (network) {
        VarInt.writeVarInt(this.stream, v);
    } else {
        if (endianness == ByteOrder.LITTLE_ENDIAN) {
            v = Integer.reverseBytes(v);
        }
        this.stream.writeInt(v);
    }
}
 
Example #15
Source File: Network.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
private DataPacket getPacketFromBuffer(byte[] buffer) throws IOException {
    ByteArrayInputStream stream = new ByteArrayInputStream(buffer);
    DataPacket pk = this.getPacket((byte) VarInt.readUnsignedVarInt(stream));
    if (pk != null) {
        pk.setBuffer(buffer, buffer.length - stream.available());
    }
    return pk;
}
 
Example #16
Source File: NBTInputStream.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String readUTF() throws IOException {
    int length = network ? (int) VarInt.readUnsignedVarInt(stream) : this.readUnsignedShort();
    byte[] bytes = new byte[length];
    this.stream.read(bytes);
    return new String(bytes, StandardCharsets.UTF_8);
}
 
Example #17
Source File: NBTInputStream.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public long readLong() throws IOException {
    if (network) {
        return VarInt.readVarLong(this.stream);
    }
    long l = this.stream.readLong();
    if (endianness == ByteOrder.LITTLE_ENDIAN) {
        l = Long.reverseBytes(l);
    }
    return l;
}
 
Example #18
Source File: NBTInputStream.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int readInt() throws IOException {
    if (network) {
        return VarInt.readVarInt(this.stream);
    }
    int i = this.stream.readInt();
    if (endianness == ByteOrder.LITTLE_ENDIAN) {
        i = Integer.reverseBytes(i);
    }
    return i;
}
 
Example #19
Source File: NBTOutputStream.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void writeUTF(String s) throws IOException {
    byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
    if (network) {
        VarInt.writeUnsignedVarInt(stream, bytes.length);
    } else {
        this.writeShort(bytes.length);
    }
    this.stream.write(bytes);
}
 
Example #20
Source File: NBTOutputStream.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void writeLong(long v) throws IOException {
    if (network) {
        VarInt.writeVarLong(this.stream, v);
    } else {
        if (endianness == ByteOrder.LITTLE_ENDIAN) {
            v = Long.reverseBytes(v);
        }
        this.stream.writeLong(v);
    }
}
 
Example #21
Source File: NBTOutputStream.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void writeInt(int v) throws IOException {
    if (network) {
        VarInt.writeVarInt(this.stream, v);
    } else {
        if (endianness == ByteOrder.LITTLE_ENDIAN) {
            v = Integer.reverseBytes(v);
        }
        this.stream.writeInt(v);
    }
}
 
Example #22
Source File: NBTInputStream.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String readUTF() throws IOException {
    int length = (int) (network ? VarInt.readUnsignedVarInt(stream) : this.readUnsignedShort());
    byte[] bytes = new byte[length];
    this.stream.read(bytes);
    return new String(bytes, StandardCharsets.UTF_8);
}
 
Example #23
Source File: NBTInputStream.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public long readLong() throws IOException {
    if (network) {
        return VarInt.readVarLong(this.stream);
    }
    long l = this.stream.readLong();
    if (endianness == ByteOrder.LITTLE_ENDIAN) {
        l = Long.reverseBytes(l);
    }
    return l;
}
 
Example #24
Source File: NBTInputStream.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int readInt() throws IOException {
    if (network) {
        return VarInt.readVarInt(this.stream);
    }
    int i = this.stream.readInt();
    if (endianness == ByteOrder.LITTLE_ENDIAN) {
        i = Integer.reverseBytes(i);
    }
    return i;
}
 
Example #25
Source File: NBTOutputStream.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void writeUTF(String s) throws IOException {
    byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
    if (network) {
        VarInt.writeUnsignedVarInt(stream, bytes.length);
    } else {
        this.writeShort(bytes.length);
    }
    this.stream.write(bytes);
}
 
Example #26
Source File: NBTOutputStream.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void writeLong(long v) throws IOException {
    if (network) {
        VarInt.writeVarLong(this.stream, v);
    } else {
        if (endianness == ByteOrder.LITTLE_ENDIAN) {
            v = Long.reverseBytes(v);
        }
        this.stream.writeLong(v);
    }
}