集册 Java实例教程 从源到目标复制尽可能多的字节,直到任何一个源没有更多的数据,目标都不能获取更多

从源到目标复制尽可能多的字节,直到任何一个源没有更多的数据,目标都不能获取更多

欢马劈雪     最近更新时间:2020-01-02 10:19:05

405
从源到目标复制尽可能多的字节,直到任何一个源没有更多的数据,目标都不能获取更多
/**来自 时 代 J a v a 公 众 号**/


//package com.nowjava;

import java.nio.ByteBuffer;


public class Main {

    /**

     * Copy as many byte from source to target, until either source has no more data, target cannot take more or 

     * the trg.position() is equals to limit value.

     */

    public static void transferUntilTargetPos(ByteBuffer src,

            ByteBuffer trg, int trgPos) {

        int cpySz = Math.min(src.remaining(),

                Math.min(trgPos - trg.position(), trg.remaining()));

        if (cpySz <= 0) {

            return;

        
展开阅读全文