Example usage for java.nio.channels FileChannel transferFrom

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

Introduction

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

Prototype

public abstract long transferFrom(ReadableByteChannel src, long position, long count) throws IOException;

Source Link

Document

Transfers bytes into this channel's file from the given readable byte channel.

Usage

From source file:org.mycore.wcms2.MCRWebPagesSynchronizer.java

private static void copyFile(File srcFile, File destFile, long chunkSize) throws IOException {
    try (FileInputStream is = new FileInputStream(srcFile);
            FileOutputStream os = new FileOutputStream(destFile, false)) {
        FileChannel iChannel = is.getChannel();
        FileChannel oChannel = os.getChannel();
        long doneBytes = 0L;
        long todoBytes = srcFile.length();
        while (todoBytes != 0L) {
            long iterationBytes = Math.min(todoBytes, chunkSize);
            long transferredLength = oChannel.transferFrom(iChannel, doneBytes, iterationBytes);
            if (iterationBytes != transferredLength) {
                throw new IOException("Error during file transfer: expected " + iterationBytes + " bytes, only "
                        + transferredLength + " bytes copied.");
            }//from  w w w  .j a v  a 2s.  c om
            doneBytes += transferredLength;
            todoBytes -= transferredLength;
        }
    }
    boolean successTimestampOp = destFile.setLastModified(srcFile.lastModified());
    if (!successTimestampOp) {
        LOGGER.warn(String.format(Locale.ROOT,
                "Could not change timestamp for %s. Index synchronization may be slow.", destFile));
    }
}

From source file:org.eclipse.smila.binarystorage.persistence.io.BssIOUtils.java

/**
 * Copies data from input stream into persistence location, by using channels.
 * /*w  ww  . j  a  v  a  2 s .c  o m*/
 * @param path
 * @param stream
 * @throws BinaryStorageException
 */
private static void writeCopyByChannels(final String path, final InputStream stream)
        throws BinaryStorageException {
    FileChannel inChannel = null;
    FileChannel outChannel = null;
    try {
        inChannel = ((FileInputStream) stream).getChannel();
        outChannel = new FileOutputStream(path).getChannel();
        if (inChannel.size() < FILE_SIZE_64MB) {
            outChannel.transferFrom(inChannel, 0, inChannel.size());
        } else {
            // the transferTo() does not transfer files > than 2^31-1 bytes
            final long size = inChannel.size();
            long position = 0;
            while (position < size) {
                position += inChannel.transferTo(position, FILE_SIZE_TRANSFER, outChannel);
            }
        }
    } catch (final IOException ioe) {
        throw new BinaryStorageException(ioe, "Could not write binary record to :" + path);
    } finally {
        // Close the channels
        closeChannel(inChannel);
        closeChannel(outChannel);
    }
}

From source file:org.messic.service.MessicMain.java

private static void copyFile(File source, File dest) throws IOException {
    FileChannel sourceChannel = null;
    FileChannel destChannel = null;
    try {//  w  w  w. j a  va  2s .com
        sourceChannel = new FileInputStream(source).getChannel();
        destChannel = new FileOutputStream(dest).getChannel();
        destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
    } finally {
        sourceChannel.close();
        destChannel.close();
    }
}

From source file:com.bb.extensions.plugin.unittests.internal.utilities.FileUtils.java

/**
 * Copy sourceFile to destFile/*  w  w  w . ja v a2  s .c  o m*/
 * 
 * @param sourceFile
 *            The source file to copy from
 * @param destFile
 *            The destination file to copy to
 * @throws IOException
 */
public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        destFile.createNewFile();
    }

    FileChannel source = null;
    FileChannel destination = null;

    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        destination.transferFrom(source, 0, source.size());
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
}

From source file:org.socialbiz.cog.BackupUtils.java

