com.anaptecs.jeaf.workload.proxy.PoolableByteBuffer Maven / Gradle / Ivy
The newest version!
/**
* Copyright 2004 - 2021 anaptecs GmbH, Burgstr. 96, 72764 Reutlingen, Germany
*
* All rights reserved.
*/
package com.anaptecs.jeaf.workload.proxy;
import java.nio.ByteBuffer;
import java.util.Arrays;
import com.anaptecs.jeaf.xfun.api.checks.Check;
import stormpot.Poolable;
import stormpot.Slot;
public class PoolableByteBuffer implements Poolable {
/**
* Location inside a pool where this instance is stored.
*/
private final Slot slot;
private final ByteBuffer byteBuffer;
public PoolableByteBuffer( int pSize, Slot pSlot ) {
byteBuffer = ByteBuffer.allocate(pSize);
slot = pSlot;
}
public PoolableByteBuffer( ByteBuffer pByteBuffer, Slot pSlot ) {
// Check parameter.
Check.checkInvalidParameterNull(pByteBuffer, "pByteBuffer");
byteBuffer = pByteBuffer;
slot = pSlot;
}
public ByteBuffer getByteBuffer( ) {
return byteBuffer;
}
public Slot getSlot( ) {
return slot;
}
@Override
public void release( ) {
// Clear content. For security reasons we also have to clear the content of the byte array even though it might cost
// some very small amount of time.
Arrays.fill(byteBuffer.array(), (byte) 0);
byteBuffer.clear();
// Return byte array back to pool
if (slot != null) {
slot.release(this);
}
}
}