@Override public Byte decode(PositionedByteRange src) { byte val = src.getBytes()[src.getOffset() + src.getPosition()]; skip(src); return val; }
@Override public Float decode(PositionedByteRange src) { float val = Bytes.toFloat(src.getBytes(), src.getOffset() + src.getPosition()); skip(src); return val; }
@Override public int encode(PositionedByteRange dst, Float val) { Bytes.putFloat(dst.getBytes(), dst.getOffset() + dst.getPosition(), val); return skip(dst); }
@Override public int encode(PositionedByteRange dst, Integer val) { Bytes.putInt(dst.getBytes(), dst.getOffset() + dst.getPosition(), val); return skip(dst); }
@Override public Short decode(PositionedByteRange src) { short val = Bytes.toShort(src.getBytes(), src.getOffset() + src.getPosition()); skip(src); return val; }
@Override public int encode(PositionedByteRange dst, Long val) { Bytes.putLong(dst.getBytes(), dst.getOffset() + dst.getPosition(), val); return skip(dst); }
@Override public Integer decode(PositionedByteRange src) { int val = Bytes.toInt(src.getBytes(), src.getOffset() + src.getPosition()); skip(src); return val; }
@Override public int encode(PositionedByteRange dst, Short val) { Bytes.putShort(dst.getBytes(), dst.getOffset() + dst.getPosition(), val); return skip(dst); }
@Override public int encode(PositionedByteRange dst, Byte val) { Bytes.putByte(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; }
@Override public int encode(PositionedByteRange dst, Double val) { Bytes.putDouble(dst.getBytes(), dst.getOffset() + dst.getPosition(), val); return skip(dst); }
@Override public Long decode(PositionedByteRange src) { long val = Bytes.toLong(src.getBytes(), src.getOffset() + src.getPosition()); skip(src); return val; }
@Override public String decode(PositionedByteRange src) { if (Order.ASCENDING == this.order) { // avoid unnecessary array copy for ASC case. String val = Bytes.toString(src.getBytes(), src.getOffset() + src.getPosition(), src.getRemaining()); src.setPosition(src.getLength()); return val; } else { byte[] b = new byte[src.getRemaining()]; src.get(b); order.apply(b, 0, b.length); return Bytes.toString(b); } }
/** * Write {@code val} into {@code dst}, respecting {@code voff} and {@code vlen}. * @return number of bytes written. */ public int encode(PositionedByteRange dst, byte[] val, int voff, int vlen) { Bytes.putBytes(dst.getBytes(), dst.getOffset() + dst.getPosition(), val, voff, vlen); dst.setPosition(dst.getPosition() + vlen); return vlen; } }
@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 SimplePositionedMutableByteRange(length); src.get(b.getBytes()); return base.decode(b); }
/** * Encode an {@code int8} value using the fixed-length encoding. * @return the number of bytes written. * @see #encodeInt64(PositionedByteRange, long, Order) * @see #decodeInt8(PositionedByteRange) */ public static int encodeInt8(PositionedByteRange dst, byte val, Order ord) { final int offset = dst.getOffset(), start = dst.getPosition(); dst.put(FIXED_INT8) .put((byte) (val ^ 0x80)); ord.apply(dst.getBytes(), offset + start, 2); return 2; }
/** * Skip {@code src} over the significand bytes. * @param src The source from which to read encoded digits. * @param comp Treat encoded bytes as compliments when {@code comp} is true. * @return the number of bytes skipped. */ private static int skipSignificand(PositionedByteRange src, boolean comp) { byte[] a = src.getBytes(); final int offset = src.getOffset(), start = src.getPosition(); int i = src.getPosition(); while (((comp ? DESCENDING : ASCENDING).apply(a[offset + i++]) & 1) != 0) ; src.setPosition(i); return i - start; }
/** * Encode an {@code int16} value using the fixed-length encoding. * @return the number of bytes written. * @see #encodeInt64(PositionedByteRange, long, Order) * @see #decodeInt16(PositionedByteRange) */ public static int encodeInt16(PositionedByteRange dst, short val, Order ord) { final int offset = dst.getOffset(), start = dst.getPosition(); dst.put(FIXED_INT16) .put((byte) ((val >> 8) ^ 0x80)) .put((byte) val); ord.apply(dst.getBytes(), offset + start, 3); return 3; }
/** * Return the number of encoded entries remaining in {@code buff}. The * state of {@code buff} is not modified through use of this method. */ public static int length(PositionedByteRange buff) { PositionedByteRange b = new SimplePositionedMutableByteRange(buff.getBytes(), buff.getOffset(), buff.getLength()); b.setPosition(buff.getPosition()); int cnt = 0; for (; isEncodedValue(b); skip(b), cnt++) ; return cnt; } }
/** * Encode an {@code int32} value using the fixed-length encoding. * @return the number of bytes written. * @see #encodeInt64(PositionedByteRange, long, Order) * @see #decodeInt32(PositionedByteRange) */ public static int encodeInt32(PositionedByteRange dst, int val, Order ord) { final int offset = dst.getOffset(), start = dst.getPosition(); dst.put(FIXED_INT32) .put((byte) ((val >> 24) ^ 0x80)) .put((byte) (val >> 16)) .put((byte) (val >> 8)) .put((byte) val); ord.apply(dst.getBytes(), offset + start, 5); return 5; }