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 int writeVInt(ByteBuffer bb, int i) {
        return writeVLong(bb, i);
    }

    public static int writeVLong(ByteBuffer bb, long l) {
        int initPos = bb.position();
        if (l >= -112 && l <= 127) {
            bb.put((byte) l);
            return 1;
        }

        int len = -112;
        if (l < 0) {
            l ^= -1L; // take one's complement'
            len = -120;
        }

        long tmp = l;
        while (tmp != 0) {
            tmp = tmp >> 8;
            len--;
        }

        bb.put((byte) len);

        len = (len < -120) ? -(len + 120) : -(len + 112);

        for (int idx = len; idx != 0; idx--) {
            int shiftbits = (idx - 1) * 8;
            long mask = 0xFFL << shiftbits;
            bb.put((byte) ((l & mask) >> shiftbits));
        }
        return bb.position() - initPos;
    }
}