Here you can find the source of writeVInt(ByteBuffer bb, int i)
public static int writeVInt(ByteBuffer bb, int i)
//package com.java2s; //License from project: Open Source License import java.nio.ByteBuffer; public class Main { public static int writeVInt(ByteBuffer bb, int i) { return writeVLong(bb, i); }/*from w w w . j a va 2s. com*/ 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; } }