Example usage for java.nio.channels FileChannel transferTo

List of usage examples for java.nio.channels FileChannel transferTo

Introduction

In this page you can find the example usage for java.nio.channels FileChannel transferTo.

Prototype

public abstract long transferTo(long position, long count, WritableByteChannel target) throws IOException;

Source Link

Document

Transfers bytes from this channel's file to the given writable byte channel.

Usage

From source file:com.becapps.easydownloader.utils.Utils.java

@SuppressWarnings("resource")
public static void copyFile(File src, File dst) throws IOException {
    FileChannel inChannel = new FileInputStream(src).getChannel();
    FileChannel outChannel = new FileOutputStream(dst).getChannel();
    //if (!dst.exists()) {
    try {//from  www  . j av a2s.  co  m
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }
    /*} else {
       logger("w", "copyFile: destination already exists", DEBUG_TAG);
    }*/
}

From source file:com.edgenius.core.util.FileUtil.java

public static void copyFile(File in, File out) throws Exception {
    FileChannel sourceChannel = new FileInputStream(in).getChannel();
    FileChannel destinationChannel = new FileOutputStream(out).getChannel();
    sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);
    sourceChannel.close();/*  w  w w.ja v a2s. c  o m*/
    destinationChannel.close();
}

From source file:org.infai.amor.test.ModelUtil.java

public static void copyFile(final File in, final File out) throws IOException {
    final FileChannel inChannel = new FileInputStream(in).getChannel();
    final FileChannel outChannel = new FileOutputStream(out).getChannel();
    try {/*from  w w w. j  ava 2  s  . c o m*/
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } catch (final IOException e) {
        throw e;
    } finally {
        if (inChannel != null) {
            inChannel.close();
        }
        if (outChannel != null) {
            outChannel.close();
        }
    }
}

From source file:org.pmedv.core.util.FileUtils.java

/**
 * Copies a file//  w w  w . ja  v a 2  s  .  com
 * 
 * @param in Source file
 * @param out Destination file
 * @throws IOException
 */
public static void copyFile(File in, File out) throws IOException {

    int bufsize = 8192;
    int transferred = 0;

    FileChannel inChannel = new FileInputStream(in).getChannel();
    FileChannel outChannel = new FileOutputStream(out).getChannel();

    try {
        while (transferred < inChannel.size()) {
            inChannel.transferTo(transferred, bufsize, outChannel);
            transferred += bufsize;
        }

    } catch (IOException e) {
        throw e;
    } finally {
        if (inChannel != null) {
            inChannel.close();
        }
        if (outChannel != null) {
            outChannel.close();
        }
    }
}

From source file:org.pmedv.core.util.FileUtils.java

/**
 * Copies a file from in to out using a progress monitor
 * //  www  .  ja  v a 2 s .c  o  m
 * @param in the file to read from
 * @param out the file to write to
 * @param monitor the monitor interface to use
 * @throws IOException
 */
public static void copyFile(File in, File out, IProgressMonitor monitor) throws IOException {

    int bufsize = 8192;
    int transferred = 0;

    FileChannel inChannel = new FileInputStream(in).getChannel();
    FileChannel outChannel = new FileOutputStream(out).getChannel();

    try {
        while (transferred < inChannel.size()) {
            inChannel.transferTo(transferred, bufsize, outChannel);
            transferred += bufsize;
            int progress = (int) (transferred / (inChannel.size() / 100));
            monitor.setProgress(progress);
        }

    } catch (IOException e) {
        throw e;
    } finally {
        if (inChannel != null) {
            inChannel.close();
        }
        if (outChannel != null) {
            outChannel.close();
        }
    }
}

From source file:Main.java

/**
 * Effective way to copy files by file channel.
 * @return Return the number of copied files.
 *//*  w w w. j  a va  2s  .  com*/
public static int fileChannelCopy(File[] sources, File[] targets) {
    int result = 0;
    if (sources == null || targets == null) {
        return result;
    }

    FileInputStream fis = null;
    FileOutputStream fos = null;
    FileChannel fc_in = null;
    FileChannel fc_out = null;
    try {
        for (int i = 0, len_s = sources.length, len_t = targets.length; i < len_s && i < len_t; ++i) {
            if (sources[i] == null || targets[i] == null) {
                continue;
            }

            fis = new FileInputStream(sources[i]);
            fos = new FileOutputStream(targets[i]);
            fc_in = fis.getChannel();
            fc_out = fos.getChannel();
            fc_in.transferTo(0, fc_in.size(), fc_out);

            ++result;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fc_out != null) {
            try {
                fc_out.close();
            } catch (IOException e) {
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
            }
        }
        if (fc_in != null) {
            try {
                fc_in.close();
            } catch (IOException e) {
            }
        }
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
            }
        }
    }

    return result;
}

