Java FileChannel Copy copyChannel(ReadableByteChannel in, long length, FileChannel out)

Here you can find the source of copyChannel(ReadableByteChannel in, long length, FileChannel out)

Description

copy Channel

License

Open Source License

Declaration

public static long copyChannel(ReadableByteChannel in, long length, FileChannel out) throws IOException 

Method Source Code


//package com.java2s;
/*/* w  ww .ja  v  a  2s.co  m*/
 * This file is part of the Jose Project
 * see http://jose-chess.sourceforge.net/
 * (c) 2002-2006 Peter Sch?fer
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 */

import java.io.*;

import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

public class Main {
    public static long copyChannel(FileChannel in, long length, WritableByteChannel out) throws IOException {
        if (length < 0L || length >= Integer.MAX_VALUE)
            return in.transferTo(0L, length, out);

        long result = 0L;
        while (result < length)
            result += in.transferTo(result, length - result, out);
        return result;
    }

    public static long copyChannel(ReadableByteChannel in, long length, FileChannel out) throws IOException {
        if (length < 0L || length >= Integer.MAX_VALUE)
            return out.transferFrom(in, 0L, length);

        long result = 0L;
        while (result < length)
            result += out.transferFrom(in, result, length - result);
        return result;
    }
}

Related

  1. copy12(File file1, File file2)
  2. copy2(String sourceFileName, String destFileName)
  3. copyAFile(File source, File target)
  4. copyAll(File source, File targetDir)
  5. copyAndReplace(File from, File to)
  6. copyFile(File aFromFile, String aToFilename)
  7. copyFile(File copyFrom, File copyTo)
  8. copyFile(File file, String destDir)
  9. copyFile(File fileIn, File fileOut)