Java Utililty Methods ByteBuffer Capacity

List of utility methods to do ByteBuffer Capacity

Description

The list of methods to do ByteBuffer Capacity are organized into topic(s).

Method

ByteBufferborrowByteBuffer(final int capacity)
borrow Byte Buffer
if (capacity >= MAX_LINE_BYTES_VERY_LARGE) {
    return ByteBuffer.allocate(capacity);
} else if (capacity >= MAX_LINE_BYTES_LARGE) {
    return borrowByteBufferVeryLarge();
} else if (capacity >= MAX_LINE_BYTES_MEDIUM) {
    return borrowByteBufferLarge();
} else if (capacity >= MAX_LINE_BYTES_NORMAL) {
    return borrowByteBufferMedium();
...
ByteBufferensureCapacity(ByteBuffer buff, int len)
Ensure the byte buffer has the given capacity, plus 1 KB.
len += 1024;
if (buff.remaining() > len) {
    return buff;
return grow(buff, len);
ByteBufferensureCapacity(ByteBuffer buffer, int capacity)
ensure Capacity
if (buffer == null)
    return allocate(capacity);
if (buffer.capacity() >= capacity)
    return buffer;
if (buffer.hasArray())
    return ByteBuffer.wrap(
            Arrays.copyOfRange(buffer.array(), buffer.arrayOffset(), buffer.arrayOffset() + capacity),
            buffer.position(), buffer.remaining());
...
ByteBufferensureCapacity(ByteBuffer existingBuffer, int newLength)
Make sure that the ByteBuffer capacity is equal to or greater than the expected length.
if (newLength > existingBuffer.capacity()) {
    ByteBuffer newBuffer = ByteBuffer.allocate(newLength);
    existingBuffer.flip();
    newBuffer.put(existingBuffer);
    return newBuffer;
return existingBuffer;
ByteBufferensureCapacity(ByteBuffer original, int newCapacity)
ensure Capacity
if (newCapacity <= original.capacity()) {
    return original;
int position = original.position();
int limit = original.limit();
original.clear();
ByteBuffer newBuffer = ByteBuffer.allocate(newCapacity);
newBuffer.put(original);
...
ByteBufferexpand(ByteBuffer buffer, int newCapacity)
If we have no more room in the current buffer, then double our capacity and copy the current buffer to the new one.
if (newCapacity < buffer.capacity())
    throw new IllegalArgumentException("newCapacity (" + newCapacity
            + ") must be larger than existing capacity (" + buffer.capacity() + ")");
ByteBuffer newBuffer = ByteBuffer.allocate(newCapacity);
int position = buffer.position();
buffer.rewind();
newBuffer.put(buffer);
newBuffer.position(position);
...
ByteBuffergetByteBuffer(int capacity)
get Byte Buffer
ByteBuffer b = ByteBuffer.allocate(capacity);
b.order(ByteOrder.LITTLE_ENDIAN);
return b;
ByteBuffergrow(ByteBuffer buffer, int minCapacityIncrease)
grow
ByteBuffer tmp = ByteBuffer
        .allocate(Math.max(buffer.capacity() << 1, buffer.capacity() + minCapacityIncrease));
buffer.flip();
tmp.put(buffer);
return tmp;
ByteBuffergrowBuffer(ByteBuffer b, int newCapacity)
Grow a byte buffer, so it has a minimal capacity or at least the double capacity of the original buffer
b.limit(b.position());
b.rewind();
int c2 = b.capacity() * 2;
ByteBuffer on = ByteBuffer.allocate(c2 < newCapacity ? newCapacity : c2);
on.put(b);
return on;
ByteBufferincreaseCapacity(ByteBuffer buffer, int size)
Increase ByteBuffer's capacity.
if (buffer == null)
    throw new IllegalArgumentException("buffer is null");
if (size < 0)
    throw new IllegalArgumentException("size less than 0");
int capacity = buffer.capacity() + size;
ByteBuffer result = allocate(capacity, buffer.isDirect());
buffer.flip();
result.put(buffer);
...