Java IntBuffer Create createIntBuffer(int... data)

Here you can find the source of createIntBuffer(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(int... data) 

Method Source Code

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

import java.nio.IntBuffer;

import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;

public class Main {
    private static final Map<Buffer, Object> trackingHash = new ConcurrentHashMap<>(
            new WeakHashMap<Buffer, Object>());
    private static final Object ref = new Object();
    private static final boolean trackDirectMemory = false;

    /**//from  w w w. j  a  va 2 s. c  o m
     * 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(int... data) {
        if (data == null)
            return null;
        IntBuffer buff = createIntBuffer(data.length);
        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(int size) {
        IntBuffer buf = ByteBuffer.allocateDirect(4 * size)
                .order(ByteOrder.nativeOrder()).asIntBuffer();
        buf.clear();
        if (trackDirectMemory) {
            trackingHash.put(buf, ref);
        }
        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, 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)
  5. createIntBuffer(int size)
  6. createIntBuffer(int[] array)
  7. createIntBufferOnHeap(final int size)
  8. newDirectIntBuffer(final int theSize)
  9. newDirectIntBuffer(final int theSize)