writes the length of data as an 24 bit unsigned value, followed by the actual data. - Java File Path IO

Java examples for File Path IO:Byte Array

Description

writes the length of data as an 24 bit unsigned value, followed by the actual 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{
    /**//  www  .j a  v  a  2 s .  co  m
     * writes the length of data as an 24 bit unsigned value, followed by the
     * actual data. does not check that the length of the data-array actually
     * fits in the lengths field.
     * 
     * @param buf
     * @param data
     */
    final static public void writeArray24(ByteBuffer buf, byte[] data) {
        putUnsigned24(buf, data.length);
        buf.put(data);
    }
    /**
     * Puts the 24 least significant bits of value in to the byte buffer, big
     * endian.
     * 
     * @param buf
     * @param value
     */
    final public static void putUnsigned24(ByteBuffer buf, int value) {
        buf.put((byte) (value >> 16));
        buf.put((byte) (value >> 8));
        buf.put((byte) value);
    }
    /**
     * Puts the 24 least significant bits of value in to the byte buffer, big
     * endian.
     * 
     * @param buf
     * @param value
     */
    final public static void putUnsigned24(ByteBuffer buf, int index,
            int value) {
        buf.put(index, (byte) (value >> 16));
        index++;
        buf.put(index, (byte) (value >> 8));
        index++;
        buf.put(index, (byte) value);
    }
}

Related Tutorials