Android ByteBuffer Create newUnsafeByteBuffer(ByteBuffer buffer)

Here you can find the source of newUnsafeByteBuffer(ByteBuffer buffer)

Description

Registers the given ByteBuffer as an unsafe ByteBuffer.

License

Apache License

Parameter

Parameter Description
buffer the ByteBuffer to register

Return

the ByteBuffer passed to the method

Declaration

public static ByteBuffer newUnsafeByteBuffer(ByteBuffer buffer) 

Method Source Code

/*******************************************************************************
 * Copyright 2011 See AUTHORS file./* w  w w  .  j  av a  2s.  c o m*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;

public class Main{
    static Array<ByteBuffer> unsafeBuffers = new Array<ByteBuffer>();
    static int allocatedUnsafe = 0;
    /** Allocates a new direct ByteBuffer from native heap memory using the native byte order. Needs to be disposed with
     * {@link #freeMemory(ByteBuffer)}.
     * @param numBytes */
    public static ByteBuffer newUnsafeByteBuffer(int numBytes) {
        ByteBuffer buffer = newDisposableByteBuffer(numBytes);
        buffer.order(ByteOrder.nativeOrder());
        allocatedUnsafe += numBytes;
        synchronized (unsafeBuffers) {
            unsafeBuffers.add(buffer);
        }
        return buffer;
    }
    /**
     * Registers the given ByteBuffer as an unsafe ByteBuffer. The ByteBuffer must have been 
     * allocated in native code, pointing to a memory region allocated via malloc. Needs to 
     * be disposed with {@link #freeMemory(ByteBuffer)}.
     * @param buffer the {@link ByteBuffer} to register
     * @return the ByteBuffer passed to the method
     */
    public static ByteBuffer newUnsafeByteBuffer(ByteBuffer buffer) {
        allocatedUnsafe += buffer.capacity();
        synchronized (unsafeBuffers) {
            unsafeBuffers.add(buffer);
        }
        return buffer;
    }
    private static native ByteBuffer newDisposableByteBuffer(int numBytes);
}

Related

  1. copy(float[] src, ByteBuffer dst, int numFloats, int offset)
  2. newByteBuffer(int numBytes)
  3. newByteBuffer(int numBytes)
  4. newUnsafeByteBuffer(int numBytes)
  5. createByteBuffer(byte[] data)
  6. createByteBuffer(int capacity)
  7. createByteBuffer(int count)