Java IntBuffer Create createIntBuffer(final int... data)

Here you can find the source of createIntBuffer(final int... data)

Description

Generate a new IntBuffer using the given array of ints.

License

Open Source License

Parameter

Parameter Description
data array of ints to place into a new IntBuffer

Declaration

public static IntBuffer createIntBuffer(final int... data) 

Method Source Code

//package com.java2s;
/**//www  .  ja v a  2s .  c om
 * Copyright (c) 2008-2012 Ardor Labs, Inc.
 *
 * This file is part of Ardor3D.
 *
 * Ardor3D is free software: you can redistribute it and/or modify it 
 * under the terms of its license which may be found in the accompanying
 * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
 */

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

import java.nio.IntBuffer;

public class Main {
    /**
     * Generate a new IntBuffer using the given array of ints. The IntBuffer will be data.length long and contain the
     * int data as data[0], data[1]... etc.
     * 
     * @param data
     *            array of ints to place into a new IntBuffer
     */
    public static IntBuffer createIntBuffer(final int... data) {
        if (data == null) {
            return null;
        }
        final IntBuffer buff = createIntBuffer(data.length);
        buff.clear();
        buff.put(data);
        buff.flip();
        return buff;
    }

    public static IntBuffer createIntBuffer(final IntBuffer reuseStore,
            final int... data) {
        if (data == null) {
            return null;
        }
        final IntBuffer buff;
        if (reuseStore == null || reuseStore.capacity() != data.length) {
            buff = createIntBuffer(data.length);
        } else {
            buff = reuseStore;
            buff.clear();
        }
        buff.clear();
        buff.put(data);
        buff.flip();
        return buff;
    }

    /**
     * Create a new IntBuffer of the specified size.
     * 
     * @param size
     *            required number of ints to store.
     * @return the new IntBuffer
     */
    public static IntBuffer createIntBuffer(final int size) {
        final IntBuffer buf = ByteBuffer.allocateDirect(4 * size)
                .order(ByteOrder.nativeOrder()).asIntBuffer();
        buf.clear();
        return buf;
    }

    /**
     * Create a new IntBuffer of an appropriate size to hold the specified number of ints only if the given buffer if
     * not already the right size.
     * 
     * @param buf
     *            the buffer to first check and rewind
     * @param size
     *            number of ints that need to be held by the newly created buffer
     * @return the requested new IntBuffer
     */
    public static IntBuffer createIntBuffer(IntBuffer buf, final int size) {
        if (buf != null && buf.limit() == size) {
            buf.rewind();
            return buf;
        }

        buf = createIntBuffer(size);
        return buf;
    }
}

Related

  1. createIntBuffer(final int... data)
  2. createIntBuffer(int capacity)
  3. createIntBuffer(int size)
  4. createIntBuffer(int size)