Java ByteBuffer to Int readVarInt(ByteBuffer buff)

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

Description

Reads a VarInt.

License

LGPL

Return

the int value

Declaration

public static int readVarInt(ByteBuffer buff) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.nio.ByteBuffer;

public class Main {
    /**/*from  ww  w.  jav a  2s . c o m*/
     * Reads a VarInt.
     *
     * @return the int value
     */
    public static int readVarInt(ByteBuffer buff) {
        int shift = 0, i = 0;
        while (true) {
            byte b = (byte) buff.get();
            i |= (b & 0x7F) << shift;// Remove sign bit and shift to get the next 7 bits
            shift += 7;
            if (b >= 0) {// VarInt byte prefix is 0, it means that we just decoded the last 7 bits, therefore we've
                // finished.
                return i;
            }
        }
    }
}

Related

  1. readUnsignedShort(ByteBuffer bb)
  2. readUnsignedShort(ByteBuffer buffer)
  3. readUnsignedTriByte(ByteBuffer buffer)
  4. readUnsignedVarint(ByteBuffer buffer)
  5. readVarInt(ByteBuffer buff)
  6. readVarint(ByteBuffer buffer)
  7. readVarInt(ByteBuffer buffer)
  8. readVarInt(ByteBuffer stream)
  9. readVarIntRest(ByteBuffer buff, int b)