Java ByteBuffer Copy copyBytes(ByteBuffer src, ByteBuffer dst, int length)

Here you can find the source of copyBytes(ByteBuffer src, ByteBuffer dst, int length)

Description

Copies data from src buffer to dst buffer using their current positions as offsets.

License

Open Source License

Declaration

public static int copyBytes(ByteBuffer src, ByteBuffer dst, int length) 

Method Source Code

//package com.java2s;
// See LICENSE.txt for license information

import java.nio.ByteBuffer;

public class Main {
    /**/* w w  w. j  a va  2 s.  co m*/
     * Copies data from {@code src} buffer to {@code dst} buffer using their current positions as offsets.
     * Current positions of byte buffers will be advanted to the end of copied data.
     */
    public static int copyBytes(ByteBuffer src, ByteBuffer dst, int length) {
        int retVal = copyBytes(src, src.position(), dst, dst.position(), length);
        src.position(src.position() + retVal);
        dst.position(dst.position() + retVal);
        return retVal;
    }

    /**
     * Copies data from {@code src} buffer to {@code dst} buffer using the specified offsets.
     * Current positions of byte buffers are unaffected.
     */
    public static int copyBytes(ByteBuffer src, int srcOffset, ByteBuffer dst, int dstOffset, int length) {
        int srcPos = src.position();
        int dstPos = dst.position();
        int maxLength = 0;
        try {
            src.position(srcOffset);
            dst.position(dstOffset);
            maxLength = Math.min(length, Math.min(src.remaining(), dst.remaining()));
            ByteBuffer bufTmp = src.duplicate(); // to preserve limit
            bufTmp.limit(bufTmp.position() + maxLength);
            dst.put(bufTmp);
        } catch (Throwable t) {
            t.printStackTrace();
        } finally {
            src.position(srcPos);
            dst.position(dstPos);
        }
        return maxLength;
    }
}

Related

  1. copyByteBuffer(ByteBuffer src)
  2. copyByteBuffer(ByteBuffer srcBuf, int srcStep, ByteBuffer dstBuf, int dstStep)
  3. copyByteBuffer(final ByteBuffer pBuffer)
  4. copyByteBuffer(java.nio.ByteBuffer data)
  5. copyBytes(ByteBuffer bytes)
  6. copyBytesFrom(ByteBuffer bb)
  7. copyContentsToArray(ByteBuffer src)
  8. copyFileByMappedByteBuffer(String srcFileName, String dstFileName)
  9. copyFromBufferToBuffer(ByteBuffer out, ByteBuffer in, int sourceOffset, int length)