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

voidcopyByteBuffer(ByteBuffer srcBuf, int srcStep, ByteBuffer dstBuf, int dstStep)
copy Byte Buffer
assert srcBuf != dstBuf;
int channels = 3;
int w = Math.min(srcStep, dstStep);
int srcLine = srcBuf.position(), dstLine = dstBuf.position();
byte[] buffer = new byte[channels];
while (srcLine < srcBuf.capacity() && dstLine < dstBuf.capacity()) {
    srcBuf.position(srcLine);
    dstBuf.position(dstLine);
...
ByteBuffercopyByteBuffer(final ByteBuffer pBuffer)
Clone a byte buffer.
if (pBuffer.isDirect()) {
    throw new IllegalArgumentException("direct buffers not supported");
final ByteBuffer buffer = ByteBuffer.allocate(pBuffer.limit());
buffer.order(pBuffer.order());
buffer.put(pBuffer.array());
buffer.position(pBuffer.position());
return buffer;
...
java.nio.ByteBuffercopyByteBuffer(java.nio.ByteBuffer data)
copy Byte Buffer
if (data == null) {
    return null;
java.nio.ByteBuffer buf = createDirectByteBuffer(convertByteBufferToArray(data));
return buf;
byte[]copyBytes(ByteBuffer bytes)
Extract the bytes in the given ByteBuffer and return it as a byte[] without affecting the mark, position, or limit of the given buffer.
ByteBuffer copy = bytes.duplicate();
byte[] result = new byte[copy.remaining()]; 
copy.get(result);
return result;
intcopyBytes(ByteBuffer src, ByteBuffer dst, int length)
Copies data from src buffer to dst buffer using their current positions as offsets.
int retVal = copyBytes(src, src.position(), dst, dst.position(), length);
src.position(src.position() + retVal);
dst.position(dst.position() + retVal);
return retVal;
byte[]copyBytesFrom(ByteBuffer bb)
Returns a copy of the bytes from the given ByteBuffer, ranging from the the buffer's current position to the buffer's limit; or null if the input is null.
if (bb == null) {
    return null;
if (bb.hasArray()) {
    return Arrays.copyOfRange(bb.array(), bb.arrayOffset() + bb.position(), bb.arrayOffset() + bb.limit());
byte[] dst = new byte[bb.remaining()];
bb.asReadOnlyBuffer().get(dst);
...
byte[]copyContentsToArray(ByteBuffer src)
Copy the remaining content of a ByteBuffer in to a new array.
return copyContentsToArray(src, true);
voidcopyFileByMappedByteBuffer(String srcFileName, String dstFileName)
copy File By Mapped Byte Buffer
FileChannel inFileChannel = new RandomAccessFile(srcFileName, "r").getChannel();
FileChannel outFileChannel = new RandomAccessFile(dstFileName, "rw").getChannel();
try {
    long fileSize = inFileChannel.size();
    long position = 0;
    while (position < fileSize) {
        long copyFileSize = Math.min((fileSize - position), COPY_FILE_SIZE);
        MappedByteBuffer mappedByteBuffer = outFileChannel.map(MapMode.READ_WRITE, position, copyFileSize);
...
voidcopyFromBufferToBuffer(ByteBuffer out, ByteBuffer in, int sourceOffset, int length)
Copy from one buffer to another from given offset.
if (in.hasArray() && out.hasArray()) {
    System.arraycopy(in.array(), sourceOffset + in.arrayOffset(), out.array(),
            out.position() + out.arrayOffset(), length);
    skip(out, length);
} else {
    for (int i = 0; i < length; ++i) {
        out.put(in.get(sourceOffset + i));
voidcopyFromBufferToBuffer(ByteBuffer out, ByteBuffer in, int sourceOffset, int length)
Copy from one buffer to another from given offset
if (in.hasArray() && out.hasArray()) {
    System.arraycopy(in.array(), sourceOffset + in.arrayOffset(), out.array(),
            out.position() + out.arrayOffset(), length);
    skip(out, length);
} else {
    for (int i = 0; i < length; ++i) {
        out.put(in.get(sourceOffset + i));