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.jab.docsearch.utils.FileUtils.java

/**
 * Copy file/*from  ww w.j  ava  2s  .c  om*/
 *
 * @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:info.evanchik.eclipse.karaf.core.KarafCorePluginUtils.java

/**
 * Copies the source file to the destination file
 *
 * @param src/*from w ww  .ja  v a  2 s.  c  om*/
 *            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();
        }
    }
}

From source file:org.apache.hadoop.hdfs.server.datanode.TestFsDatasetCache.java

private static long[] getBlockSizes(HdfsBlockLocation[] locs) throws Exception {
    long[] sizes = new long[locs.length];
    for (int i = 0; i < locs.length; i++) {
        HdfsBlockLocation loc = locs[i];
        String bpid = loc.getLocatedBlock().getBlock().getBlockPoolId();
        Block block = loc.getLocatedBlock().getBlock().getLocalBlock();
        ExtendedBlock extBlock = new ExtendedBlock(bpid, block);
        FileInputStream blockInputStream = null;
        FileChannel blockChannel = null;
        try {//from www  . j av  a  2s.  c o  m
            blockInputStream = (FileInputStream) fsd.getBlockInputStream(extBlock, 0);
            blockChannel = blockInputStream.getChannel();
            sizes[i] = blockChannel.size();
        } finally {
            IOUtils.cleanup(LOG, blockChannel, blockInputStream);
        }
    }
    return sizes;
}

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

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

    try {//www. j a  v  a2 s .  c om
        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//  w  w  w .  j a va  2  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:cd.education.data.collector.android.utilities.FileUtils.java

private static String actualCopy(File sourceFile, File destFile) {
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;
    FileChannel src = null;
    FileChannel dst = null;//from w  ww.  j a v  a 2s. com
    try {
        fileInputStream = new FileInputStream(sourceFile);
        src = fileInputStream.getChannel();
        fileOutputStream = new FileOutputStream(destFile);
        dst = fileOutputStream.getChannel();
        dst.transferFrom(src, 0, src.size());
        dst.force(true);
        return null;
    } catch (FileNotFoundException e) {
        Log.e(t, "FileNotFoundException while copying file", e);
        return e.getMessage();
    } catch (IOException e) {
        Log.e(t, "IOException while copying file", e);
        return e.getMessage();
    } catch (Exception e) {
        Log.e(t, "Exception while copying file", e);
        return e.getMessage();
    } finally {
        IOUtils.closeQuietly(fileInputStream);
        IOUtils.closeQuietly(fileOutputStream);
        IOUtils.closeQuietly(src);
        IOUtils.closeQuietly(dst);
    }
}

From source file:com.evolveum.midpoint.util.MiscUtil.java

public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        destFile.createNewFile();//  w ww.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:org.eclipse.thym.core.internal.util.FileUtils.java

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

    File file = null;//from   w ww  .j  a va2 s  .c o  m
    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:ru.adios.budgeter.BundleProvider.java

private static void transferFile(String source, String destination, String errMsg) {
    final File sourceFile = new File(source);
    final File destFile = new File(destination);

    FileChannel fcSource = null;
    FileChannel fcDest = null;//from  w  ww . j a  va 2 s.co  m
    try {
        fcSource = new FileInputStream(sourceFile).getChannel();
        fcDest = new FileOutputStream(destFile).getChannel();
        fcDest.transferFrom(fcSource, 0, fcSource.size());
        fcSource.close();
        fcDest.close();
    } catch (IOException e) {
        logger.error(errMsg, e);
        throw new DataAccessResourceFailureException(errMsg, e);
    } finally {
        try {
            if (fcSource != null) {
                fcSource.close();
            }
            if (fcDest != null) {
                fcDest.close();
            }
        } catch (IOException ignore) {
            logger.warn("Exception while closing IO channels", ignore);
        }
    }
}

From source file:com.aurel.track.lucene.util.FileUtil.java

/**
 * Copies a source file to a destination file
 * @param source/*w  ww .j  a  va  2 s .c  o m*/
 * @param destination
 */
public static void copyFile(File source, File destination) {
    if (!source.exists()) {
        return;
    }

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

        destination.getParentFile().mkdirs();
    }

    FileChannel srcChannel = null;
    FileChannel dstChannel = null;

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

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

        srcChannel.close();
        dstChannel.close();
    } catch (IOException ioe) {
        LOGGER.error(ExceptionUtils.getStackTrace(ioe));
    } finally {
        if (srcChannel != null)
            try {
                srcChannel.close();
            } catch (IOException ioe) {
            }
        ;
        if (dstChannel != null)
            try {
                dstChannel.close();
            } catch (IOException ioe) {
            }
        ;
    }
}