create Short Buffer - Java java.nio

Java examples for java.nio:ShortBuffer

Description

create Short Buffer

Demo Code


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

public class Main {
    /**//  w  w w.ja va  2s  .  c  om
     * 
     * @param size
     * @return
     */
    public static ShortBuffer createShortBuffer(int size) {
        return createByteBuffer(size << 1).asShortBuffer();
    }

    /**
     * 
     * @param data
     * @param offset
     * @param length
     * @return
     */
    public static ShortBuffer createShortBuffer(short[] data, int offset,
            int length) {
        return createBuffer(data, offset, length).asShortBuffer();
    }

    /**
     * 
     * @param data
     * @param length
     * @return
     */
    public static ShortBuffer createShortBuffer(short[] data, int length) {
        return createBuffer(data, 0, length).asShortBuffer();
    }

    /**
     * 
     * @param data
     * @return
     */
    public static ShortBuffer createShortBuffer(short[] data) {
        return createBuffer(data, 0, data.length).asShortBuffer();
    }

    /**
     * 
     * @param size
     * @return
     */
    public static ByteBuffer createByteBuffer(int size) {
        return prepareBuffer(ByteBuffer.allocate(size));
    }

    /**
     * 
     * @param data
     * @param offset
     * @param length
     * @return
     */
    public static ByteBuffer createBuffer(byte[] data, int offset,
            int length) {
        if (data == null)
            throw new IllegalArgumentException("The data may not be null.");

        if (offset > data.length)
            throw new IllegalArgumentException(
                    "The offset must be below the length of the data provided.");

        if (offset + length > data.length)
            throw new IllegalArgumentException(
                    "The length of the buffer added to the offset cannot exceed the data length.");

        ByteBuffer buffer = createByteBuffer(length);
        buffer.put(data, offset, length);
        buffer.limit(length);
        buffer.position(0);
        return buffer;
    }

    /**
     * 
     * @param data
     * @param length
     * @return
     */
    public static ByteBuffer createBuffer(byte[] data, int length) {
        return createBuffer(data, 0, length);
    }

    /**
     * 
     * @param data
     * @return
     */
    public static ByteBuffer createBuffer(byte[] data) {
        return createBuffer(data, 0, data.length);
    }

    /**
     * 
     * @param data
     * @param offset
     * @param length
     * @return
     */
    public static ByteBuffer createBuffer(double[] data, int offset,
            int length) {
        if (data == null)
            throw new IllegalArgumentException("The data may not be null.");

        if (offset > data.length)
            throw new IllegalArgumentException(
                    "The offset must be below the length of the data provided.");

        if (offset + length > data.length)
            throw new IllegalArgumentException(
                    "The length of the buffer added to the offset cannot exceed the data length.");

        ByteBuffer backing = createByteBuffer(length << 3);
        DoubleBuffer buffer = backing.asDoubleBuffer();
        buffer.put(data, offset, length);
        backing.limit(length << 3);
        backing.position(0);
        return backing;
    }

    /**
     * 
     * @param data
     * @param length
     * @return
     */
    public static ByteBuffer createBuffer(double[] data, int length) {
        return createBuffer(data, 0, length);
    }

    /**
     * 
     * @param data
     * @return
     */
    public static ByteBuffer createBuffer(double[] data) {
        return createBuffer(data, 0, data.length);
    }

    /**
     * 
     * @param data
     * @param offset
     * @param length
     * @return
     */
    public static ByteBuffer createBuffer(float[] data, int offset,
            int length) {
        if (data == null)
            throw new IllegalArgumentException("The data may not be null.");

        if (offset > data.length)
            throw new IllegalArgumentException(
                    "The offset must be below the length of the data provided.");

        if (offset + length > data.length)
            throw new IllegalArgumentException(
                    "The length of the buffer added to the offset cannot exceed the data length.");

        ByteBuffer backing = createByteBuffer(length << 2);
        FloatBuffer buffer = backing.asFloatBuffer();
        buffer.put(data, offset, length);
        backing.limit(length << 2);
        backing.position(0);
        return backing;
    }

    /**
     * 
     * @param data
     * @param length
     * @return
     */
    public static ByteBuffer createBuffer(float[] data, int length) {
        return createBuffer(data, 0, length);
    }

    /**
     * 
     * @param data
     * @return
     */
    public static ByteBuffer createBuffer(float[] data) {
        return createBuffer(data, 0, data.length);
    }

    /**
     * 
     * @param data
     * @param offset
     * @param length
     * @return
     */
    public static ByteBuffer createBuffer(int[] data, int offset, int length) {
        if (data == null)
            throw new IllegalArgumentException("The data may not be null.");

        if (offset > data.length)
            throw new IllegalArgumentException(
                    "The offset must be below the length of the data provided.");

        if (offset + length > data.length)
            throw new IllegalArgumentException(
                    "The length of the buffer added to the offset cannot exceed the data length.");

        ByteBuffer backing = createByteBuffer(length << 2);
        IntBuffer buffer = backing.asIntBuffer();
        buffer.put(data, offset, length);
        backing.limit(length << 2);
        backing.position(0);
        return backing;
    }

    /**
     * 
     * @param data
     * @param length
     * @return
     */
    public static ByteBuffer createBuffer(int[] data, int length) {
        return createBuffer(data, 0, length);
    }

    /**
     * 
     * @param data
     * @return
     */
    public static ByteBuffer createBuffer(int[] data) {
        return createBuffer(data, 0, data.length);
    }

    /**
     * 
     * @param data
     * @param offset
     * @param length
     * @return
     */
    public static ByteBuffer createBuffer(short[] data, int offset,
            int length) {
        if (data == null)
            throw new IllegalArgumentException("The data may not be null.");

        if (offset > data.length)
            throw new IllegalArgumentException(
                    "The offset must be below the length of the data provided.");

        if (offset + length > data.length)
            throw new IllegalArgumentException(
                    "The length of the buffer added to the offset cannot exceed the data length.");

        ByteBuffer backing = createByteBuffer(length << 1);
        ShortBuffer buffer = backing.asShortBuffer();
        buffer.put(data, offset, length);
        backing.limit(length << 1);
        backing.position(0);
        return backing;
    }

    /**
     * 
     * @param data
     * @param length
     * @return
     */
    public static ByteBuffer createBuffer(short[] data, int length) {
        return createBuffer(data, 0, length);
    }

    /**
     * 
     * @param data
     * @return
     */
    public static ByteBuffer createBuffer(short[] data) {
        return createBuffer(data, 0, data.length);
    }

    /**
     * 
     * @param buffer
     * @return
     */
    private static ByteBuffer prepareBuffer(ByteBuffer buffer) {
        return buffer.order(ByteOrder.nativeOrder());
    }
}

Related Tutorials