Make a direct NIO ByteBuffer from an array of floats - Java java.nio

Java examples for java.nio:ByteBuffer Float

Description

Make a direct NIO ByteBuffer from an array of floats

Demo Code


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

public class Main {
    /**/*from   w  w w .j a va 2s  .c o m*/
     * Make a direct NIO ByteBuffer from an array of floats
     * @param arr The array
     * @return The newly created FloatBuffer
     */
    public static ByteBuffer makeByteBuffer(byte[] arr) {
        ByteBuffer bb = ByteBuffer.allocateDirect(arr.length);
        bb.order(ByteOrder.nativeOrder());
        bb.put(arr);
        bb.position(0);
        return bb;
    }

    public static ByteBuffer makeByteBuffer(byte[] arr, int length) {
        ByteBuffer bb = ByteBuffer.allocateDirect(length);
        bb.order(ByteOrder.nativeOrder());
        bb.put(arr, 0, length);
        bb.position(0);

        return bb;
    }

    public static ByteBuffer makeByteBuffer(int size) {
        ByteBuffer bb = ByteBuffer.allocateDirect(size);
        bb.position(0);
        return bb;
    }
}

Related Tutorials