I’m slightly puzzled why we have so much put/get methods in ByteBuffer, yet ByteBuffer.get(ByteBuffer) is missing.
(And slightly puzzled why I only ran into this after so many years…)
We have:
ByteBuffer.put(byte[])
ByteBuffer.put(byte[], off, len)
ByteBuffer.put(ByteBuffer)
ByteBuffer.get(byte[])
ByteBuffer.get(byte[], off, len)
----- where is ByteBuffer.get(ByteBuffer)
Here’s my own workaround:
public static void put(ByteBuffer dst, ByteBuffer src)
{
dst.put(src); // oh the simplicity!
}
public static void get(ByteBuffer dst, ByteBuffer src)
{
if(src.remaining() == dst.remaining())
{
dst.put(src);
return;
}
if (src.remaining() < dst.remaining())
throw new BufferUnderflowException();
// what this effectivly does:
//
// while (dst.hasRemaining()) // dst, not src - like BB.put(BB)
// dst.put(src.get());
int pos = src.position();
int lim = src.limit();
int rem = dst.remaining();
// reduce the visible bytes, to the range we are about to copy
src.limit(pos + rem);
// copy bytes
dst.put(src);
// restore the visible bytes
src.limit(lim);
}
So what on earth am I missing, or… why is that method missing? 
???