Reads an 31 bit array length followed by the actual array data. - Java File Path IO

Java examples for File Path IO:Byte Array

Description

Reads an 31 bit array length followed by the actual array data.

Demo Code


import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Iterator;
import org.apache.log4j.Logger;

public class Main{
    /**//w w w .j  a va 2s .co m
     * Reads an 31 bit array length followed by the actual array data.
     * 
     * @param buf
     * @return the array read
     */
    final static public byte[] readArray31(ByteBuffer buf) {
        int length = getUnsigned31(buf);
        byte[] array = new byte[length];
        buf.get(array);
        return array;
    }
    /**
     * Returns 31 bits of an int, removing the sign bit.
     * 
     * @param buf
     * @return
     */
    final public static int getUnsigned31(ByteBuffer buf) {
        return buf.getInt() & 0x7fffffff;
    }
}

Related Tutorials