Example usage for java.nio.channels FileChannel size

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

Introduction

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

Prototype

public abstract long size() throws IOException;

Source Link

Document

Returns the current size of this channel's file.

Usage

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

/**
 * Copies a file//from w  ww .  j  a  v a2s  .co m
 * 
 * @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.cloudata.core.commitlog.pipe.CommitLogFileChannel.java

public static long readLastIndex(FileChannel ch) throws IOException {
    ByteBuffer buf = ByteBuffer.allocate(Long.SIZE / 8);
    long chSize = ch.size();

    if (chSize > 0) {
        ch.position(chSize > (Long.SIZE / 8) ? chSize - (Long.SIZE / 8) : 0);
        ch.read(buf);// w w  w .ja  v a  2s  . co m
        buf.flip();
        return buf.getLong();
    } else {
        return 0;
    }
}

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

/**
 * Copies a file from in to out using a progress monitor
 * //www .  ja  va  2s. com
 * @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:com.germinus.easyconf.FileUtil.java

public static void copyFile(File source, File destination) {
    if (!source.exists()) {
        return;/*from   ww w .j a v  a  2 s  . c  o m*/
    }

    if ((destination.getParentFile() != null) && (!destination.getParentFile().exists())) {

        destination.getParentFile().mkdirs();
    }

    try {
        FileChannel srcChannel = new FileInputStream(source).getChannel();
        FileChannel dstChannel = new FileOutputStream(destination).getChannel();

        dstChannel.transferFrom(srcChannel, 0, srcChannel.size());

        srcChannel.close();
        dstChannel.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

From source file:net.ftb.util.FileUtils.java

public static void copyFile(File sourceFile, File destinationFile, boolean overwrite) throws IOException {
    if (sourceFile.exists()) {
        if (!destinationFile.exists()) {
            destinationFile.getParentFile().mkdirs();
            destinationFile.createNewFile();
        } else if (!overwrite)
            return;
        FileChannel sourceStream = null, destinationStream = null;
        try {/*w  w w . ja v a  2  s  . c  o  m*/
            sourceStream = new FileInputStream(sourceFile).getChannel();
            destinationStream = new FileOutputStream(destinationFile).getChannel();
            destinationStream.transferFrom(sourceStream, 0, sourceStream.size());
        } finally {
            if (sourceStream != null) {
                sourceStream.close();
            }
            if (destinationStream != null) {
                destinationStream.close();
            }
        }
    }
}

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();/*from   w  ww.ja v  a2s  .co  m*/
    destinationChannel.close();
}

From source file:net.ftb.util.FTBFileUtils.java

public static void copyFile(File sourceFile, File destinationFile, boolean overwrite) throws IOException {
    if (sourceFile.exists()) {
        if (!destinationFile.exists()) {
            destinationFile.getParentFile().mkdirs();
            destinationFile.createNewFile();
        } else if (!overwrite) {
            return;
        }//from  w w  w  .  jav  a 2s . c  om
        FileChannel sourceStream = null, destinationStream = null;
        try {
            sourceStream = new FileInputStream(sourceFile).getChannel();
            destinationStream = new FileOutputStream(destinationFile).getChannel();
            destinationStream.transferFrom(sourceStream, 0, sourceStream.size());
        } finally {
            if (sourceStream != null) {
                sourceStream.close();
            }
            if (destinationStream != null) {
                destinationStream.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   www.  ja  v a  2 s.  com*/
        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.messic.service.MessicMain.java

private static void copyFile(File source, File dest) throws IOException {
    FileChannel sourceChannel = null;
    FileChannel destChannel = null;
    try {//  www  . ja  v a 2 s.co m
        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/*  ww  w.  j a  v a 2 s  . co 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();
        }
    }
}