From source file:com.weihuoya.bboo._G.java

public static boolean copyFile(File src, File dst) {
    FileInputStream instream = null;
    FileOutputStream outstream = null;
    FileChannel inchannel = null;
    FileChannel outchannel = null;

    try {//  www  .ja  v a2 s . c om
        // channel
        instream = new FileInputStream(src);
        outstream = new FileOutputStream(dst);
        inchannel = instream.getChannel();
        outchannel = outstream.getChannel();
        // transfer
        inchannel.transferTo(0, inchannel.size(), outchannel);
        // close
        inchannel.close();
        outchannel.close();
        instream.close();
        outstream.close();
        //
        return true;
    } catch (IOException e) {
        _G.log(e.toString());
    }
    return false;
}

From source file:it.isislab.dmason.util.SystemManagement.Worker.Updater.java

public static void copyFile(File sfile, File dfile) throws Exception {
    FileChannel source = new FileInputStream(sfile).getChannel();
    FileChannel dest = new FileOutputStream(dfile).getChannel();
    source.transferTo(0, source.size(), dest);
    source.close();//from   w  w w  . j  av a2  s  . c om
    dest.close();

}

From source file:com.alibaba.otter.shared.common.utils.NioUtils.java

/**
 * ??copy/*  w  w  w .  j a v  a 2  s .c o  m*/
 */
public static long copy(InputStream input, OutputStream output) throws IOException {
    long count = 0;
    long n = 0;
    if (input instanceof FileInputStream) {
        FileChannel inChannel = ((FileInputStream) input).getChannel();
        WritableByteChannel outChannel = Channels.newChannel(output);
        count = inChannel.transferTo(0, inChannel.size(), outChannel);
    } else if (output instanceof FileOutputStream) {
        FileChannel outChannel = ((FileOutputStream) output).getChannel();
        ReadableByteChannel inChannel = Channels.newChannel(input);
        do {
            n = outChannel.transferFrom(inChannel, count, DEFAULT_BUFFER_SIZE);
            count += n;
        } while (n > 0);
    } else {
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];

        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, (int) n);
            count += n;
        }
        // ReadableByteChannel inChannel = Channels.newChannel(input);
        // WritableByteChannel outChannel = Channels.newChannel(output);
        //            
        // //ByteBuffer buffer = new ByteBuffer(DEFAULT_BUFFER_SIZE);
        // ByteBuffer buffer = ByteBuffer.allocateDirect(DEFAULT_BUFFER_SIZE);
        // while (-1 != (n = inChannel.read(buffer))) {
        // outChannel.write(buffer);
        // count += n;
        // }
    }
    return count;
}

From source file:com.alibaba.otter.shared.common.utils.NioUtils.java

/**
 * ??copy//from   w  ww . j  ava 2 s  .  c o m
 */
public static long copy(InputStream input, OutputStream output, long offset) throws IOException {
    long count = 0;
    long n = 0;
    if (input instanceof FileInputStream) {
        FileChannel inChannel = ((FileInputStream) input).getChannel();
        WritableByteChannel outChannel = Channels.newChannel(output);
        count = inChannel.transferTo(offset, inChannel.size() - offset, outChannel);
    } else if (output instanceof FileOutputStream) {
        FileChannel outChannel = ((FileOutputStream) output).getChannel();
        ReadableByteChannel inChannel = Channels.newChannel(input);
        do {
            n = outChannel.transferFrom(inChannel, offset + count, DEFAULT_BUFFER_SIZE);
            count += n;
        } while (n > 0);
    } else {
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];

        input.skip(offset);
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, (int) n);
            count += n;
        }
        // ReadableByteChannel inChannel = Channels.newChannel(input);
        // WritableByteChannel outChannel = Channels.newChannel(output);
        //            
        // //ByteBuffer buffer = new ByteBuffer(DEFAULT_BUFFER_SIZE);
        // ByteBuffer buffer = ByteBuffer.allocateDirect(DEFAULT_BUFFER_SIZE);
        // while (-1 != (n = inChannel.read(buffer))) {
        // outChannel.write(buffer);
        // count += n;
        // }
    }
    return count;
}