BufferUtils { public static ByteBuffer ensureCapacity(ByteBuffer buffer, int 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()); } else { ByteBuffer newBuffer = allocateDirect(capacity); append(newBuffer, buffer); flipToFill(buffer); return newBuffer; } }  static ByteBuffer allocate(int capacity); static ByteBuffer allocateDirect(int capacity); static void clear(ByteBuffer buffer); static void clearToFill(ByteBuffer buffer); static int flipToFill(ByteBuffer buffer); static void flipToFlush(ByteBuffer buffer, int position); static byte[] toArray(ByteBuffer buffer); static byte[] toArray(Collection<ByteBuffer> buffers); static boolean isTheEmptyBuffer(ByteBuffer buf); static boolean isEmpty(ByteBuffer buf); static boolean hasContent(ByteBuffer buf); static boolean isFull(ByteBuffer buf); static int length(ByteBuffer buffer); static int space(ByteBuffer buffer); static boolean compact(ByteBuffer buffer); static int put(ByteBuffer from, ByteBuffer to); static int flipPutFlip(ByteBuffer from, ByteBuffer to); static void append(ByteBuffer to, byte[] b, int off, int len); static void append(ByteBuffer to, byte b); static int append(ByteBuffer to, ByteBuffer b); static int fill(ByteBuffer to, byte[] b, int off, int len); static void readFrom(File file, ByteBuffer buffer); static void readFrom(InputStream is, int needed, ByteBuffer buffer); static void writeTo(ByteBuffer buffer, OutputStream out); static String toString(ByteBuffer buffer); static String toUTF8String(ByteBuffer buffer); static String toString(ByteBuffer buffer, Charset charset); static String toString(List<ByteBuffer> buffers, Charset charset); static String toString(ByteBuffer buffer, int position, int length, Charset charset); static int toInt(ByteBuffer buffer); static int toInt(ByteBuffer buffer, int position, int length); static int takeInt(ByteBuffer buffer); static long toLong(ByteBuffer buffer); static void putHexInt(ByteBuffer buffer, int n); static void putDecInt(ByteBuffer buffer, int n); static void putDecLong(ByteBuffer buffer, long n); static ByteBuffer toBuffer(int value); static ByteBuffer toBuffer(long value); static ByteBuffer toBuffer(String s); static ByteBuffer toBuffer(String s, Charset charset); static ByteBuffer toBuffer(byte[] array); static ByteBuffer toBuffer(byte[] array, int offset, int length); static ByteBuffer toDirectBuffer(String s); static ByteBuffer toDirectBuffer(String s, Charset charset); static ByteBuffer toMappedBuffer(File file); @Deprecated static boolean isMappedBuffer(ByteBuffer buffer); static String toSummaryString(ByteBuffer buffer); static String toDetailString(ByteBuffer[] buffer); static String toIDString(ByteBuffer buffer); static String toDetailString(ByteBuffer buffer); static String toHexSummary(ByteBuffer buffer); static String toHexString(ByteBuffer buffer); static void putCRLF(ByteBuffer buffer); static boolean isPrefix(ByteBuffer prefix, ByteBuffer buffer); static ByteBuffer ensureCapacity(ByteBuffer buffer, int capacity); static ByteBuffer addCapacity(ByteBuffer buffer, int capacity); static ByteBuffer merge(List<ByteBuffer> buffers); static final ByteBuffer EMPTY_BUFFER; }
@Test void testEnsureCapacity() { ByteBuffer b = BufferUtils.toBuffer("Goodbye Cruel World"); assertSame(b, BufferUtils.ensureCapacity(b, 0)); assertSame(b, BufferUtils.ensureCapacity(b, 10)); assertSame(b, BufferUtils.ensureCapacity(b, b.capacity())); ByteBuffer b1 = BufferUtils.ensureCapacity(b, 64); assertNotSame(b, b1); assertEquals(64, b1.capacity()); assertEquals("Goodbye Cruel World", BufferUtils.toString(b1)); b1.position(8); b1.limit(13); assertEquals("Cruel", BufferUtils.toString(b1)); ByteBuffer b2 = b1.slice(); assertEquals("Cruel", BufferUtils.toString(b2)); System.err.println(BufferUtils.toDetailString(b2)); assertEquals(8, b2.arrayOffset()); assertEquals(5, b2.capacity()); assertSame(b2, BufferUtils.ensureCapacity(b2, 5)); ByteBuffer b3 = BufferUtils.ensureCapacity(b2, 64); assertNotSame(b2, b3); assertEquals(64, b3.capacity()); assertEquals("Cruel", BufferUtils.toString(b3)); assertEquals(0, b3.arrayOffset()); assertEquals(19, b.remaining()); b.position(19); ByteBuffer b4 = BufferUtils.ensureCapacity(b, b.position() + 20); BufferUtils.flipToFill(b4); b4.position(b.position()); assertEquals(20, b4.remaining()); assertEquals(19, b4.position()); assertEquals(39, b4.capacity()); BufferUtils.flipToFill(b); ByteBuffer b5 = BufferUtils.allocateDirect(19); assertEquals(0, b5.remaining()); assertEquals(19, b.remaining()); BufferUtils.append(b5, b); assertEquals(0, b.remaining()); assertEquals(19, b5.remaining()); BufferUtils.flipToFill(b); assertEquals(19, b.remaining()); ByteBuffer b6 = BufferUtils.ensureCapacity(b5, 20); assertEquals(19, b6.remaining()); BufferUtils.flipToFill(b6); assertEquals(1, b6.remaining()); }