Java ByteBuffer Write writeVInt(ByteBuffer bb, int i)

Here you can find the source of writeVInt(ByteBuffer bb, int i)

Description

write V Int

License

Open Source License

Declaration

public static int writeVInt(ByteBuffer bb, int i) 

Method Source Code


//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;
    }
}

Related

  1. writeVarint(int value, ByteBuffer buffer)
  2. writeVarLong(ByteBuffer buff, long x)
  3. writeVarLong(ByteBuffer buff, long x)
  4. writeVarLong(long n, ByteBuffer buff)
  5. writeVarLong(long value, ByteBuffer buf)
  6. writeWithLength(ByteBuffer buffer, byte[] src)
  7. writeXid(ByteBuffer logBuf, Xid xid)