read U32 from ByteBuffer - Java java.nio

Java examples for java.nio:ByteBuffer Read

Description

read U32 from ByteBuffer

Demo Code


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

public class Main {
    public static int readU32(ByteBuffer buffer) {
        int result = readUI8(buffer);
        if ((result & 0x80) == 0) {
            return result;
        }// w  w w.java 2  s. c  o m

        result = result & 0x7F | readUI8(buffer) << 7;
        if ((result & 0x4000) == 0) {
            return result;
        }

        result = result & 0x3FFF | readUI8(buffer) << 14;
        if ((result & 0x200000) == 0) {
            return result;
        }

        result = result & 0x1FFFFF | readUI8(buffer) << 21;
        if ((result & 0x10000000) == 0) {
            return result;
        }

        return result & 0xFFFFFFF | readUI8(buffer) << 28;
    }

    public static int readUI8(ByteBuffer buffer) {
        return buffer.get() & 0xff;
    }
}

Related Tutorials