Example usage for java.nio Buffer capacity

List of usage examples for java.nio Buffer capacity

Introduction

In this page you can find the example usage for java.nio Buffer capacity.

Prototype

int capacity

To view the source code for java.nio Buffer capacity.

Click Source Link

Document

The capacity of this buffer, which never change.

Usage

From source file:Main.java

private static void println(Buffer buffer) {
    System.out.println("pos=" + buffer.position() + ", limit=" + buffer.limit() + ", capacity="
            + buffer.capacity() + ": '" + buffer.toString() + "'");
}

From source file:Main.java

/**
 * Creates an int array from the provided {@link IntBuffer} or {@link ShortBuffer}.
 * /*from w ww.  j a  v  a2  s. c om*/
 * @param buffer {@link Buffer} the data source. Should be either a {@link IntBuffer} 
 * or {@link ShortBuffer}.
 * @return int array containing the data of the buffer.
 */
public static int[] getIntArrayFromBuffer(Buffer buffer) {
    int[] array = null;
    if (buffer.hasArray()) {
        array = (int[]) buffer.array();
    } else {
        buffer.rewind();
        array = new int[buffer.capacity()];
        if (buffer instanceof IntBuffer) {
            ((IntBuffer) buffer).get(array);
        } else if (buffer instanceof ShortBuffer) {
            int count = 0;
            while (buffer.hasRemaining()) {
                array[count] = (int) (((ShortBuffer) buffer).get());
                ++count;
            }
        }
    }
    return array;
}

From source file:com.linkedin.pinot.common.utils.MmapUtils.java

/**
 * Unloads a byte buffer from memory//from w  w  w  .j a va2 s .c o m
 *
 * @param buffer The buffer to unload, can be null
 */
public static void unloadByteBuffer(Buffer buffer) {
    if (null == buffer)
        return;

    // Non direct byte buffers do not require any special handling, since they're on heap
    if (!buffer.isDirect())
        return;

    // Remove usage from direct byte buffer usage
    final int bufferSize = buffer.capacity();
    final AllocationContext bufferContext = BUFFER_TO_CONTEXT_MAP.get(buffer);

    // A DirectByteBuffer can be cleaned up by doing buffer.cleaner().clean(), but this is not a public API. This is
    // probably not portable between JVMs.
    try {
        Method cleanerMethod = buffer.getClass().getMethod("cleaner");
        Method cleanMethod = Class.forName("sun.misc.Cleaner").getMethod("clean");

        cleanerMethod.setAccessible(true);
        cleanMethod.setAccessible(true);

        // buffer.cleaner().clean()
        Object cleaner = cleanerMethod.invoke(buffer);
        if (cleaner != null) {
            cleanMethod.invoke(cleaner);

            if (bufferContext != null) {
                switch (bufferContext.allocationType) {
                case DIRECT_BYTE_BUFFER:
                    DIRECT_BYTE_BUFFER_USAGE.addAndGet(-bufferSize);
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(
                                "Releasing byte buffer of size {} with context {}, allocation after operation {}",
                                bufferSize, bufferContext, getTrackedAllocationStatus());
                    }
                    break;
                case MMAP:
                    MMAP_BUFFER_USAGE.addAndGet(-bufferSize);
                    MMAP_BUFFER_COUNT.decrementAndGet();
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(
                                "Unmapping byte buffer of size {} with context {}, allocation after operation {}",
                                bufferSize, bufferContext, getTrackedAllocationStatus());
                    }
                    break;
                }
                BUFFER_TO_CONTEXT_MAP.remove(buffer);
            } else {
                LOGGER.warn(
                        "Attempted to release byte buffer of size {} with no context, no deallocation performed.",
                        bufferSize);
                if (LOGGER.isDebugEnabled()) {
                    List<String> matchingAllocationContexts = new ArrayList<>();

                    synchronized (BUFFER_TO_CONTEXT_MAP) {
                        clearSynchronizedMapEntrySetCache();

                        for (Map.Entry<ByteBuffer, AllocationContext> byteBufferAllocationContextEntry : BUFFER_TO_CONTEXT_MAP
                                .entrySet()) {
                            if (byteBufferAllocationContextEntry.getKey().capacity() == bufferSize) {
                                matchingAllocationContexts
                                        .add(byteBufferAllocationContextEntry.getValue().toString());
                            }
                        }

                        // Clear the entry set cache afterwards so that we don't hang on to stale entries
                        clearSynchronizedMapEntrySetCache();
                    }

                    LOGGER.debug("Contexts with a size of {}: {}", bufferSize, matchingAllocationContexts);
                    LOGGER.debug("Called by: {}", Utils.getCallingMethodDetails());
                }
            }
        }
    } catch (Exception e) {
        LOGGER.warn("Caught (ignored) exception while unloading byte buffer", e);
    }
}