@Override public void fromBinary(byte[] bytes) { ByteBuffer buf = ByteBuffer.wrap(bytes); maxVertices = VarintUtils.readUnsignedInt(buf); simpVertMin = VarintUtils.readUnsignedInt(buf); tolerance = buf.getDouble(); }
@Override public byte[] toBinary() { final byte[] backingBuffer = new byte[VarintUtils.unsignedIntByteLength(maxVertices) + VarintUtils.unsignedIntByteLength(simpVertMin) + Double.BYTES]; ByteBuffer buf = ByteBuffer.wrap(backingBuffer); VarintUtils.writeUnsignedInt(maxVertices, buf); VarintUtils.writeUnsignedInt(simpVertMin, buf); buf.putDouble(tolerance); return backingBuffer; }
@Override public int[] readField(final byte[] fieldData) { if ((fieldData == null) || (fieldData.length == 0)) { return null; } final ByteBuffer buff = ByteBuffer.wrap(fieldData); int count = VarintUtils.readUnsignedInt(buff); final int[] result = new int[count]; for (int i = 0; i < count; i++) { result[i] = VarintUtils.readSignedInt(buff); } return result; }
public int bufferSize() { int bufferSize = VarintUtils.unsignedLongByteLength(totalCount) + VarintUtils.unsignedIntByteLength(count.length) + 16; for (int i = 0; i < count.length; i++) { bufferSize += VarintUtils.unsignedLongByteLength(count[i]); } return bufferSize; }
public static int signedIntByteLength(int value) { return unsignedIntByteLength(signedToUnsignedInt(value)); }
public static void writeSignedInt(int value, ByteBuffer buffer) { writeUnsignedInt(signedToUnsignedInt(value), buffer); }
@Override public byte[] writeField(final int[] fieldValue) { if (fieldValue == null) { return new byte[] {}; } int bytes = VarintUtils.unsignedIntByteLength(fieldValue.length); for (final int value : fieldValue) { bytes += VarintUtils.signedIntByteLength(value); } final ByteBuffer buf = ByteBuffer.allocate(bytes); VarintUtils.writeUnsignedInt(fieldValue.length, buf); for (final int value : fieldValue) { VarintUtils.writeSignedInt(value, buf); } return buf.array(); } }
public void fromBinary(ByteBuffer buffer) { totalCount = VarintUtils.readUnsignedLong(buffer); minValue = buffer.getDouble(); maxValue = buffer.getDouble(); final int s = VarintUtils.readUnsignedInt(buffer); count = new long[s]; for (int i = 0; i < s; i++) { count[i] = VarintUtils.readUnsignedLong(buffer); } }
@Override public byte[] writeField(final Integer fieldValue) { if (fieldValue == null) { return new byte[] {}; } final ByteBuffer buf = ByteBuffer.allocate(VarintUtils.signedIntByteLength(fieldValue)); VarintUtils.writeSignedInt(fieldValue, buf); return buf.array(); } }
@Override public void fromBinary(final byte[] bytes) { final ByteBuffer bb = ByteBuffer.wrap(bytes); final byte[] fieldNameBytes = new byte[VarintUtils.readUnsignedInt(bb)]; bb.get(fieldNameBytes); fieldName = StringUtils.stringFromBinary(fieldNameBytes); start = new Date(VarintUtils.readTime(bb)); end = new Date(VarintUtils.readTime(bb)); inclusiveLow = (bb.get() > 0) ? true : false; inclusiveHigh = (bb.get() > 1) ? true : false; } }
public void toBinary(ByteBuffer buffer) { VarintUtils.writeUnsignedLong(totalCount, buffer); buffer.putDouble(minValue); buffer.putDouble(maxValue); VarintUtils.writeUnsignedInt(count.length, buffer); for (int i = 0; i < count.length; i++) { VarintUtils.writeUnsignedLong(count[i], buffer); } }
@Override public long[] readField(final byte[] fieldData) { if ((fieldData == null) || (fieldData.length == 0)) { return null; } final ByteBuffer buff = ByteBuffer.wrap(fieldData); int count = VarintUtils.readUnsignedInt(buff); final long[] result = new long[count]; for (int i = 0; i < count; i++) { result[i] = VarintUtils.readSignedLong(buff); } return result; }
public static int unsignedShortByteLength(short value) { return unsignedIntByteLength(value & 0xFFFF); }
public static void writeUnsignedShort(short value, ByteBuffer buffer) { writeUnsignedInt(value & 0xFFFF, buffer); }
@Override public Integer readField(final byte[] fieldData) { if ((fieldData == null) || (fieldData.length == 0)) { return null; } return VarintUtils.readSignedInt(ByteBuffer.wrap(fieldData)); }
@Override public Long resultFromBinary(final byte[] binary) { return VarintUtils.readUnsignedLong(ByteBuffer.wrap(binary)); } }
public static long readSignedLong(ByteBuffer buffer) { return unsignedToSignedLong(readUnsignedLong(buffer)); }
/** * Read a timestamp from a {@code ByteBuffer} that was previously encoded with {@link #writeTime}. * * @param buffer the {@code ByteBuffer} to read from * @return the decoded timestamp */ public static long readTime(ByteBuffer buffer) { return VarintUtils.readSignedLong(buffer) + TIME_EPOCH; } }
@Override public byte[] writeField(Integer[] fieldValue) { if (fieldValue == null) { return new byte[] {}; } int bytes = VarintUtils.unsignedIntByteLength(fieldValue.length); for (final Integer value : fieldValue) { bytes++; if (value != null) { bytes += VarintUtils.signedIntByteLength(value); } } final ByteBuffer buf = ByteBuffer.allocate(bytes); VarintUtils.writeUnsignedInt(fieldValue.length, buf); for (final Integer value : fieldValue) { if (value == null) { buf.put((byte) 0x0); } else { buf.put((byte) 0x1); VarintUtils.writeSignedInt(value, buf); } } return buf.array(); } }