diff --git a/src/main/java/org/indunet/fastproto/Decoder.java b/src/main/java/org/indunet/fastproto/Decoder.java index dc84e748..4edf70e3 100644 --- a/src/main/java/org/indunet/fastproto/Decoder.java +++ b/src/main/java/org/indunet/fastproto/Decoder.java @@ -435,162 +435,322 @@ public Decoder readInt16(int offset) { return this.put(CodecUtils.int16Type(this.bytes, offset, this.byteOrder)); } + public Decoder readInt16(int offset, Function formula) { + return this.put(formula.apply(CodecUtils.int16Type(this.bytes, offset, this.byteOrder))); + } + public Decoder readInt16(int offset, String name) { return this.put(name, CodecUtils.int16Type(this.bytes, offset, this.byteOrder)); } + public Decoder readInt16(int offset, String name, Function formula) { + return this.put(name, formula.apply(CodecUtils.int16Type(this.bytes, offset, this.byteOrder))); + } + public Decoder readInt16(int offset, ByteOrder byteOrder) { return this.put(CodecUtils.int16Type(this.bytes, offset, byteOrder)); } + public Decoder readInt16(int offset, ByteOrder byteOrder, Function formula) { + return this.put(formula.apply(CodecUtils.int16Type(this.bytes, offset, byteOrder))); + } + public Decoder readInt16(int offset, ByteOrder byteOrder, String name) { return this.put(name, CodecUtils.int16Type(this.bytes, offset, byteOrder)); } + public Decoder readInt16(int offset, ByteOrder byteOrder, String name, Function formula) { + return this.put(name, formula.apply(CodecUtils.int16Type(this.bytes, offset, byteOrder))); + } + public Decoder readInt16() { return this.put(CodecUtils.int16Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder)); } + public Decoder readInt16(Function formula) { + return this.put(formula.apply(CodecUtils.int16Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder))); + } + public Decoder readInt16(String name) { return this.put(name, CodecUtils.int16Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder)); } + public Decoder readInt16(String name, Function formula) { + return this.put(name, formula.apply(CodecUtils.int16Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder))); + } + public Decoder readInt16(ByteOrder byteOrder) { return this.put(CodecUtils.int16Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder)); } + public Decoder readInt16(ByteOrder byteOrder, Function formula) { + return this.put(formula.apply(CodecUtils.int16Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder))); + } + public Decoder readInt16(ByteOrder byteOrder, String name) { return this.put(name, CodecUtils.int16Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder)); } + public Decoder readInt16(ByteOrder byteOrder, String name, Function formula) { + return this.put(name, formula.apply(CodecUtils.int16Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder))); + } + public Decoder readInt32(int offset) { return this.put(CodecUtils.int32Type(this.bytes, offset, this.byteOrder)); } + public Decoder readInt32(int offset, Function formula) { + return this.put(formula.apply(CodecUtils.int32Type(this.bytes, offset, this.byteOrder))); + } + public Decoder readInt32(int offset, String name) { return this.put(name, CodecUtils.int32Type(this.bytes, offset, this.byteOrder)); } + public Decoder readInt32(int offset, String name, Function formula) { + return this.put(name, formula.apply(CodecUtils.int32Type(this.bytes, offset, this.byteOrder))); + } + public Decoder readInt32(int offset, ByteOrder byteOrder) { return this.put(CodecUtils.int32Type(this.bytes, offset, byteOrder)); } + public Decoder readInt32(int offset, ByteOrder byteOrder, Function formula) { + return this.put(formula.apply(CodecUtils.int32Type(this.bytes, offset, byteOrder))); + } + public Decoder readInt32(int offset, ByteOrder byteOrder, String name) { return this.put(name, CodecUtils.int32Type(this.bytes, offset, byteOrder)); } + public Decoder readInt32(int offset, ByteOrder byteOrder, String name, Function formula) { + return this.put(name, formula.apply(CodecUtils.int32Type(this.bytes, offset, byteOrder))); + } + public Decoder readInt32() { return this.put(CodecUtils.int32Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder)); } + public Decoder readInt32(Function formula) { + return this.put(formula.apply(CodecUtils.int32Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder))); + } + public Decoder readInt32(String name) { return this.put(name, CodecUtils.int32Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder)); } + public Decoder readInt32(String name, Function formula) { + return this.put(name, formula.apply(CodecUtils.int32Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder))); + } + public Decoder readInt32(ByteOrder byteOrder) { return this.put(CodecUtils.int32Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder)); } + public Decoder readInt32(ByteOrder byteOrder, Function formula) { + return this.put(formula.apply(CodecUtils.int32Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder))); + } + public Decoder readInt32(ByteOrder byteOrder, String name) { return this.put(name, CodecUtils.int32Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder)); } + public Decoder readInt32(ByteOrder byteOrder, String name, Function formula) { + return this.put(name, formula.apply(CodecUtils.int32Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder))); + } + public Decoder readInt64(int offset) { return this.put(CodecUtils.int64Type(this.bytes, offset, this.byteOrder)); } + public Decoder readInt64(int offset, Function formula) { + return this.put(formula.apply(CodecUtils.int64Type(this.bytes, offset, this.byteOrder))); + } + public Decoder readInt64(int offset, String name) { return this.put(name, CodecUtils.int64Type(this.bytes, offset, this.byteOrder)); } + public Decoder readInt64(int offset, String name, Function formula) { + return this.put(name, formula.apply(CodecUtils.int64Type(this.bytes, offset, this.byteOrder))); + } + public Decoder readInt64(int offset, ByteOrder byteOrder) { return this.put(CodecUtils.int64Type(this.bytes, offset, byteOrder)); } + public Decoder readInt64(int offset, ByteOrder byteOrder, Function formula) { + return this.put(formula.apply(CodecUtils.int64Type(this.bytes, offset, byteOrder))); + } + public Decoder readInt64(int offset, ByteOrder byteOrder, String name) { return this.put(name, CodecUtils.int64Type(this.bytes, offset, byteOrder)); } + public Decoder readInt64(int offset, ByteOrder byteOrder, String name, Function formula) { + return this.put(name, formula.apply(CodecUtils.int64Type(this.bytes, offset, byteOrder))); + } + public Decoder readInt64() { return this.put(CodecUtils.int64Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder)); } + public Decoder readInt64(Function formula) { + return this.put(formula.apply(CodecUtils.int64Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder))); + } + public Decoder readInt64(String name) { return this.put(name, CodecUtils.int64Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder)); } + public Decoder readInt64(String name, Function formula) { + return this.put(name, formula.apply(CodecUtils.int64Type(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder))); + } + public Decoder readInt64(ByteOrder byteOrder) { return this.put(CodecUtils.int64Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder)); } + public Decoder readInt64(ByteOrder byteOrder, Function formula) { + return this.put(formula.apply(CodecUtils.int64Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder))); + } + public Decoder readInt64(ByteOrder byteOrder, String name) { return this.put(name, CodecUtils.int64Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder)); } + public Decoder readInt64(ByteOrder byteOrder, String name, Function formula) { + return this.put(name, formula.apply(CodecUtils.int64Type(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder))); + } + public Decoder readFloat(int offset) { return this.put(CodecUtils.floatType(this.bytes, offset, this.byteOrder)); } + public Decoder readFloat(int offset, Function formula) { + return this.put(formula.apply(CodecUtils.floatType(this.bytes, offset, this.byteOrder))); + } + public Decoder readFloat(int offset, String name) { return this.put(name, CodecUtils.floatType(this.bytes, offset, this.byteOrder)); } + public Decoder readFloat(int offset, String name, Function formula) { + return this.put(name, formula.apply(CodecUtils.floatType(this.bytes, offset, this.byteOrder))); + } + public Decoder readFloat(int offset, ByteOrder byteOrder) { return this.put(CodecUtils.floatType(this.bytes, offset, byteOrder)); } + public Decoder readFloat(int offset, ByteOrder byteOrder, Function formula) { + return this.put(formula.apply(CodecUtils.floatType(this.bytes, offset, byteOrder))); + } + public Decoder readFloat(int offset, ByteOrder byteOrder, String name) { return this.put(name, CodecUtils.floatType(this.bytes, offset, byteOrder)); } + public Decoder readFloat(int offset, ByteOrder byteOrder, String name, Function formula) { + return this.put(name, formula.apply(CodecUtils.floatType(this.bytes, offset, byteOrder))); + } + public Decoder readFloat() { return this.put(CodecUtils.floatType(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder)); } + public Decoder readFloat(Function formula) { + return this.put(formula.apply(CodecUtils.floatType(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder))); + } + public Decoder readFloat(String name) { return this.put(name, CodecUtils.floatType(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder)); } + public Decoder readFloat(String name, Function formula) { + return this.put(name, formula.apply(CodecUtils.floatType(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder))); + } + public Decoder readFloat(ByteOrder byteOrder) { return this.put(CodecUtils.floatType(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder)); } + public Decoder readFloat(ByteOrder byteOrder, Function formula) { + return this.put(formula.apply(CodecUtils.floatType(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder))); + } + public Decoder readFloat(ByteOrder byteOrder, String name) { return this.put(name, CodecUtils.floatType(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder)); } + public Decoder readFloat(ByteOrder byteOrder, String name, Function formula) { + return this.put(name, formula.apply(CodecUtils.floatType(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder))); + } + public Decoder readDouble(int offset) { return this.put(CodecUtils.doubleType(this.bytes, offset, this.byteOrder)); } + public Decoder readDouble(int offset, Function formula) { + return this.put(formula.apply(CodecUtils.doubleType(this.bytes, offset, this.byteOrder))); + } + public Decoder readDouble(int offset, String name) { return this.put(name, CodecUtils.doubleType(this.bytes, offset, this.byteOrder)); } + public Decoder readDouble(int offset, String name, Function formula) { + return this.put(name, formula.apply(CodecUtils.doubleType(this.bytes, offset, this.byteOrder))); + } + public Decoder readDouble(int offset, ByteOrder byteOrder) { return this.put(CodecUtils.doubleType(this.bytes, offset, byteOrder)); } + public Decoder readDouble(int offset, ByteOrder byteOrder, Function formula) { + return this.put(formula.apply(CodecUtils.doubleType(this.bytes, offset, byteOrder))); + } + public Decoder readDouble(int offset, ByteOrder byteOrder, String name) { return this.put(name, CodecUtils.doubleType(this.bytes, offset, byteOrder)); } + public Decoder readDouble(int offset, ByteOrder byteOrder, String name, Function formula) { + return this.put(name, formula.apply(CodecUtils.doubleType(this.bytes, offset, byteOrder))); + } + public Decoder readDouble() { return this.put(CodecUtils.doubleType(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder)); } + public Decoder readDouble(Function formula) { + return this.put(formula.apply(CodecUtils.doubleType(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder))); + } + public Decoder readDouble(String name) { return this.put(name, CodecUtils.doubleType(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder)); } + public Decoder readDouble(String name, Function formula) { + return this.put(name, formula.apply(CodecUtils.doubleType(this.byteBuffer, this.byteBuffer.getReadIndex(), this.byteOrder))); + } + public Decoder readDouble(ByteOrder byteOrder) { return this.put(CodecUtils.doubleType(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder)); } + public Decoder readDouble(ByteOrder byteOrder, Function formula) { + return this.put(formula.apply(CodecUtils.doubleType(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder))); + } + public Decoder readDouble(ByteOrder byteOrder, String name) { return this.put(name, CodecUtils.doubleType(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder)); } + public Decoder readDouble(ByteOrder byteOrder, String name, Function formula) { + return this.put(name, formula.apply(CodecUtils.doubleType(this.byteBuffer, this.byteBuffer.getReadIndex(), byteOrder))); + } + public boolean getAsBoolean() { return (Boolean) this.last; } diff --git a/src/test/java/org/indunet/fastproto/DecoderTest.java b/src/test/java/org/indunet/fastproto/DecoderTest.java index b36389a5..dcb75613 100644 --- a/src/test/java/org/indunet/fastproto/DecoderTest.java +++ b/src/test/java/org/indunet/fastproto/DecoderTest.java @@ -85,6 +85,71 @@ public void testRead3() { assertEquals(2.71, actual.get("double64")); } + @Test + public void testRead4() { + val bytes = new byte[] { + 0x01, + 0x01, 0x02, + 0x01, + 0x01, 0x02, + 0x03, 0x04, 0x05, 0x06, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 + }; + val actual = FastProto.parse(bytes) + .defaultByteOrder(ByteOrder.BIG) + .readByte("byte1", x -> x * 0.1) + .readShort("short16", x -> x * 10) + .readInt8("int8", x -> x * 0.2) + .readInt16("int16", x -> x / 10) + .readInt32("int32", x -> x / 100.0) + .readInt64("int64", x -> x * 0.01) + .getAsMap(); + + assertEquals((byte) 0x01 * 0.1, actual.get("byte1")); + assertEquals((short) 0x0102 * 10, actual.get("short16")); + assertEquals(0x01 * 0.2, actual.get("int8")); + assertEquals(0x0102 / 10, actual.get("int16")); + assertEquals(0x03040506 / 100.0, actual.get("int32")); + assertEquals(0x0102030405060708l * 0.01, actual.get("int64")); + } + + @Test + public void testRead5() { + val bytes = new byte[] { + 0x01, + 0x01, 0x02, + 0x03, 0x04, 0x05, 0x06, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 + }; + val actual = FastProto.parse(bytes) + .defaultByteOrder(ByteOrder.LITTLE) + .readUInt8("uint8", x -> x * 0.1) + .readUInt16("uint16", x -> x * 10) + .readUInt32("uint32", x -> x / 100.0) + .readUInt64("uint64", x -> x.multiply(BigInteger.valueOf(10l))) + .getAsMap(); + + assertEquals(0x01 * 0.1, actual.get("uint8")); + assertEquals(0x0201 * 10, actual.get("uint16")); + assertEquals(0x06050403l / 100.0, actual.get("uint32")); + assertEquals(BigInteger.valueOf(0x0807060504030201l).multiply(BigInteger.valueOf(10l)), actual.get("uint64")); + } + + @Test + public void testRead6() { + val bytes = new byte[12]; + System.arraycopy(BinaryUtils.valueOf(3.14f), 0, bytes, 0, FloatType.SIZE); + System.arraycopy(BinaryUtils.valueOf(2.71), 0, bytes, 4, DoubleType.SIZE); + + val actual = FastProto.parse(bytes) + .readFloat("float32", x -> x * 0.1) + .readDouble("double64", x -> x * 10) + .getAsMap(); + + assertEquals(3.14f * 0.1, actual.get("float32")); + assertEquals(2.71 * 10, actual.get("double64")); + } + @Test public void testGetAsType() { val bytes = new byte[]{1, 2, 3, 0, 0, 0, 1, 0};