Java ByteBuffer Decode decodeNumber(byte firstByte, ByteBuffer buffer)

Here you can find the source of decodeNumber(byte firstByte, ByteBuffer buffer)

Description

decode Number

License

Open Source License

Declaration

public static long decodeNumber(byte firstByte, ByteBuffer buffer) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.nio.ByteBuffer;

public class Main {
    private static final long OB_MAX_INT_1B = 23;
    private static final byte OB_INT_SIGN_BIT_POS = 5;
    private static final byte OB_INT_VALUE_MASK = 0x1f;

    public static long decodeNumber(byte firstByte, ByteBuffer buffer) {
        boolean isNeg = testBit(firstByte, OB_INT_SIGN_BIT_POS);
        byte lenOrValue = (byte) (firstByte & OB_INT_VALUE_MASK);
        if (lenOrValue <= OB_MAX_INT_1B) {
            return isNeg ? -lenOrValue : lenOrValue;
        } else {/*from  ww  w. ja v a2s . c  o  m*/
            long value = 0;
            long len = lenOrValue - OB_MAX_INT_1B;
            if (len == 5 || len == 7) {
                len++;
            }
            for (int index = 0; index < len; index++) {
                value |= ((buffer.get() & 0xffl) << (index << 3));
            }
            return isNeg ? -value : value;
        }
    }

    private static boolean testBit(byte target, byte pos) {
        return 0 != (target & (1 << pos));
    }
}

Related

  1. decodeInt(ByteBuffer buffer, int start)
  2. decodeIO(String encoding, ByteBuffer bbuf)
  3. decodeLength(ByteBuffer buf)
  4. decodeLong(ByteBuffer buffer, int start)
  5. decodeNIO(String encoding, ByteBuffer bbuf)
  6. decodeStoredBits(ByteBuffer bb)
  7. decodeStringSequence(ByteBuffer bb)
  8. decodeThrowing(Charset charset, ByteBuffer in)
  9. decodeTime(byte firstByte, ByteBuffer buffer)