/** * Encode a null value. * @param dst The destination to which encoded digits are written. * @param ord The {@link Order} to respect while encoding {@code val}. * @return the number of bytes written. */ public static int encodeNull(PositionedByteRange dst, Order ord) { dst.put(ord.apply(NULL)); return 1; }
/** * Create a terminated version of the {@code wrapped}. * @throws IllegalArgumentException when {@code term} is null or empty. */ public TerminatedWrapper(DataType<T> wrapped, byte[] term) { if (null == term || term.length == 0) { throw new IllegalArgumentException("terminator must be non-null and non-empty."); } this.wrapped = wrapped; wrapped.getOrder().apply(term); this.term = term; }
/** * Decode an {@code int8} value. * @see #encodeInt8(PositionedByteRange, byte, Order) */ public static byte decodeInt8(PositionedByteRange src) { final byte header = src.get(); assert header == FIXED_INT8 || header == DESCENDING.apply(FIXED_INT8); Order ord = header == FIXED_INT8 ? ASCENDING : DESCENDING; return (byte)((ord.apply(src.get()) ^ 0x80) & 0xff); }
@Override public int encode(PositionedByteRange dst, String val) { byte[] s = Bytes.toBytes(val); order.apply(s); dst.put(s); return s.length; } }
/** * Decode an {@code int16} value. * @see #encodeInt16(PositionedByteRange, short, Order) */ public static short decodeInt16(PositionedByteRange src) { final byte header = src.get(); assert header == FIXED_INT16 || header == DESCENDING.apply(FIXED_INT16); Order ord = header == FIXED_INT16 ? ASCENDING : DESCENDING; short val = (short) ((ord.apply(src.get()) ^ 0x80) & 0xff); val = (short) ((val << 8) + (ord.apply(src.get()) & 0xff)); return val; }
/** * Decode an {@code int32} value. * @see #encodeInt32(PositionedByteRange, int, Order) */ public static int decodeInt32(PositionedByteRange src) { final byte header = src.get(); assert header == FIXED_INT32 || header == DESCENDING.apply(FIXED_INT32); Order ord = header == FIXED_INT32 ? ASCENDING : DESCENDING; int val = (ord.apply(src.get()) ^ 0x80) & 0xff; for (int i = 1; i < 4; i++) { val = (val << 8) + (ord.apply(src.get()) & 0xff); } return val; }
/** * Decode an {@code int64} value. * @see #encodeInt64(PositionedByteRange, long, Order) */ public static long decodeInt64(PositionedByteRange src) { final byte header = src.get(); assert header == FIXED_INT64 || header == DESCENDING.apply(FIXED_INT64); Order ord = header == FIXED_INT64 ? ASCENDING : DESCENDING; long val = (ord.apply(src.get()) ^ 0x80) & 0xff; for (int i = 1; i < 8; i++) { val = (val << 8) + (ord.apply(src.get()) & 0xff); } return val; }
/** * Decode a 64-bit floating point value using the fixed-length encoding. * @see #encodeFloat64(PositionedByteRange, double, Order) */ public static double decodeFloat64(PositionedByteRange src) { final byte header = src.get(); assert header == FIXED_FLOAT64 || header == DESCENDING.apply(FIXED_FLOAT64); Order ord = header == FIXED_FLOAT64 ? ASCENDING : DESCENDING; long val = ord.apply(src.get()) & 0xff; for (int i = 1; i < 8; i++) { val = (val << 8) + (ord.apply(src.get()) & 0xff); } val ^= (~val >> (Long.SIZE - 1)) | Long.MIN_VALUE; return Double.longBitsToDouble(val); }
/** * Decode a 32-bit floating point value using the fixed-length encoding. * @see #encodeFloat32(PositionedByteRange, float, Order) */ public static float decodeFloat32(PositionedByteRange src) { final byte header = src.get(); assert header == FIXED_FLOAT32 || header == DESCENDING.apply(FIXED_FLOAT32); Order ord = header == FIXED_FLOAT32 ? ASCENDING : DESCENDING; int val = ord.apply(src.get()) & 0xff; for (int i = 1; i < 4; i++) { val = (val << 8) + (ord.apply(src.get()) & 0xff); } val ^= (~val >> (Integer.SIZE - 1)) | Integer.MIN_VALUE; return Float.intBitsToFloat(val); }
/** * Return true when the next encoded value in {@code src} uses Numeric * encoding and is {@code NaN}, false otherwise. */ public static boolean isNumericNaN(PositionedByteRange src) { return NAN == (-1 == Integer.signum(src.peek()) ? DESCENDING : ASCENDING).apply(src.peek()); }
/** * Return true when the next encoded value in {@code src} uses fixed-width * Int32 encoding, false otherwise. */ public static boolean isFixedInt32(PositionedByteRange src) { return FIXED_INT32 == (-1 == Integer.signum(src.peek()) ? DESCENDING : ASCENDING).apply(src.peek()); }
/** * Return true when the next encoded value in {@code src} uses BlobVar * encoding, false otherwise. */ public static boolean isBlobVar(PositionedByteRange src) { return BLOB_VAR == (-1 == Integer.signum(src.peek()) ? DESCENDING : ASCENDING).apply(src.peek()); }
/** * Return true when the next encoded value in {@code src} uses fixed-width * Int64 encoding, false otherwise. */ public static boolean isFixedInt64(PositionedByteRange src) { return FIXED_INT64 == (-1 == Integer.signum(src.peek()) ? DESCENDING : ASCENDING).apply(src.peek()); }
/** * Return true when the next encoded value in {@code src} uses Numeric * encoding, false otherwise. {@code NaN}, {@code +/-Inf} are valid Numeric * values. */ public static boolean isNumeric(PositionedByteRange src) { byte x = (-1 == Integer.signum(src.peek()) ? DESCENDING : ASCENDING).apply(src.peek()); return x >= NEG_INF && x <= NAN; }
/** * Return true when the next encoded value in {@code src} uses Numeric * encoding and is {@code Infinite}, false otherwise. */ public static boolean isNumericInfinite(PositionedByteRange src) { byte x = (-1 == Integer.signum(src.peek()) ? DESCENDING : ASCENDING).apply(src.peek()); return NEG_INF == x || POS_INF == x; }
/** * Return true when the next encoded value in {@code src} uses fixed-width * Int8 encoding, false otherwise. */ public static boolean isFixedInt8(PositionedByteRange src) { return FIXED_INT8 == (-1 == Integer.signum(src.peek()) ? DESCENDING : ASCENDING).apply(src.peek()); }
/** * Return true when the next encoded value in {@code src} uses BlobCopy * encoding, false otherwise. */ public static boolean isBlobCopy(PositionedByteRange src) { return BLOB_COPY == (-1 == Integer.signum(src.peek()) ? DESCENDING : ASCENDING).apply(src.peek()); }
/** * Return true when the next encoded value in {@code src} uses fixed-width * Int16 encoding, false otherwise. */ public static boolean isFixedInt16(PositionedByteRange src) { return FIXED_INT16 == (-1 == Integer.signum(src.peek()) ? DESCENDING : ASCENDING).apply(src.peek()); }
/** * Return true when the next encoded value in {@code src} uses Text encoding, * false otherwise. */ public static boolean isText(PositionedByteRange src) { return TEXT == (-1 == Integer.signum(src.peek()) ? DESCENDING : ASCENDING).apply(src.peek()); }
/** * Return true when the next encoded value in {@code src} is null, false * otherwise. */ public static boolean isNull(PositionedByteRange src) { return NULL == (-1 == Integer.signum(src.peek()) ? DESCENDING : ASCENDING).apply(src.peek()); }