Here you can find the source of copy(ByteBuffer source, int byteCount)
public static ByteBuffer copy(ByteBuffer source, int byteCount)
//package com.java2s; //License from project: Open Source License import java.nio.ByteBuffer; import java.nio.ByteOrder; public class Main { private static final ByteOrder NATIVE_BYTE_ORDER = ByteOrder.nativeOrder(); public static ByteBuffer copy(ByteBuffer source, int byteCount) { ByteBuffer duplicate = source.duplicate(); duplicate.position(0);//from w ww . j a v a 2s. c o m int copyLimit = Math.min(duplicate.capacity(), byteCount); duplicate.limit(copyLimit); ByteBuffer copy = createByteBuffer(byteCount); copy.put(duplicate); copy.position(0); return copy; } public static ByteBuffer createByteBuffer(int byteCount) { return ByteBuffer.allocate(byteCount).order(NATIVE_BYTE_ORDER); } }