Skip to content

Commit

Permalink
Add to Input/Output methods for writing and reading bytes of an int.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wojtek Gdela committed Oct 17, 2023
1 parent 029190b commit 9698899
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/com/esotericsoftware/kryo/io/ByteBufferInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,17 @@ public void readBytes (byte[] bytes, int offset, int count) throws KryoException
}
}

public int readInt (int count) {
if (count < 0 || count > 4) throw new IllegalArgumentException("count must be >= 0 and <= 4: " + count);
require(count);
position += count;
int bytes = byteBuffer.get();
for (int i = 1; i < count; i++) {
bytes = (bytes << 8) | (byteBuffer.get() & 0xFF);
}
return bytes;
}

public long readLong (int count) {
if (count < 0 || count > 8) throw new IllegalArgumentException("count must be >= 0 and <= 8: " + count);
require(count);
Expand Down
9 changes: 9 additions & 0 deletions src/com/esotericsoftware/kryo/io/ByteBufferOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,15 @@ public void writeBytes (byte[] bytes, int offset, int count) throws KryoExceptio
}
}

public void writeInt (int bytes, int count) {
if (count < 0 || count > 4) throw new IllegalArgumentException("count must be >= 0 and <= 4: " + count);
require(count);
position += count;
for (int i = count - 1; i >= 0; i--) {
byteBuffer.put((byte) (bytes >> (i << 3)));
}
}

public void writeLong (long bytes, int count) {
if (count < 0 || count > 8) throw new IllegalArgumentException("count must be >= 0 and <= 8: " + count);
require(count);
Expand Down
13 changes: 13 additions & 0 deletions src/com/esotericsoftware/kryo/io/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,19 @@ public void readBytes (byte[] bytes, int offset, int count) throws KryoException
}
}

/** Reads count bytes and returns them as int, the last byte read will be the lowest byte in the int. */
public int readInt (int count) {
if (count < 0 || count > 4) throw new IllegalArgumentException("count must be >= 0 and <= 4: " + count);
require(count);
int p = position;
position = p + count;
int bytes = buffer[p++];
for (int i = 1; i < count; i++) {
bytes = (bytes << 8) | (buffer[p++] & 0xFF);
}
return bytes;
}

/** Reads count bytes and returns them as long, the last byte read will be the lowest byte in the long. */
public long readLong (int count) {
if (count < 0 || count > 8) throw new IllegalArgumentException("count must be >= 0 and <= 8: " + count);
Expand Down
12 changes: 12 additions & 0 deletions src/com/esotericsoftware/kryo/io/Output.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,18 @@ public void writeBytes (byte[] bytes, int offset, int count) throws KryoExceptio
}
}

/** Writes count bytes from long, the last byte written is the lowest byte from the long.
* Note the number of bytes is not written. */
public void writeInt (int bytes, int count) {
if (count < 0 || count > 4) throw new IllegalArgumentException("count must be >= 0 and <= 4: " + count);
require(count);
int p = position;
position = p + count;
for (int i = count - 1; i >= 0; i--) {
buffer[p++] = (byte) (bytes >> (i << 3));
}
}

/** Writes count bytes from long, the last byte written is the lowest byte from the long.
* Note the number of bytes is not written. */
public void writeLong (long bytes, int count) {
Expand Down
42 changes: 42 additions & 0 deletions test/com/esotericsoftware/kryo/io/InputOutputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,25 @@ private void runIntTest (Output write) throws IOException {
write.writeInt(-268435455);
write.writeInt(-134217728);
write.writeInt(-268435456);
write.writeInt(0, 1);
write.writeInt(63, 1);
write.writeInt(64, 1);
write.writeInt(127, 1);
write.writeInt(128, 2);
write.writeInt(8192, 2);
write.writeInt(16384, 2);
write.writeInt(2097151, 3);
write.writeInt(1048575, 3);
write.writeInt(134217727, 4);
write.writeInt(268435455, 4);
write.writeInt(134217728, 4);
write.writeInt(268435456, 4);
write.writeInt(-2097151, 3);
write.writeInt(-1048575, 3);
write.writeInt(-134217727, 4);
write.writeInt(-268435455, 4);
write.writeInt(-134217728, 4);
write.writeInt(-268435456, 4);
assertEquals(1, write.writeVarInt(0, true));
assertEquals(1, write.writeVarInt(0, false));
assertEquals(1, write.writeVarInt(63, true));
Expand Down Expand Up @@ -417,6 +436,25 @@ private void runIntTest (Output write) throws IOException {
assertEquals(-268435455, read.readInt());
assertEquals(-134217728, read.readInt());
assertEquals(-268435456, read.readInt());
assertEquals(0, read.readInt(1));
assertEquals(63, read.readInt(1));
assertEquals(64, read.readInt(1));
assertEquals(127, read.readInt(1));
assertEquals(128, read.readInt(2));
assertEquals(8192, read.readInt(2));
assertEquals(16384, read.readInt(2));
assertEquals(2097151, read.readInt(3));
assertEquals(1048575, read.readInt(3));
assertEquals(134217727, read.readInt(4));
assertEquals(268435455, read.readInt(4));
assertEquals(134217728, read.readInt(4));
assertEquals(268435456, read.readInt(4));
assertEquals(-2097151, read.readInt(3));
assertEquals(-1048575, read.readInt(3));
assertEquals(-134217727, read.readInt(4));
assertEquals(-268435455, read.readInt(4));
assertEquals(-134217728, read.readInt(4));
assertEquals(-268435456, read.readInt(4));
assertTrue(read.canReadVarInt());
assertTrue(read.canReadVarInt());
assertTrue(read.canReadVarInt());
Expand Down Expand Up @@ -477,11 +515,15 @@ private void runIntTest (Output write) throws IOException {
write.writeInt(value);
write.writeVarInt(value, true);
write.writeVarInt(value, false);
int numOfBytes = (i % 4) + 1;
write.writeInt(value, numOfBytes);

read.setBuffer(write.toBytes());
assertEquals(value, read.readInt());
assertEquals(value, read.readVarInt(true));
assertEquals(value, read.readVarInt(false));
int numOfBytesMask = numOfBytes == 4 ? -1 : (1 << numOfBytes * 8) - 1;
assertEquals(value & numOfBytesMask, read.readInt(numOfBytes) & numOfBytesMask);
}
}

Expand Down

0 comments on commit 9698899

Please sign in to comment.