33package com.jogamp.common.nio;
35import java.nio.ByteBuffer;
36import java.nio.CharBuffer;
37import java.nio.DoubleBuffer;
38import java.nio.FloatBuffer;
39import java.nio.IntBuffer;
40import java.nio.LongBuffer;
41import java.nio.ShortBuffer;
71 private final int ALLOCATION_SIZE;
72 private ByteBuffer currentBuffer;
80 ALLOCATION_SIZE = allocationSize;
122 return new SynchronizedCachedBufferFactory();
151 return ALLOCATION_SIZE == -1;
159 return ALLOCATION_SIZE;
165 private void checkIfFixed() {
166 if(ALLOCATION_SIZE == 0) {
167 throw new RuntimeException(
"fixed size buffer factory ran out ouf bounds.");
172 if(
null != currentBuffer) {
173 currentBuffer.clear();
174 currentBuffer =
null;
180 if (size > currentBuffer.capacity()) {
186 if (size > currentBuffer.remaining()) {
191 currentBuffer.limit(currentBuffer.position() + size);
192 final ByteBuffer result = currentBuffer.slice().order(currentBuffer.order());
193 currentBuffer.position(currentBuffer.limit());
194 currentBuffer.limit(currentBuffer.capacity());
308 public boolean equals(
final Object obj) {
312 if (getClass() != obj.getClass()) {
316 if (this.ALLOCATION_SIZE != other.ALLOCATION_SIZE) {
319 if (this.currentBuffer != other.currentBuffer && (
this.currentBuffer ==
null || !
this.currentBuffer.equals(other.currentBuffer))) {
334 private SynchronizedCachedBufferFactory() {
338 private SynchronizedCachedBufferFactory(
final int size,
final int step) {
344 return super.newDirectByteBuffer(size);
Utility methods allowing easy java.nio.Buffer manipulations.
static ByteBuffer newDirectByteBuffer(final int numElements)
Allocates a new direct ByteBuffer with the specified number of elements.
static final int SIZEOF_SHORT
static final int SIZEOF_DOUBLE
static final int SIZEOF_INT
static final int SIZEOF_LONG
static final int SIZEOF_FLOAT
Buffer factory attempting to reduce buffer creation overhead.
LongBuffer newDirectLongBuffer(final long[] values)
IntBuffer newDirectIntBuffer(final int numElements)
ShortBuffer newDirectShortBuffer(final short[] values, final int offset, final int lenght)
ByteBuffer newDirectByteBuffer(final byte[] values, final int offset, final int lenght)
static CachedBufferFactory create()
Creates a factory with initial size and allocation size set to DEFAULT_ALLOCATION_SIZE.
ShortBuffer newDirectShortBuffer(final short[] values)
static CachedBufferFactory createSynchronized(final int initialSize, final boolean fixed)
Synchronized version of create(int, boolean).
IntBuffer newDirectIntBuffer(final int[] values, final int offset, final int lenght)
CharBuffer newDirectCharBuffer(final char[] values)
ByteBuffer newDirectByteBuffer(final byte[] values, final int offset)
boolean equals(final Object obj)
static CachedBufferFactory createSynchronized(final int initialSize, final int allocationSize)
Synchronized version of create(int, int).
int getAllocationSize()
Returns the allocation size used to create new internal buffers.
IntBuffer newDirectIntBuffer(final int[] values)
FloatBuffer newDirectFloatBuffer(final float[] values)
static CachedBufferFactory create(final int initialSize, final boolean fixed)
Creates a factory with the specified initial size.
static CachedBufferFactory createSynchronized()
Synchronized version of create().
static CachedBufferFactory create(final int initialSize, final int allocationSize)
Creates a factory with the specified initial size and allocation size.
boolean isFixed()
Returns true only if this factory does not allow to allocate more buffers as limited by the initial s...
CharBuffer newDirectCharBuffer(final char[] values, final int offset, final int lenght)
LongBuffer newDirectLongBuffer(final long[] values, final int offset, final int lenght)
DoubleBuffer newDirectDoubleBuffer(final double[] values, final int offset, final int lenght)
static final int DEFAULT_ALLOCATION_SIZE
default size for internal buffer allocation.
FloatBuffer newDirectFloatBuffer(final float[] values, final int offset, final int lenght)
ByteBuffer newDirectByteBuffer(final int size)
static CachedBufferFactory createSynchronized(final int initialSize)
Synchronized version of create(int).
LongBuffer newDirectLongBuffer(final long[] values, final int offset)
ByteBuffer newDirectByteBuffer(final byte[] values)
LongBuffer newDirectLongBuffer(final int numElements)
static CachedBufferFactory create(final int initialSize)
Creates a factory with the specified initial size.
FloatBuffer newDirectFloatBuffer(final int numElements)
CharBuffer newDirectCharBuffer(final char[] values, final int offset)
CharBuffer newDirectCharBuffer(final int numElements)
ShortBuffer newDirectShortBuffer(final short[] values, final int offset)
FloatBuffer newDirectFloatBuffer(final float[] values, final int offset)
IntBuffer newDirectIntBuffer(final int[] values, final int offset)
DoubleBuffer newDirectDoubleBuffer(final double[] values)
DoubleBuffer newDirectDoubleBuffer(final int numElements)
DoubleBuffer newDirectDoubleBuffer(final double[] values, final int offset)
ShortBuffer newDirectShortBuffer(final int numElements)