예제 #1
0
 @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());
     }
   }
 }
예제 #2
0
 @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);
 }
예제 #3
0
 @Override
 public int encode(PositionedByteRange dst, T val) {
   if (dst.getRemaining() < length) {
     throw new IllegalArgumentException(
         "Not enough buffer remaining. dst.offset: "
             + dst.getOffset()
             + " dst.length: "
             + dst.getLength()
             + " dst.position: "
             + dst.getPosition()
             + " max length: "
             + length);
   }
   int written = base.encode(dst, val);
   if (written > length) {
     throw new IllegalArgumentException(
         "Length of encoded value (" + written + ") exceeds max length (" + length + ").");
   }
   // TODO: is the zero-padding appropriate?
   for (; written < length; written++) {
     dst.put((byte) 0x00);
   }
   return written;
 }