Here you can find the source of newUnsafeByteBuffer(ByteBuffer buffer)
Parameter | Description |
---|---|
buffer | the ByteBuffer to register |
public static ByteBuffer newUnsafeByteBuffer(ByteBuffer buffer)
/******************************************************************************* * 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); }