build Float Buffer - Java java.nio

Java examples for java.nio:FloatBuffer

Description

build Float Buffer

Demo Code


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

public class Main {
    public static FloatBuffer buildFloatBuffer(float[] array, int typeSize) {
        ByteBuffer byteBuf = ByteBuffer.allocateDirect(array.length
                * typeSize);/*from   w w  w  .j av a2s. c  om*/
        byteBuf.order(ByteOrder.nativeOrder());
        FloatBuffer buffer = byteBuf.asFloatBuffer();
        buffer.put(array);
        buffer.position(0);
        return buffer;
    }

    public static FloatBuffer buildFloatBuffer(float[] array) {
        ByteBuffer byteBuf = ByteBuffer.allocateDirect(array.length * 4);
        byteBuf.order(ByteOrder.nativeOrder());
        FloatBuffer buffer = byteBuf.asFloatBuffer();
        buffer.put(array);
        buffer.position(0);
        return buffer;
    }

    public static FloatBuffer buildFloatBuffer(int size) {
        ByteBuffer byteBuf = ByteBuffer.allocateDirect(size * 4);
        byteBuf.order(ByteOrder.nativeOrder());
        FloatBuffer buffer = byteBuf.asFloatBuffer();
        buffer.position(0);
        return buffer;
    }
}

Related Tutorials