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:es.uvigo.darwin.jmodeltest.io.HtmlReporter.java

@SuppressWarnings("resource")
public static void copyFile(File in, File out) throws IOException {
    FileChannel inChannel = new FileInputStream(in).getChannel();
    FileChannel outChannel = new FileOutputStream(out).getChannel();
    try {//from  ww w.  j a  v  a  2s  .com
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } catch (IOException e) {
        throw e;
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }
}

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

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

        input.skip(offset);
        while (count > 0) {
            if (count < DEFAULT_BUFFER_SIZE) {
                n = input.read(buffer, 0, (int) count);
            } else {
                n = input.read(buffer, 0, DEFAULT_BUFFER_SIZE);
            }

            output.write(buffer, 0, (int) n);
            count -= n;
            rcount += 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 rcount;
}

From source file:org.jboss.as.forge.util.Files.java

/**
 * Copies the source file to the destination file.
 *
 * @param srcFile    the file to copy/*  w  w  w  . jav  a2 s  .  co m*/
 * @param targetFile the target file
 *
 * @return {@code true} if the file was successfully copied, {@code false} if the copy failed or was incomplete
 *
 * @throws IOException if an IO error occurs copying the file
 */
public static boolean copyFile(final File srcFile, final File targetFile) throws IOException {
    FileInputStream in = null;
    FileOutputStream out = null;
    FileChannel inChannel = null;
    FileChannel outChannel = null;
    try {
        in = new FileInputStream(srcFile);
        inChannel = in.getChannel();
        out = new FileOutputStream(targetFile);
        outChannel = out.getChannel();
        long bytesTransferred = 0;
        while (bytesTransferred < inChannel.size()) {
            bytesTransferred += inChannel.transferTo(0, inChannel.size(), outChannel);
        }
    } finally {
        Streams.safeClose(outChannel);
        Streams.safeClose(out);
        Streams.safeClose(inChannel);
        Streams.safeClose(in);
    }
    return srcFile.length() == targetFile.length();
}

From source file:org.jab.docsearch.utils.FileUtils.java

/**
 * Copy file/*from  w w  w.  j  ava2  s. com*/
 *
 * @param sourceFilename       source file
 * @param destinationFilename  destination file
 */
public static boolean copyFile(String sourceFilename, String destinationFilename) {
    if (sourceFilename == null) {
        logger.warn("copyFile() failed because sourceFilename is null");
        return false;
    }
    if (destinationFilename == null) {
        logger.warn("copyFile() failed because destinationFilename is null");
        return false;
    }

    FileInputStream fis = null;
    FileOutputStream fos = null;
    FileChannel fcin = null;
    FileChannel fcout = null;
    try {
        // open stream and channel from input
        fis = new FileInputStream(sourceFilename);
        fcin = fis.getChannel();

        // open stream and channel from output
        fos = new FileOutputStream(destinationFilename);
        fcout = fos.getChannel();

        fcin.transferTo(0, fcin.size(), fcout);

        return true;
    } catch (IOException ioe) {
        logger.fatal("copyFile() failed", ioe);
        return false;
    } catch (SecurityException se) {
        logger.fatal("copyFile() failed", se);
        return false;
    } finally {
        try {
            if (fcin != null) {
                fcin.close();
            }
        } catch (IOException ioe) {
            logger.fatal("copyFile() can't close FileChannel");
        }

        try {
            if (fis != null) {
                fis.close();
            }
        } catch (IOException ioe) {
            logger.fatal("copyFile() can't close FileInputStream");
        }

        try {
            if (fcout != null) {
                fcout.close();
            }
        } catch (IOException ioe) {
            logger.fatal("copyFile() can't close FileChannel");
        }

        try {
            if (fos != null) {
                fos.close();
            }
        } catch (IOException ioe) {
            logger.fatal("copyFile() can't close FileOutputStream");
        }
    }
}

From source file:com.mods.grx.settings.utils.Utils.java

