@Override public int encodeInt(int v, byte[] b, int o) { checkForSufficientLength(b, o, Bytes.SIZEOF_INT); b[o + 0] = (byte) ((v >> 24) ^ 0x80); // Flip sign bit so that INTEGER is binary comparable b[o + 1] = (byte) (v >> 16); b[o + 2] = (byte) (v >> 8); b[o + 3] = (byte) v; return Bytes.SIZEOF_INT; }
@Override public int encodeShort(short v, byte[] b, int o) { checkForSufficientLength(b, o, Bytes.SIZEOF_SHORT); b[o + 0] = (byte) ((v >> 8) ^ 0x80); // Flip sign bit so that Short is binary comparable b[o + 1] = (byte) v; return Bytes.SIZEOF_SHORT; }
@Override public int encodeByte(byte v, byte[] b, int o) { checkForSufficientLength(b, o, Bytes.SIZEOF_BYTE); b[o] = (byte) (v ^ 0x80); // Flip sign bit so that Short is binary comparable return Bytes.SIZEOF_BYTE; }
@Override public int encodeLong(long v, byte[] b, int o) { checkForSufficientLength(b, o, Bytes.SIZEOF_LONG); b[o + 0] = (byte) ((v >> 56) ^ 0x80); // Flip sign bit so that INTEGER is binary comparable b[o + 1] = (byte) (v >> 48); b[o + 2] = (byte) (v >> 40); b[o + 3] = (byte) (v >> 32); b[o + 4] = (byte) (v >> 24); b[o + 5] = (byte) (v >> 16); b[o + 6] = (byte) (v >> 8); b[o + 7] = (byte) v; return Bytes.SIZEOF_LONG; }
@Override public byte decodeByte(byte[] b, int o, SortOrder sortOrder) { Preconditions.checkNotNull(sortOrder); checkForSufficientLength(b, o, Bytes.SIZEOF_BYTE); int v; if (sortOrder == SortOrder.ASC) { v = b[o] ^ 0x80; // Flip sign bit back } else { v = b[o] ^ 0xff ^ 0x80; // Flip sign bit back } return (byte) v; }
@Override public int decodeInt(byte[] bytes, int o, SortOrder sortOrder) { Preconditions.checkNotNull(sortOrder); checkForSufficientLength(bytes, o, Bytes.SIZEOF_INT); int v; if (sortOrder == SortOrder.ASC) { v = bytes[o] ^ 0x80; // Flip sign bit back for (int i = 1; i < Bytes.SIZEOF_INT; i++) { v = (v << 8) + (bytes[o + i] & 0xff); } } else { v = bytes[o] ^ 0xff ^ 0x80; // Flip sign bit back for (int i = 1; i < Bytes.SIZEOF_INT; i++) { v = (v << 8) + ((bytes[o + i] ^ 0xff) & 0xff); } } return v; }
@Override public int encodeFloat(float v, byte[] b, int o) { checkForSufficientLength(b, o, Bytes.SIZEOF_FLOAT); int i = Float.floatToIntBits(v); i = (i ^ ((i >> Integer.SIZE - 1) | Integer.MIN_VALUE)) + 1; Bytes.putInt(b, o, i); return Bytes.SIZEOF_FLOAT; }
@Override public int encodeDouble(double v, byte[] b, int o) { checkForSufficientLength(b, o, Bytes.SIZEOF_LONG); long l = Double.doubleToLongBits(v); l = (l ^ ((l >> Long.SIZE - 1) | Long.MIN_VALUE)) + 1; Bytes.putLong(b, o, l); return Bytes.SIZEOF_LONG; }
@Override public float decodeFloat(byte[] b, int o, SortOrder sortOrder) { Preconditions.checkNotNull(sortOrder); checkForSufficientLength(b, o, Bytes.SIZEOF_INT); int value; if (sortOrder == SortOrder.DESC) { value = 0; for(int i = o; i < (o + Bytes.SIZEOF_INT); i++) { value <<= 8; value ^= (b[i] ^ 0xff) & 0xFF; } } else { value = Bytes.toInt(b, o); } value--; value ^= (~value >> Integer.SIZE - 1) | Integer.MIN_VALUE; return Float.intBitsToFloat(value); }
@Override public byte decodeByte(byte[] b, int o, SortOrder sortOrder) { Preconditions.checkNotNull(sortOrder); checkForSufficientLength(b, o, Bytes.SIZEOF_BYTE); byte v = b[o]; if (sortOrder == SortOrder.DESC) { v = SortOrder.invert(v); } if (v < 0) { throw newIllegalDataException(); } return v; }
@Override public int encodeInt(int v, byte[] b, int o) { checkForSufficientLength(b, o, Bytes.SIZEOF_INT); if (v < 0) { throw newIllegalDataException(); } Bytes.putInt(b, o, v); return Bytes.SIZEOF_INT; } }
@Override public int encodeShort(short v, byte[] b, int o) { checkForSufficientLength(b, o, Bytes.SIZEOF_SHORT); if (v < 0) { throw newIllegalDataException(); } Bytes.putShort(b, o, v); return Bytes.SIZEOF_SHORT; } }
@Override public int encodeShort(short v, byte[] b, int o) { checkForSufficientLength(b, o, Bytes.SIZEOF_BYTE); if (v < Byte.MIN_VALUE || v > Byte.MAX_VALUE) { throw newIllegalDataException( "Value " + v + " cannot be encoded as an Byte without changing its value"); } return encodeByte((byte) v, b, o); }
@Override public int encodeDouble(double v, byte[] b, int o) { checkForSufficientLength(b, o, Bytes.SIZEOF_DOUBLE); if (v < 0) { throw newIllegalDataException(); } Bytes.putDouble(b, o, v); return Bytes.SIZEOF_DOUBLE; }
@Override public int encodeFloat(float v, byte[] b, int o) { checkForSufficientLength(b, o, Bytes.SIZEOF_FLOAT); if (v < 0) { throw newIllegalDataException(); } Bytes.putFloat(b, o, v); return Bytes.SIZEOF_FLOAT; }
@Override public int encodeLong(long v, byte[] b, int o) { checkForSufficientLength(b, o, Bytes.SIZEOF_LONG); if (v < 0) { throw newIllegalDataException(); } Bytes.putLong(b, o, v); return Bytes.SIZEOF_LONG; } }
@Override public int decodeInt(byte[] b, int o, SortOrder sortOrder) { Preconditions.checkNotNull(sortOrder); checkForSufficientLength(b, o, Bytes.SIZEOF_INT); if (sortOrder == SortOrder.DESC) { b = SortOrder.invert(b, o, new byte[Bytes.SIZEOF_INT], 0, Bytes.SIZEOF_INT); o = 0; } int v = Bytes.toInt(b, o); if (v < 0) { throw newIllegalDataException(); } return v; }
@Override public short decodeShort(byte[] b, int o, SortOrder sortOrder) { Preconditions.checkNotNull(sortOrder); checkForSufficientLength(b, o, Bytes.SIZEOF_SHORT); if (sortOrder == SortOrder.DESC) { b = SortOrder.invert(b, o, new byte[Bytes.SIZEOF_SHORT], 0, Bytes.SIZEOF_SHORT); o = 0; } short v = Bytes.toShort(b, o); if (v < 0) { throw newIllegalDataException(); } return v; }
@Override public double decodeDouble(byte[] b, int o, SortOrder sortOrder) { Preconditions.checkNotNull(sortOrder); checkForSufficientLength(b, o, Bytes.SIZEOF_DOUBLE); if (sortOrder == SortOrder.DESC) { b = SortOrder.invert(b, o, new byte[Bytes.SIZEOF_DOUBLE], 0, Bytes.SIZEOF_DOUBLE); o = 0; } double v = Bytes.toDouble(b, o); if (v < 0) { throw newIllegalDataException(); } return v; } }
@Override public float decodeFloat(byte[] b, int o, SortOrder sortOrder) { Preconditions.checkNotNull(sortOrder); checkForSufficientLength(b, o, Bytes.SIZEOF_FLOAT); if (sortOrder == SortOrder.DESC) { b = SortOrder.invert(b, o, new byte[Bytes.SIZEOF_FLOAT], 0, Bytes.SIZEOF_FLOAT); o = 0; } float v = Bytes.toFloat(b, o); if (v < 0) { throw newIllegalDataException(); } return v; } }