Allocates a ShortBuffer with the given length. - Java java.nio

Java examples for java.nio:ShortBuffer

Description

Allocates a ShortBuffer with the given length.

Demo Code


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

import java.nio.ShortBuffer;

public class Main {
    public static void main(String[] argv) throws Exception {
        int length = 2;
        System.out.println(allocateShortBuffer(length));
    }//from  w w  w . ja  va  2s  . c  o m

    /**
     * Allocates a ShortBuffer with the given length.
     *
     * @param length The length of the buffer to allocate (in elements, not bytes!).
     * @return The newly allocated buffer.
     */
    public static ShortBuffer allocateShortBuffer(int length) {
        return ByteBuffer.allocateDirect(length * 2) // short == 2 bytes
                .order(ByteOrder.nativeOrder()).asShortBuffer();
    }
}

Related Tutorials