Example usage for org.apache.lucene.util BitUtil zigZagEncode

List of usage examples for org.apache.lucene.util BitUtil zigZagEncode

Introduction

In this page you can find the example usage for org.apache.lucene.util BitUtil zigZagEncode.

Prototype

public static long zigZagEncode(long l) 

Source Link

Document

<a href="https://developers.google.com/protocol-buffers/docs/encoding#types">Zig-zag</a> encode the provided long.

Usage

From source file:org.codelibs.elasticsearch.common.io.stream.StreamOutput.java

License:Apache License

/**
 * Writes a long in a variable-length format. Writes between one and ten bytes.
 * Values are remapped by sliding the sign bit into the lsb and then encoded as an unsigned number
 * e.g., 0 -;&gt; 0, -1 -;&gt; 1, 1 -;&gt; 2, ..., Long.MIN_VALUE -;&gt; -1, Long.MAX_VALUE -;&gt; -2
 * Numbers with small absolute value will have a small encoding
 * If the numbers are known to be non-negative, use {#writeVLong(long)}
 *//*w  ww. j ava2 s .  c o  m*/
public void writeZLong(long i) throws IOException {
    // zig-zag encoding cf. https://developers.google.com/protocol-buffers/docs/encoding?hl=en
    long value = BitUtil.zigZagEncode(i);
    while ((value & 0xFFFFFFFFFFFFFF80L) != 0L) {
        writeByte((byte) ((value & 0x7F) | 0x80));
        value >>>= 7;
    }
    writeByte((byte) (value & 0x7F));
}