public static void file_copy(File ori_file, File dest_file) {

    try {/*from   w w w .  j  a  v a  2 s  . c o m*/
        FileInputStream i_s = new FileInputStream(ori_file);
        FileOutputStream o_s = new FileOutputStream(dest_file);
        FileChannel inChannel = i_s.getChannel();
        FileChannel outChannel = o_s.getChannel();
        inChannel.transferTo(0, inChannel.size(), outChannel);
        i_s.close();
        o_s.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.github.feribg.audiogetter.helpers.Utils.java

/**
 * Copy a file with the option to overwrite existing files
 *
 * @param src/*from   w w w  .  j  a v a2  s.  c o  m*/
 * @param dst
 * @param overwrite
 * @throws IOException
 */
public static void copyFile(File src, File dst, Boolean overwrite) throws IOException {
    FileChannel inChannel = new FileInputStream(src).getChannel();
    FileChannel outChannel = new FileOutputStream(dst).getChannel();
    if (!dst.exists() || (dst.exists() && overwrite)) {
        try {
            if (dst.exists()) {
                Log.d(App.TAG, "copyFile: destination exists but overwriting");
            }
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } finally {
            if (inChannel != null)
                inChannel.close();
            if (outChannel != null)
                outChannel.close();
        }
    } else {
        Log.e(App.TAG, "copyFile: destination already exists:" + dst.getAbsolutePath());
        throw new IOException("copyFile: destination already exists:" + dst.getAbsolutePath());
    }
}

From source file:dsd.controller.ParserControler.java

@SuppressWarnings("resource")
private static boolean TransferImageToSite(File inFile, File outFile) {
    if (!outFile.exists()) {
        try {//from  www  .  j  av  a  2s.  com
            outFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    FileChannel inChannel = null;
    FileChannel outChannel = null;
    try {
        inChannel = new FileInputStream(inFile).getChannel();
        outChannel = new FileOutputStream(outFile).getChannel();
        int maxCount = (64 * 1024 * 1024) - (32 * 1024);
        long size = inChannel.size();
        long position = 0;
        while (position < size) {
            position += inChannel.transferTo(position, maxCount, outChannel);
        }
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (inChannel != null)
                inChannel.close();
            if (outChannel != null)
                outChannel.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return false;
}

From source file:org.eclipse.thym.core.internal.util.FileUtils.java

private static void copyFile(File source, File target) throws IOException {

    File file = null;/*  www.java2  s . c  om*/
    if (source.isDirectory() && source.exists() && target.isDirectory() && target.exists()) {

        File[] children = source.listFiles();
        for (File child : children) {
            file = target;
            if (child.isDirectory()) {
                file = new File(target, child.getName());
                if (!file.exists())
                    file.mkdir();
            }
            copyFile(child, file);
        }
    } else {// source is a file
        if (target.isFile()) {
            file = target;
        } else {
            file = new File(target, source.getName());
        }

        FileChannel out = null;
        FileChannel in = null;
        try {
            if (!file.exists()) {
                file.createNewFile();
            }

            out = new FileOutputStream(file).getChannel();
            in = new FileInputStream(source).getChannel();
            in.transferTo(0, in.size(), out);
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (out != null)
                out.close();
            if (in != null)
                in.close();
        }
    }
}

From source file:org.yamj.core.service.file.tools.FileTools.java

/**
 * Copy the source file to the destination
 *
 * @param src//from   w ww  . j a  v a2  s .co m
 * @param dst
 * @return
 */
public static boolean copyFile(File src, File dst) {
    boolean returnValue = Boolean.FALSE;

    if (!src.exists()) {
        LOG.error("The file '{}' does not exist", src);
        return returnValue;
    }

    if (dst.isDirectory()) {
        makeDirectories(dst);
        returnValue = copyFile(src, new File(dst + File.separator + src.getName()));
    } else {
        FileInputStream inSource = null;
        FileOutputStream outSource = null;
        FileChannel inChannel = null;
        FileChannel outChannel = null;
        try {
            // gc: copy using file channels, potentially much faster
            inSource = new FileInputStream(src);
            outSource = new FileOutputStream(dst);
            inChannel = inSource.getChannel();
            outChannel = outSource.getChannel();

            long p = 0, s = inChannel.size();
            while (p < s) {
                p += inChannel.transferTo(p, 1024 * 1024, outChannel);
            }
            return Boolean.TRUE;
        } catch (IOException error) {
            LOG.error("Failed copying file '{}' to '{}'", src, dst);
            LOG.error("File copying error", error);
            returnValue = Boolean.FALSE;
        } finally {
            if (inChannel != null) {
                try {
                    inChannel.close();
                } catch (IOException ex) {
                    // Ignore
                }
            }
            if (inSource != null) {
                try {
                    inSource.close();
                } catch (IOException ex) {
                    // Ignore
                }
            }

            if (outChannel != null) {
                try {
                    outChannel.close();
                } catch (IOException ex) {
                    // Ignore
                }
            }
            if (outSource != null) {
                try {
                    outSource.close();
                } catch (IOException ex) {
                    // Ignore
                }
            }
        }
    }

    return returnValue;
}

From source file:biz.bokhorst.xprivacy.Util.java

public static void copy(File src, File dst) throws IOException {
    FileInputStream inStream = null;
    try {/*from  ww  w .j  a  v a2 s . co  m*/
        inStream = new FileInputStream(src);
        FileOutputStream outStream = null;
        try {
            outStream = new FileOutputStream(dst);
            FileChannel inChannel = inStream.getChannel();
            FileChannel outChannel = outStream.getChannel();
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } finally {
            if (outStream != null)
                outStream.close();
        }
    } finally {
        if (inStream != null)
            inStream.close();
    }
}