Here you can find the source of copyBytes(ByteBuffer src, ByteBuffer dst, int length)
public static int copyBytes(ByteBuffer src, ByteBuffer dst, int length)
//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; } }