@Test void testAddCapacity() { Arrays.asList(BufferUtils.allocateDirect(19), BufferUtils.allocate(19)).forEach(b -> { BufferUtils.flipToFill(b); b.put(BufferUtils.toBuffer("Goodbye Cruel World")); assertEquals(19, b.position()); assertEquals(0, b.remaining()); ByteBuffer b1 = BufferUtils.addCapacity(b, 20); assertEquals(20, b1.remaining()); assertEquals(19, b1.position()); assertEquals(39, b1.capacity()); assertEquals(39, b1.limit()); BufferUtils.flipToFlush(b1, 0); assertEquals("Goodbye Cruel World", BufferUtils.toString(b1).trim()); }); }
public static ByteBuffer addCapacity(ByteBuffer buffer, int capacity) { int srcPos = buffer.position(); int newCapacity = srcPos + capacity; ByteBuffer newBuffer; if (buffer.hasArray()) { newBuffer = ByteBuffer.wrap(Arrays.copyOfRange(buffer.array(), buffer.arrayOffset(), buffer.arrayOffset() + newCapacity)); newBuffer.position(srcPos); } else { newBuffer = BufferUtils.allocateDirect(newCapacity); BufferUtils.flipToFill(newBuffer); BufferUtils.flipToFlush(buffer, 0); newBuffer.put(buffer); } return newBuffer; }
BufferUtils { public static ByteBuffer addCapacity(ByteBuffer buffer, int capacity) { int srcPos = buffer.position(); int newCapacity = srcPos + capacity; ByteBuffer newBuffer; if (buffer.hasArray()) { newBuffer = ByteBuffer.wrap(Arrays.copyOfRange(buffer.array(), buffer.arrayOffset(), buffer.arrayOffset() + newCapacity)); newBuffer.position(srcPos); } else { newBuffer = BufferUtils.allocateDirect(newCapacity); BufferUtils.flipToFill(newBuffer); BufferUtils.flipToFlush(buffer, 0); newBuffer.put(buffer); } return newBuffer; } }
BufferUtils { public static ByteBuffer addCapacity(ByteBuffer buffer, int capacity) { int srcPos = buffer.position(); int newCapacity = srcPos + capacity; ByteBuffer newBuffer; if (buffer.hasArray()) { newBuffer = ByteBuffer.wrap(Arrays.copyOfRange(buffer.array(), buffer.arrayOffset(), buffer.arrayOffset() + newCapacity)); newBuffer.position(srcPos); } else { newBuffer = BufferUtils.allocateDirect(newCapacity); BufferUtils.flipToFill(newBuffer); BufferUtils.flipToFlush(buffer, 0); newBuffer.put(buffer); } return newBuffer; }  }
BufferUtils { public static ByteBuffer addCapacity(ByteBuffer buffer, int capacity) { int srcPos = buffer.position(); int newCapacity = srcPos + capacity; ByteBuffer newBuffer; if (buffer.hasArray()) { newBuffer = ByteBuffer.wrap(Arrays.copyOfRange(buffer.array(), buffer.arrayOffset(), buffer.arrayOffset() + newCapacity)); newBuffer.position(srcPos); } else { newBuffer = BufferUtils.allocateDirect(newCapacity); BufferUtils.flipToFill(newBuffer); BufferUtils.flipToFlush(buffer, 0); newBuffer.put(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); }
BufferUtils { public static ByteBuffer addCapacity(ByteBuffer buffer, int capacity) { int srcPos = buffer.position(); int newCapacity = srcPos + capacity; ByteBuffer newBuffer; if (buffer.hasArray()) { newBuffer = ByteBuffer.wrap(Arrays.copyOfRange(buffer.array(), buffer.arrayOffset(), buffer.arrayOffset() + newCapacity)); newBuffer.position(srcPos); } else { newBuffer = BufferUtils.allocateDirect(newCapacity); BufferUtils.flipToFill(newBuffer); BufferUtils.flipToFlush(buffer, 0); newBuffer.put(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; }