diff --git a/src/com/esotericsoftware/kryo/io/ByteBufferInput.java b/src/com/esotericsoftware/kryo/io/ByteBufferInput.java index 95cd8104..f15e5d43 100644 --- a/src/com/esotericsoftware/kryo/io/ByteBufferInput.java +++ b/src/com/esotericsoftware/kryo/io/ByteBufferInput.java @@ -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); diff --git a/src/com/esotericsoftware/kryo/io/ByteBufferOutput.java b/src/com/esotericsoftware/kryo/io/ByteBufferOutput.java index 70dd32bf..e3f4fb66 100644 --- a/src/com/esotericsoftware/kryo/io/ByteBufferOutput.java +++ b/src/com/esotericsoftware/kryo/io/ByteBufferOutput.java @@ -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); diff --git a/src/com/esotericsoftware/kryo/io/Input.java b/src/com/esotericsoftware/kryo/io/Input.java index 24178e73..ec4d6af0 100644 --- a/src/com/esotericsoftware/kryo/io/Input.java +++ b/src/com/esotericsoftware/kryo/io/Input.java @@ -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); diff --git a/src/com/esotericsoftware/kryo/io/Output.java b/src/com/esotericsoftware/kryo/io/Output.java index 9cbd1eae..cfec8bb5 100644 --- a/src/com/esotericsoftware/kryo/io/Output.java +++ b/src/com/esotericsoftware/kryo/io/Output.java @@ -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) { diff --git a/test/com/esotericsoftware/kryo/io/InputOutputTest.java b/test/com/esotericsoftware/kryo/io/InputOutputTest.java index 6c5774fc..5b48be08 100644 --- a/test/com/esotericsoftware/kryo/io/InputOutputTest.java +++ b/test/com/esotericsoftware/kryo/io/InputOutputTest.java @@ -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)); @@ -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()); @@ -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); } }