Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.nio.ByteBuffer;

public class Main {
    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);
    }
}