transport Array To Native Buffer, FloatBuffer, IntBuffer - Android java.nio

Android examples for java.nio:FloatBuffer

Description

transport Array To Native Buffer, FloatBuffer, IntBuffer

Demo Code


//package com.book2s;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;

public class Main {

    public static FloatBuffer transportArrayToNativeBuffer(float[] fArray) {
        ByteBuffer byteBuffer = ByteBuffer
                .allocateDirect(fArray.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        return (FloatBuffer) byteBuffer.asFloatBuffer().put(fArray)
                .position(0);//from   www.java2  s  .c o  m
    }

    public static IntBuffer transportArrayToNativeBuffer(int[] iArray) {
        ByteBuffer byteBuffer = ByteBuffer
                .allocateDirect(iArray.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        return (IntBuffer) byteBuffer.asIntBuffer().put(iArray).position(0);
    }

    public static ShortBuffer transportArrayToNativeBuffer(short[] sArray) {
        ByteBuffer byteBuffer = ByteBuffer
                .allocateDirect(sArray.length * 2);
        byteBuffer.order(ByteOrder.nativeOrder());
        return (ShortBuffer) byteBuffer.asShortBuffer().put(sArray)
                .position(0);
    }
}

Related Tutorials