setup ByteBuffer - Java java.nio

Java examples for java.nio:ByteBuffer

Description

setup ByteBuffer

Demo Code

/**//from   w  w w  .  j ava 2 s  .co m
 *
 *  You can modify and use this source freely
 *  only for the development of application related Live2D.
 *
 *  (c) Live2D Inc. All rights reserved.
 */
//package com.java2s;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    public static ByteBuffer setupByteBuffer(ByteBuffer preBuffer,
            byte[] array) {

        if (preBuffer == null || preBuffer.capacity() < array.length) {
            preBuffer = createByteBuffer(array.length * 2);
        } else {
            preBuffer.clear();
        }
        preBuffer.put(array);
        preBuffer.position(0);
        return preBuffer;
    }

    public static ByteBuffer createByteBuffer(int count) {
        ByteBuffer data = ByteBuffer.allocateDirect(count * 4);
        data.order(ByteOrder.nativeOrder());
        return data;
    }
}

Related Tutorials