Java ByteBuffer Create createByteBufferOnHeap(int size)

Here you can find the source of createByteBufferOnHeap(int size)

Description

Create a new ByteBuffer of the specified size.

License

Open Source License

Parameter

Parameter Description
size required number of ints to store.

Return

the new IntBuffer

Declaration

public static ByteBuffer createByteBufferOnHeap(int size) 

Method Source Code

//package com.java2s;

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

public class Main {
    /**/*  w  ww. ja v a2s. c o  m*/
     * Create a new ByteBuffer of the specified size.
     *
     * @param size
     *            required number of ints to store.
     * @return the new IntBuffer
     */
    public static ByteBuffer createByteBufferOnHeap(int size) {
        ByteBuffer buf = ByteBuffer.allocate(size).order(
                ByteOrder.nativeOrder());
        buf.clear();
        return buf;
    }

    /**
     * Create a new ByteBuffer 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 bytes that need to be held by the newly created
     *            buffer
     * @return the requested new IntBuffer
     */
    public static ByteBuffer createByteBufferOnHeap(ByteBuffer buf, int size) {
        if (buf != null && buf.limit() == size) {
            buf.rewind();
            return buf;
        }

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

Related

  1. createByteBuffer(int byteCount)
  2. createByteBuffer(int capacity)
  3. createByteBuffer(int capacity)
  4. createByteBuffer(int size)
  5. createByteBuffer(int size)
  6. createCopy(ByteBuffer buffer)
  7. createDirectByteBuffer(byte[] data)
  8. createIconsFromBuffers(List byteBuffers)
  9. createInputStream(final ByteBuffer buf)