BufferUtils { public static int put(ByteBuffer from, ByteBuffer to) { int put; int remaining = from.remaining(); if (remaining > 0) { if (remaining <= to.remaining()) { to.put(from); put = remaining; from.position(from.limit()); } else if (from.hasArray()) { put = to.remaining(); to.put(from.array(), from.arrayOffset() + from.position(), put); from.position(from.position() + put); } else { put = to.remaining(); ByteBuffer slice = from.slice(); slice.limit(put); to.put(slice); from.position(from.position() + put); } } else { put = 0; } return put; }  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 testPut() { ByteBuffer to = BufferUtils.allocate(10); ByteBuffer from = BufferUtils.toBuffer("12345"); BufferUtils.clear(to); assertEquals(5, BufferUtils.append(to, from)); assertTrue(BufferUtils.isEmpty(from)); assertEquals("12345", BufferUtils.toString(to)); from = BufferUtils.toBuffer("XX67890ZZ"); from.position(2); assertEquals(5, BufferUtils.append(to, from)); assertEquals(2, from.remaining()); assertEquals("1234567890", BufferUtils.toString(to)); from = BufferUtils.toBuffer("1234"); to = BufferUtils.allocate(from.remaining() * 2); assertEquals(0, to.position()); assertEquals(0, to.limit()); assertEquals(0, from.position()); assertEquals(4, from.limit()); BufferUtils.append(to, from); assertEquals(0, to.position()); assertEquals(4, to.limit()); assertEquals(4, from.position()); assertEquals(4, from.limit()); to.get(); from = BufferUtils.toBuffer("1234"); BufferUtils.append(to, from); assertEquals(1, to.position()); assertEquals(8, to.limit()); }