Java Utililty Methods ByteBuffer Copy

List of utility methods to do ByteBuffer Copy

Description

The list of methods to do ByteBuffer Copy are organized into topic(s).

Method

voidcopyFromStreamToBuffer(ByteBuffer out, DataInputStream in, int length)
Copy the given number of bytes from the given stream and put it at the current position of the given buffer, updating the position in the buffer.
if (out.hasArray()) {
    in.readFully(out.array(), out.position() + out.arrayOffset(), length);
    skip(out, length);
} else {
    for (int i = 0; i < length; ++i) {
        out.put(in.readByte());
ByteBuffercopyOf(ByteBuffer payload)
Make a copy of a byte buffer.
if (payload == null)
    return null;
ByteBuffer copy = ByteBuffer.allocate(payload.remaining());
copy.put(payload.slice());
copy.flip();
return copy;
voidcopyRemaining(ByteBuffer src, ByteBuffer dst)
Copies as much as possible
int n = Math.min(src.remaining(), dst.remaining());
copy(src, dst, n);
voidcopyTo(ByteBuffer original, ByteBuffer copy)
copy To
original.rewind();
copy.put(original);
original.rewind();
byte[]copyToArray(@Nonnull ByteBuffer data)
copy To Array
byte[] copy = new byte[data.remaining()];
data.duplicate().get(copy);
return copy;
intcopyToHeapBuffer(ByteBuffer src, ByteBuffer dest)
copy To Heap Buffer
int n = Math.min(src.remaining(), dest.remaining());
if (n > 0) {
    int srcPosition = src.position();
    int destPosition = dest.position();
    int ixSrc = srcPosition + src.arrayOffset();
    int ixDest = destPosition + dest.arrayOffset();
    System.arraycopy(src.array(), ixSrc, dest.array(), ixDest, n);
    src.position(srcPosition + n);
...
voidcopyToStream(ByteBuffer byteBuffer, OutputStream os)
copy To Stream
byte[] buffer = new byte[32768];
while (true) {
    int byteCount = Math.min(byteBuffer.remaining(), buffer.length);
    if (byteCount == 0) {
        break;
    byteBuffer.get(buffer, 0, byteCount);
    os.write(buffer, 0, byteCount);
...
voidcopyWholeFrame(ByteBuffer srcFrame, ByteBuffer destFrame)
copy Whole Frame
srcFrame.clear();
destFrame.clear();
destFrame.put(srcFrame);
ByteBufferdeepCopy(ByteBuffer orig)
deep Copy
int pos = orig.position(), lim = orig.limit();
try {
    orig.position(0).limit(orig.capacity()); 
    ByteBuffer toReturn = deepCopyVisible(orig); 
    toReturn.position(pos).limit(lim); 
    return toReturn;
} finally { 
    orig.position(pos).limit(lim); 
...
byte[]deepCopyByteBufferToByteArray(ByteBuffer byteBuffer)
deep Copy Byte Buffer To Byte Array
byte[] result = new byte[byteBuffer.remaining()];
System.arraycopy(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(), result, 0,
        byteBuffer.remaining());
return result;