@Test public void testReadWrite() { for (Order ord : new Order[] {Order.ASCENDING, Order.DESCENDING}) { RawString type = Order.ASCENDING == ord ? RawString.ASCENDING : RawString.DESCENDING; for (String val : VALUES) { PositionedByteRange buff = new SimplePositionedByteRange(Bytes.toBytes(val).length); assertEquals(buff.getLength(), type.encode(buff, val)); byte[] expected = Bytes.toBytes(val); ord.apply(expected); assertArrayEquals(expected, buff.getBytes()); buff.setPosition(0); assertEquals(val, type.decode(buff)); buff.setPosition(0); assertEquals(buff.getLength(), type.skip(buff)); assertEquals(buff.getLength(), buff.getPosition()); } } }
@Override public T decode(PositionedByteRange src) { if (src.getRemaining() < length) { throw new IllegalArgumentException( "Not enough buffer remaining. src.offset: " + src.getOffset() + " src.length: " + src.getLength() + " src.position: " + src.getPosition() + " max length: " + length); } // create a copy range limited to length bytes. boo. PositionedByteRange b = new SimplePositionedByteRange(length); src.get(b.getBytes()); return base.decode(b); }
@Override public int encode(PositionedByteRange dst, Double val) { Bytes.putDouble(dst.getBytes(), dst.getOffset() + dst.getPosition(), val); return skip(dst); }
@Override public Double decode(PositionedByteRange src) { double val = Bytes.toDouble(src.getBytes(), src.getOffset() + src.getPosition()); skip(src); return val; }