Copy as many byte from source to target, until either source has no more data, target cannot take more - Java File Path IO

Java examples for File Path IO:Byte Array

Description

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

Demo Code


//package com.java2s;
import java.nio.ByteBuffer;

public class Main {
    /**/*from ww w  . j a  v  a 2  s  .c o  m*/
     * 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;
        }
        int oldLimit = src.limit();
        src.limit(src.position() + cpySz);
        trg.put(src);
        src.limit(oldLimit);
    }
}

Related Tutorials