Java ByteBuffer Read readVariableLength(ByteBuffer buf)

Here you can find the source of readVariableLength(ByteBuffer buf)

Description

read Variable Length

License

Open Source License

Declaration

public final static int readVariableLength(ByteBuffer buf) 

Method Source Code


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

public class Main {
    public final static int readVariableLength(InputStream is) {
        int length = 0;
        int cur;//from  w  w w  .ja v a2 s. co  m
        try {
            do {
                cur = is.read();
                length |= (cur & 0x7F);
                if ((cur & 0x80) != 0)
                    length <<= 7;
            } while ((cur & 0x80) != 0);
            return length;
        } catch (Exception e) {
            return 0;
        }
    }

    public final static int readVariableLength(ByteBuffer buf) {
        int length = 0;
        int cur;
        if (buf.hasRemaining())
            do {
                cur = buf.get();
                length |= (cur & 0x7F);
                if ((cur & 0x80) != 0)
                    length <<= 7;
            } while ((cur & 0x80) != 0 && buf.hasRemaining());
        return length;
    }
}

Related

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