Here you can find the source of copy(ByteBuffer src, ByteBuffer dst)
public static int copy(ByteBuffer src, ByteBuffer dst)
//package com.java2s; /*/* w w w . jav a2 s . co m*/ * JLibs: Common Utilities for Java * Copyright (C) 2009 Santhosh Kumar T <santhosh.tekuri@gmail.com> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. */ import java.nio.ByteBuffer; public class Main { public static int copy(ByteBuffer src, ByteBuffer dst) { int srcRemaining = src.remaining(); int dstRemaining = dst.remaining(); if (srcRemaining <= dstRemaining) { dst.put(src); return srcRemaining; } int srcLimit = src.limit(); src.limit(src.position() + dstRemaining); dst.put(src); src.limit(srcLimit); return dstRemaining; } public static int copy(ByteBuffer src, ByteBuffer dsts[], int offset, int length) { int read = 0; while (src.hasRemaining() && length > 0) { read += copy(src, dsts[offset]); ++offset; --length; } return read; } }