/** * 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); }
/** * 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); }
@Override public void serialize(DataOutput out, Long value) throws IOException { Varint.writeUnsignedVarLong(value.longValue(), out); }
public static void writeUnsignedLongArray(long[] values, DataOutput out) throws IOException { final int length = writeLength(values, out); for (int i = 0; i < length; i++) { writeUnsignedVarLong(values[i], out); } }
public static void writeUnsignedLongArray(long[] values, DataOutput out) throws IOException { final int length = writeLength(values, out); for (int i = 0; i < length; i++) { writeUnsignedVarLong(values[i], out); } }
public final void writePerson(RevPerson person, DataOutput data) throws IOException { data.writeUTF(person.getName().or("")); data.writeUTF(person.getEmail().or("")); writeUnsignedVarLong(person.getTimestamp(), data); writeUnsignedVarInt(person.getTimeZoneOffset(), data); }
public final void writePerson(RevPerson person, DataOutput data) throws IOException { data.writeUTF(person.getName().or("")); data.writeUTF(person.getEmail().or("")); writeUnsignedVarLong(person.getTimestamp(), data); writeUnsignedVarInt(person.getTimeZoneOffset(), data); }
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); }
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); }
public static void serialize(DAG dag, DataOutput out) throws IOException { final ObjectId treeId = dag.originalTreeId; final STATE state = dag.getState(); final long childCount = dag.getTotalChildCount(); final Collection<NodeId> children = dag.children.values(); final Set<TreeId> buckets = dag.buckets; treeId.writeTo(out); out.writeByte(state.ordinal()); Varint.writeUnsignedVarLong(childCount, out); Varint.writeUnsignedVarInt(children.size(), out); Varint.writeUnsignedVarInt(buckets.size(), out); for (NodeId nodeid : children) { NodeId.write(nodeid, out); } for (TreeId tid : buckets) { byte[] bucketIndicesByDepth = tid.bucketIndicesByDepth; Varint.writeUnsignedVarInt(bucketIndicesByDepth.length, out); out.write(bucketIndicesByDepth); } }
public void writeTree(RevTree tree, DataOutput data) throws IOException { writeUnsignedVarLong(tree.size(), data); writeUnsignedVarInt(tree.numTrees(), data); Envelope envBuff = new Envelope(); final int nFeatures = tree.features().size(); writeUnsignedVarInt(nFeatures, data); for (Node feature : tree.features()) { writeNode(feature, data, envBuff); } final int nTrees = tree.trees().size(); writeUnsignedVarInt(nTrees, data); for (Node subTree : tree.trees()) { writeNode(subTree, data, envBuff); } ImmutableSortedMap<Integer, Bucket> buckets = tree.buckets(); final int nBuckets = buckets.size(); writeUnsignedVarInt(nBuckets, data); for (Map.Entry<Integer, Bucket> bucket : buckets.entrySet()) { writeBucket(bucket.getKey(), bucket.getValue(), data, envBuff); } }
private void checkUnsignedVarLong(final long value) throws IOException { ByteArrayDataOutput out = newDataOutput(); writeUnsignedVarLong(value, out); long read = readUnsignedVarLong(newDataInput(out.toByteArray())); assertEquals(value, read); } }
private void checkUnsignedVarLong(final long value) throws IOException { ByteArrayDataOutput out = newDataOutput(); writeUnsignedVarLong(value, out); long read = readUnsignedVarLong(newDataInput(out.toByteArray())); assertEquals(value, read); } }
public void writeTree(RevTree tree, DataOutput data) throws IOException { writeUnsignedVarLong(tree.size(), data); writeUnsignedVarInt(tree.numTrees(), data); Envelope envBuff = new Envelope(); final int nFeatures = tree.featuresSize(); writeUnsignedVarInt(nFeatures, data); tree.forEachFeature(n -> writeNodeQuiet(n, data, envBuff)); final int nTrees = tree.treesSize(); writeUnsignedVarInt(nTrees, data); tree.forEachTree(n -> writeNodeQuiet(n, data, envBuff)); final int nBuckets = tree.bucketsSize(); writeUnsignedVarInt(nBuckets, data); tree.forEachBucket(bucket -> { try { writeBucket(bucket, data, envBuff); } catch (IOException e) { throw new RuntimeException(e); } }); }