Java ByteBuffer Read readVInt(ByteBuffer bb)

Here you can find the source of readVInt(ByteBuffer bb)

Description

read V Int

License

Open Source License

Declaration

public static int readVInt(ByteBuffer bb) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.nio.ByteBuffer;

public class Main {
    public static int readVInt(ByteBuffer bb) {
        return (int) readVLong(bb);
    }//  w w w  .  j a v  a  2  s.  c o  m

    public static int readVInt(ByteBuffer bb, int len, byte firstByte) {
        return (int) readVLong(bb, len, firstByte);
    }

    public static long readVLong(ByteBuffer bb) {
        byte firstByte = bb.get();
        int len = decodeVNumSize(firstByte);
        return readVLong(bb, len, firstByte);
    }

    public static long readVLong(ByteBuffer bb, int len, byte firstByte) {
        if (len == 1) {
            return firstByte;
        }
        long l = 0;
        for (int idx = 0; idx < len - 1; idx++) {
            byte b = bb.get();
            l = l << 8;
            l = l | (b & 0xFF);
        }
        return (isNegativeVNum(firstByte) ? ~l : l);
    }

    public static int decodeVNumSize(byte value) {
        if (value >= -112) {
            return 1;
        } else if (value < -120) {
            return -119 - value;
        }
        return -111 - value;
    }

    public static boolean isNegativeVNum(byte value) {
        return value < -120 || (value >= -112 && value < 0);
    }
}

Related

  1. readToBytes(ByteBuffer byteBuffer)
  2. readTs(ByteBuffer is)
  3. readTs(ByteBuffer is, int c)
  4. readUUID(ByteBuffer buffer)
  5. readVariableLength(ByteBuffer buf)
  6. readVInt(ByteBuffer bb)
  7. readVL(ByteBuffer byteBuffer)
  8. readZeroTermStr(ByteBuffer bb)