public final RevPerson readRevPerson(DataInput in) throws IOException { final String name = in.readUTF(); final String email = in.readUTF(); final long timestamp = readUnsignedVarLong(in); final int tzOffset = readUnsignedVarInt(in); return RevPersonBuilder.build(name.length() == 0 ? null : name, email.length() == 0 ? null : email, timestamp, tzOffset); }
/** * Writes the length of the array * * @return the number of bytes written * @throws IOException */ private static int writeLength(int[] values, DataOutput out) throws IOException { final int length = values.length; return writeUnsignedVarInt(length, out); }
/** * Encodes a value using the variable-length encoding from * <a href="http://code.google.com/apis/protocolbuffers/docs/encoding.html"> Google Protocol * Buffers</a>. It uses zig-zag encoding to efficiently encode signed values. If values are * known to be nonnegative, {@link #writeUnsignedVarLong(long, DataOutput)} should be used. * * @param value value to encode * @param out to write bytes to * @throws IOException if {@link DataOutput} throws {@link IOException} */ public static int writeSignedVarLong(long value, DataOutput out) throws IOException { // Great trick from http://code.google.com/apis/protocolbuffers/docs/encoding.html#types return writeUnsignedVarLong((value << 1) ^ (value >> 63), out); }
public static void encode(DataOutput out, RevTree tree) throws IOException { // object type out.write(RevObject.TYPE.TREE.ordinal()); final long totalSize = tree.size(); final int totalSubtrees = tree.numTrees(); Varint.writeUnsignedVarLong(totalSize, out); Varint.writeUnsignedVarInt(totalSubtrees, out); }
@Override public double[] readDoubleArray(DataInput in) throws IOException { final int len = readUnsignedVarInt(in); double[] doubles = new double[len]; for (int i = 0; i < len; i++) { doubles[i] = in.readDouble(); } return doubles; }
private void checkUnsignedVarLong(final long value) throws IOException { ByteArrayDataOutput out = newDataOutput(); writeUnsignedVarLong(value, out); long read = readUnsignedVarLong(newDataInput(out.toByteArray())); assertEquals(value, read); } }
public static void readLongArrayDeltaEncoded(DataInput in, long[] values, int offset, int length) throws IOException { long prev = 0; final int max = offset + length; for (int i = offset; i < max; i++) { long delta = readSignedVarLong(in); long value = prev + delta; values[i] = value; prev = value; } }
public static void readIntArrayDeltaEncoded(DataInput in, int[] values, final int offset, final int length) throws IOException { int prev = 0; final int max = offset + length; for (int i = offset; i < max; i++) { int delta = readSignedVarInt(in); int value = prev + delta; values[i] = value; prev = value; } }
@Override public Long deserialize(DataInput in, int available) throws IOException { return Varint.readUnsignedVarLong(in); } };
public static void writeLongArrayDeltaEncoded(DataOutput out, long[] values, int offset, int length) throws IOException { long prev = 0; final int max = offset + length; for (int i = offset; i < max; i++) { long value = values[i]; long delta = value - prev; prev = value; writeSignedVarLong(delta, out); } }
public static void writeIntArrayDeltaEncoded(DataOutput out, int[] values, int offset, int length) throws IOException { int prev = 0; final int max = offset + length; for (int i = offset; i < max; i++) { int value = values[i]; int delta = value - prev; prev = value; writeSignedVarInt(delta, out); } }
private void checkSignedVarInt(final int value) throws IOException { ByteArrayDataOutput out = newDataOutput(); writeSignedVarInt(value, out); int read = readSignedVarInt(newDataInput(out.toByteArray())); assertEquals(value, read); }
private void checkSignedVarLong(final long value) throws IOException { ByteArrayDataOutput out = newDataOutput(); writeSignedVarLong(value, out); long read = readSignedVarLong(newDataInput(out.toByteArray())); assertEquals(value, read); }
public static void encode(DataOutput out, RevTree tree) throws IOException { // object type out.write(RevObject.TYPE.TREE.ordinal()); final long totalSize = tree.size(); final int totalSubtrees = tree.numTrees(); Varint.writeUnsignedVarLong(totalSize, out); Varint.writeUnsignedVarInt(totalSubtrees, out); }
@Override public byte[] readByteArray(DataInput in) throws IOException { final int len = readUnsignedVarInt(in); byte[] bytes = new byte[len]; in.readFully(bytes); return bytes; }
private void checkUnsignedVarLong(final long value) throws IOException { ByteArrayDataOutput out = newDataOutput(); writeUnsignedVarLong(value, out); long read = readUnsignedVarLong(newDataInput(out.toByteArray())); assertEquals(value, read); } }
public static void readLongArrayDeltaEncoded(DataInput in, long[] values, int offset, int length) throws IOException { long prev = 0; final int max = offset + length; for (int i = offset; i < max; i++) { long delta = readSignedVarLong(in); long value = prev + delta; values[i] = value; prev = value; } }
public static void readIntArrayDeltaEncoded(DataInput in, int[] values, final int offset, final int length) throws IOException { int prev = 0; final int max = offset + length; for (int i = offset; i < max; i++) { int delta = readSignedVarInt(in); int value = prev + delta; values[i] = value; prev = value; } }
/** * @param in to read bytes from * @return decode value * @throws IOException if {@link DataInput} throws {@link IOException} * @throws IllegalArgumentException if variable-length value does not terminate after 9 bytes * have been read * @see #writeSignedVarLong(long, DataOutput) */ public static long readSignedVarLong(DataInput in) throws IOException { long raw = readUnsignedVarLong(in); // This undoes the trick in writeSignedVarLong() long temp = (((raw << 63) >> 63) ^ raw) >> 1; // This extra step lets us deal with the largest signed values by treating // negative results from read unsigned methods as like unsigned values // Must re-flip the top bit if the original read value had it set. return temp ^ (raw & (1L << 63)); }
public static void writeLongArrayDeltaEncoded(DataOutput out, long[] values, int offset, int length) throws IOException { long prev = 0; final int max = offset + length; for (int i = offset; i < max; i++) { long value = values[i]; long delta = value - prev; prev = value; writeSignedVarLong(delta, out); } }