Java ByteBuffer Set releaseTemporaryDirectBuffer(ByteBuffer buf)

Here you can find the source of releaseTemporaryDirectBuffer(ByteBuffer buf)

Description

release Temporary Direct Buffer

License

Open Source License

Declaration

static void releaseTemporaryDirectBuffer(ByteBuffer buf) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.lang.ref.SoftReference;
import java.nio.ByteBuffer;

public class Main {
    private static final int TEMP_BUF_POOL_SIZE = 3;
    private static ThreadLocal<SoftReference<ByteBuffer>>[] bufferPool;

    static void releaseTemporaryDirectBuffer(ByteBuffer buf) {
        if (buf == null)
            return;
        // Put it in an empty slot if such exists
        for (int i = 0; i < TEMP_BUF_POOL_SIZE; i++) {
            SoftReference<ByteBuffer> ref = (bufferPool[i].get());
            if ((ref == null) || (ref.get() == null)) {
                bufferPool[i].set(new SoftReference<>(buf));
                return;
            }//  w  w  w .  j  a va 2s  .co m
        }
        // Otherwise replace a smaller one in the cache if such exists
        for (int i = 0; i < TEMP_BUF_POOL_SIZE; i++) {
            SoftReference ref = (bufferPool[i].get());
            ByteBuffer inCacheBuf = (ByteBuffer) ref.get();
            if ((inCacheBuf == null) || (buf.capacity() > inCacheBuf.capacity())) {
                bufferPool[i].set(new SoftReference<>(buf));
                return;
            }
        }
    }
}

Related

  1. readKey(ByteBuffer index, int indexByteOffset, byte[] foundKey)
  2. readPackageName(ByteBuffer buffer, int offset)
  3. redByteArray(ByteBuffer buffer, int offset, int length)
  4. redInteger1(ByteBuffer buffer, int _offset)
  5. redString(ByteBuffer buffer, int offset, Charset charset)
  6. reset(int offset, ByteBuffer b)
  7. resetBit(ByteBuffer in, int pos)
  8. set0byte(MappedByteBuffer mem, byte value)
  9. set24BitInt(ByteBuffer buffer, int value)