setup ShortBuffer - Java java.nio

Java examples for java.nio:ShortBuffer

Description

setup ShortBuffer

Demo Code

/**//  w w w  . j a  va  2 s .  c om
 *
 *  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;

import java.nio.ShortBuffer;

public class Main {
    public static ShortBuffer setupShortBuffer(ShortBuffer preBuffer,
            short[] array) {

        if (preBuffer == null || preBuffer.capacity() < array.length) {
            preBuffer = createShortBuffer(array.length * 2);
        } else {
            preBuffer.clear();
        }

        preBuffer.clear();
        preBuffer.put(array);
        preBuffer.position(0);

        return preBuffer;
    }

    public static ShortBuffer createShortBuffer(int shortCount) {
        ByteBuffer data = ByteBuffer.allocateDirect(shortCount * 4);
        data.order(ByteOrder.nativeOrder());
        ShortBuffer p1 = data.asShortBuffer();
        return p1;
    }
}

Related Tutorials