Java ByteBuffer to Long readVarLong(ByteBuffer buff)

Here you can find the source of readVarLong(ByteBuffer buff)

Description

Read a variable size long.

License

Mozilla Public License

Parameter

Parameter Description
buff the source buffer

Return

the value

Declaration

public static long readVarLong(ByteBuffer buff) 

Method Source Code

//package com.java2s;
/*//from w  w w  .  j  a va  2s.c om
 * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0,
 * and the EPL 1.0 (http://h2database.com/html/license.html).
 * Initial Developer: H2 Group
 */

import java.nio.ByteBuffer;

public class Main {
    /**
     * Read a variable size long.
     *
     * @param buff the source buffer
     * @return the value
     */
    public static long readVarLong(ByteBuffer buff) {
        long x = buff.get();
        if (x >= 0) {
            return x;
        }
        x &= 0x7f;
        for (int s = 7; s < 64; s += 7) {
            long b = buff.get();
            x |= (b & 0x7f) << s;
            if (b >= 0) {
                break;
            }
        }
        return x;
    }
}

Related

  1. readUInt16BE(ByteBuffer bb)
  2. readUInt32(ByteBuffer buf, int length)
  3. readUInt64(ByteBuffer bb)
  4. readUInt64(ByteBuffer byteBuffer)
  5. readUInt8(ByteBuffer bb)
  6. readVarlong(ByteBuffer buffer)
  7. toLong(ByteBuffer buffer)
  8. toLong(ByteBuffer buffer)
  9. toLong(ByteBuffer bytes)