Java ByteBuffer Get getDirectByteBuffer(ByteBuffer var0, boolean var1)

Here you can find the source of getDirectByteBuffer(ByteBuffer var0, boolean var1)

Description

get Direct Byte Buffer

License

LGPL

Declaration

private static ByteBuffer getDirectByteBuffer(ByteBuffer var0, boolean var1) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.lang.ref.Reference;
import java.lang.ref.SoftReference;

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

import java.util.HashMap;

import java.util.Map;

public class Main {
    private static final Map bufferPool = new HashMap();

    private static ByteBuffer getDirectByteBuffer(ByteBuffer var0, boolean var1) {
        if (var0.isDirect()) {
            return var0;
        } else {//w w w .  j a v  a  2 s. co m
            ByteBuffer var2 = var1 ? getPooledBuffer(var0.capacity()) : ByteBuffer.allocateDirect(var0.capacity());
            var2.order(var0.order());
            var2.put(var0);
            var2.flip();
            return var2;
        }
    }

    private static ByteBuffer getPooledBuffer(int var0) {
        Reference var1 = (Reference) bufferPool.get(Integer.valueOf(var0));
        ByteBuffer var2 = var1 == null ? null : (ByteBuffer) var1.get();

        if (var2 == null) {
            var2 = ByteBuffer.allocateDirect(var0);
            bufferPool.put(Integer.valueOf(var0), new SoftReference(var2));
        }

        var2.order(ByteOrder.BIG_ENDIAN);
        var2.position(0);
        return var2;
    }
}

Related

  1. getCRLFCRLFIndex(ByteBuffer buffer)
  2. getCRLFIndex(ByteBuffer buffer)
  3. getCRLFLine(ByteBuffer buf)
  4. getData(ByteBuffer buf, int index, int size)
  5. getDataFromBuffer(ByteBuffer b)
  6. getDouble(ByteBuffer buffer)
  7. getDoubleArray(ByteBuffer buffer, int numDoubles)
  8. getDoubleFromByteBuffer(ByteBuffer data)
  9. getEmptyByteBuffer()