List of utility methods to do ByteBuffer Capacity
ByteBuffer | borrowByteBuffer(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(); ... |
ByteBuffer | ensureCapacity(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); |
ByteBuffer | ensureCapacity(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()); ... |
ByteBuffer | ensureCapacity(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; |
ByteBuffer | ensureCapacity(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); ... |
ByteBuffer | expand(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); ... |
ByteBuffer | getByteBuffer(int capacity) get Byte Buffer ByteBuffer b = ByteBuffer.allocate(capacity);
b.order(ByteOrder.LITTLE_ENDIAN);
return b;
|
ByteBuffer | grow(ByteBuffer buffer, int minCapacityIncrease) grow ByteBuffer tmp = ByteBuffer
.allocate(Math.max(buffer.capacity() << 1, buffer.capacity() + minCapacityIncrease));
buffer.flip();
tmp.put(buffer);
return tmp;
|
ByteBuffer | growBuffer(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; |
ByteBuffer | increaseCapacity(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); ... |