create Direct Buffer - Java java.nio

Java examples for java.nio:ByteBuffer

Description

create Direct Buffer

Demo Code


//package com.java2s;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;

public class Main {
    /**/*from w  w  w .  ja  v a2  s. c  om*/
     * 
     * @param data
     * @param offset
     * @param length
     * @return
     */
    public static ByteBuffer createDirectBuffer(byte[] data, int offset,
            int length) {
        if (data == null)
            throw new IllegalArgumentException("The data may not be null.");

        if (offset > data.length)
            throw new IllegalArgumentException(
                    "The offset must be below the length of the data provided.");

        if (offset + length > data.length)
            throw new IllegalArgumentException(
                    "The length of the buffer added to the offset cannot exceed the data length.");

        ByteBuffer buffer = createDirectByteBuffer(length);
        buffer.put(data, offset, length);
        buffer.limit(length);
        buffer.position(0);
        return buffer;
    }

    /**
     * 
     * @param data
     * @param length
     * @return
     */
    public static ByteBuffer createDirectBuffer(byte[] data, int length) {
        return createDirectBuffer(data, 0, length);
    }

    /**
     * 
     * @param data
     * @return
     */
    public static ByteBuffer createDirectBuffer(byte[] data) {
        return createDirectBuffer(data, 0, data.length);
    }

    /**
     * 
     * @param data
     * @param offset
     * @param length
     * @return
     */
    public static ByteBuffer createDirectBuffer(double[] data, int offset,
            int length) {
        if (data == null)
            throw new IllegalArgumentException("The data may not be null.");

        if (offset > data.length)
            throw new IllegalArgumentException(
                    "The offset must be below the length of the data provided.");

        if (offset + length > data.length)
            throw new IllegalArgumentException(
                    "The length of the buffer added to the offset cannot exceed the data length.");

        ByteBuffer backing = createDirectByteBuffer(length << 3);
        DoubleBuffer buffer = backing.asDoubleBuffer();
        buffer.put(data, offset, length);
        backing.limit(length << 3);
        backing.position(0);
        return backing;
    }

    /**
     * 
     * @param data
     * @param length
     * @return
     */
    public static ByteBuffer createDirectBuffer(double[] data, int length) {
        return createDirectBuffer(data, 0, length);
    }

    /**
     * 
     * @param data
     * @return
     */
    public static ByteBuffer createDirectBuffer(double[] data) {
        return createDirectBuffer(data, 0, data.length);
    }

    /**
     * 
     * @param data
     * @param offset
     * @param length
     * @return
     */
    public static ByteBuffer createDirectBuffer(float[] data, int offset,
            int length) {
        if (data == null)
            throw new IllegalArgumentException("The data may not be null.");

        if (offset > data.length)
            throw new IllegalArgumentException(
                    "The offset must be below the length of the data provided.");

        if (offset + length > data.length)
            throw new IllegalArgumentException(
                    "The length of the buffer added to the offset cannot exceed the data length.");

        ByteBuffer backing = createDirectByteBuffer(length << 2);
        FloatBuffer buffer = backing.asFloatBuffer();
        buffer.put(data, offset, length);
        backing.limit(length << 2);
        backing.position(0);
        return backing;
    }

    /**
     * 
     * @param data
     * @param length
     * @return
     */
    public static ByteBuffer createDirectBuffer(float[] data, int length) {
        return createDirectBuffer(data, 0, length);
    }

    /**
     * 
     * @param data
     * @return
     */
    public static ByteBuffer createDirectBuffer(float[] data) {
        return createDirectBuffer(data, 0, data.length);
    }

    /**
     * 
     * @param data
     * @param offset
     * @param length
     * @return
     */
    public static ByteBuffer createDirectBuffer(int[] data, int offset,
            int length) {
        if (data == null)
            throw new IllegalArgumentException("The data may not be null.");

        if (offset > data.length)
            throw new IllegalArgumentException(
                    "The offset must be below the length of the data provided.");

        if (offset + length > data.length)
            throw new IllegalArgumentException(
                    "The length of the buffer added to the offset cannot exceed the data length.");

        ByteBuffer backing = createDirectByteBuffer(length << 2);
        IntBuffer buffer = backing.asIntBuffer();
        buffer.put(data, offset, length);
        backing.limit(length << 2);
        backing.position(0);
        return backing;
    }

    /**
     * 
     * @param data
     * @param length
     * @return
     */
    public static ByteBuffer createDirectBuffer(int[] data, int length) {
        return createDirectBuffer(data, 0, length);
    }

    /**
     * 
     * @param data
     * @return
     */
    public static ByteBuffer createDirectBuffer(int[] data) {
        return createDirectBuffer(data, 0, data.length);
    }

    /**
     * 
     * @param data
     * @param offset
     * @param length
     * @return
     */
    public static ByteBuffer createDirectBuffer(short[] data, int offset,
            int length) {
        if (data == null)
            throw new IllegalArgumentException("The data may not be null.");

        if (offset > data.length)
            throw new IllegalArgumentException(
                    "The offset must be below the length of the data provided.");

        if (offset + length > data.length)
            throw new IllegalArgumentException(
                    "The length of the buffer added to the offset cannot exceed the data length.");

        ByteBuffer backing = createDirectByteBuffer(length << 1);
        ShortBuffer buffer = backing.asShortBuffer();
        buffer.put(data, offset, length);
        backing.limit(length << 1);
        backing.position(0);
        return backing;
    }

    /**
     * 
     * @param data
     * @param length
     * @return
     */
    public static ByteBuffer createDirectBuffer(short[] data, int length) {
        return createDirectBuffer(data, 0, length);
    }

    /**
     * 
     * @param data
     * @return
     */
    public static ByteBuffer createDirectBuffer(short[] data) {
        return createDirectBuffer(data, 0, data.length);
    }

    /**
     * 
     * @param size
     * @return
     */
    public static ByteBuffer createDirectByteBuffer(int size) {
        return prepareBuffer(ByteBuffer.allocate(size));
    }

    /**
     * 
     * @param buffer
     * @return
     */
    private static ByteBuffer prepareBuffer(ByteBuffer buffer) {
        return buffer.order(ByteOrder.nativeOrder());
    }
}

Related Tutorials