private static void copyFile(File srcFile, File destFile) throws IOException {
    FileInputStream is = null;/*w ww .  j a  v  a 2s  .c o m*/
    FileOutputStream os = null;
    try {

        is = new FileInputStream(srcFile);
        FileChannel iChannel = is.getChannel();

        os = new FileOutputStream(destFile, false);
        FileChannel oChannel = os.getChannel();

        long doneBytes = 0L;
        long todoBytes = srcFile.length();

        while (todoBytes != 0L) {

            //Return the smallest of two
            long iterationBytes = Math.min(todoBytes, DEFAULT_COPY_BUFFER_SIZE);

            long transferredLength = oChannel.transferFrom(iChannel, doneBytes, iterationBytes);

            if (iterationBytes != transferredLength) {
                throw new IOException("Error during file transfer: expected " + iterationBytes + " bytes, only "
                        + transferredLength + " bytes copied.");
            }
            doneBytes += transferredLength;
            todoBytes -= transferredLength;
        }
    } finally {
        if (is != null) {
            is.close();
        }
        if (os != null) {
            os.close();
        }
    }
    boolean successTimestampOp = destFile.setLastModified(srcFile.lastModified());
    if (!successTimestampOp) {
        log.info("Could not change timestamp for {}. Index synchronization may be slow. " + destFile);
    }
}

From source file:och.util.FileUtil.java

public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        destFile.getParentFile().mkdirs();
        destFile.createNewFile();//from   ww  w .  j a  v a  2 s  .c o  m
    }

    FileChannel source = null;
    FileChannel destination = null;
    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        destination.transferFrom(source, 0, source.size());
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
}

From source file:com.bjorsond.android.timeline.utilities.Utilities.java

public static void copyFile(String fromFile, String toPath, String toFilename) {

    System.out.println("COPY!");
    if (Environment.getExternalStorageState().equals("mounted")) {
        File sdCardDirectory = Environment.getExternalStorageDirectory();

        try {//from   ww w . j  a v a 2  s.co  m
            if (sdCardDirectory.canWrite()) {

                File destinationDirectory = new File(toPath);
                File sourceFile = new File(fromFile);
                File destinationFile = new File(destinationDirectory, toFilename);

                if (!destinationDirectory.exists()) {
                    destinationDirectory.mkdirs();
                }

                FileChannel source = new FileInputStream(sourceFile).getChannel();
                FileChannel destination = new FileOutputStream(destinationFile).getChannel();
                destination.transferFrom(source, 0, source.size());
                source.close();
                destination.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

From source file:edu.mit.csail.sdg.alloy4.Terminal.java

public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!sourceFile.exists()) {
        return;//w  ww  .  j ava  2 s  .  co  m
    }
    if (!destFile.exists()) {
        destFile.createNewFile();
    }

    FileChannel source = null;
    FileChannel destination = null;

    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        destination.transferFrom(source, 0, source.size());
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
}

From source file:org.fusesource.mop.support.Database.java

static private void copy(File from, File to) throws IOException {
    to.delete();// w w w. ja  va 2  s.  c o m
    FileChannel in = new FileInputStream(from).getChannel();
    try {

        File tmp = File.createTempFile(to.getName(), ".part", to.getParentFile());
        FileChannel out = new FileOutputStream(tmp).getChannel();
        try {
            out.transferFrom(in, 0, from.length());
        } finally {
            out.close();
        }

        tmp.renameTo(to);
    } finally {
        in.close();
    }
}

From source file:info.evanchik.eclipse.karaf.core.KarafCorePluginUtils.java

/**
 * Copies the source file to the destination file
 *
 * @param src/*from ww  w.  ja  v a 2s  .  c o m*/
 *            the source file
 * @param dst
 *            the destination file
 * @throws IOException
 *             if there is a problem during the file copy
 */
public static void copyFile(final File src, final File dst) throws IOException {
    if (!src.exists()) {
        throw new FileNotFoundException("File does not exist: " + src.getAbsolutePath());
    }

    if (!dst.exists()) {
        dst.createNewFile();
    }

    FileChannel source = null;
    FileChannel destination = null;
    try {
        source = new FileInputStream(src).getChannel();
        destination = new FileOutputStream(dst).getChannel();

        destination.transferFrom(source, 0, source.size());
    } finally {
        if (source != null) {
            source.close();
        }

        if (destination != null) {
            destination.close();
        }
    }
}