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.datavaultplatform.common.io.FileCopy.java

/**
 * Internal copy file method.//ww  w  .  java2  s.  c  o m
 * 
 * @param progress  the progress tracking object
 * @param srcFile  the validated source file, must not be {@code null}
 * @param destFile  the validated destination file, must not be {@code null}
 * @param preserveFileDate  whether to preserve the file date
 * @throws IOException if an error occurs
 */
private static void doCopyFile(Progress progress, File srcFile, File destFile, boolean preserveFileDate)
        throws IOException {
    if (destFile.exists() && destFile.isDirectory()) {
        throw new IOException("Destination '" + destFile + "' exists but is a directory");
    }

    FileInputStream fis = null;
    FileOutputStream fos = null;
    FileChannel input = null;
    FileChannel output = null;
    try {
        fis = new FileInputStream(srcFile);
        fos = new FileOutputStream(destFile);
        input = fis.getChannel();
        output = fos.getChannel();
        long size = input.size();
        long pos = 0;
        long count = 0;
        while (pos < size) {
            count = size - pos > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : size - pos;
            long copied = output.transferFrom(input, pos, count);
            pos += copied;
            progress.byteCount += copied;
            progress.timestamp = System.currentTimeMillis();
        }
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(fis);
    }

    if (srcFile.length() != destFile.length()) {
        throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'");
    }
    if (preserveFileDate) {
        destFile.setLastModified(srcFile.lastModified());
    }

    progress.fileCount += 1;
}

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

public static void copy(File src, File dst) throws IOException {
    FileInputStream inStream = null;
    try {//from www. ja v a  2 s .com
        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();
    }
}

From source file:gov.nasa.ensemble.common.io.FileUtilities.java

/**
 * Make a copy of a file on the filesystem (platform independent).
 * //from www .j a  va2 s.  c  om
 * @param source
 *            the file to copy.
 * @param dest
 *            the copy to make.
 * @return whether the file copy exists on the filesystem upon completion.
 * @throws IOException
 */
public static boolean copyFile(File source, File dest) throws IOException {
    FileInputStream sourceStream = new FileInputStream(source);
    FileChannel sourceChannel = sourceStream.getChannel();
    FileOutputStream destStream = new FileOutputStream(dest);
    FileChannel destChannel = destStream.getChannel();
    try {
        sourceChannel.transferTo(0, sourceChannel.size(), destChannel);
    } catch (IOException ex) {
        if (ex.getCause() instanceof OutOfMemoryError) {
            IOUtils.copy(sourceStream, destStream);
        }
    } finally {
        destChannel.close();
        IOUtils.closeQuietly(destStream);
        sourceChannel.close();
        IOUtils.closeQuietly(sourceStream);
    }

    return dest.exists();
}

From source file:org.solmix.commons.util.Files.java

/**
 * ?// w  w  w .ja  v a 2s . c o  m
 *
 * @param f1
 *            ?
 * @param f2
 *            
 * @throws Exception
 */
public static void copyFile(File f1, File f2) throws Exception {
    int length = 2097152;
    FileInputStream in = new FileInputStream(f1);
    FileOutputStream out = new FileOutputStream(f2);
    FileChannel inC = in.getChannel();
    FileChannel outC = out.getChannel();
    ByteBuffer b = null;
    while (true) {
        if (inC.position() == inC.size()) {
            inC.close();
            outC.close();
        }
        if ((inC.size() - inC.position()) < length) {
            length = (int) (inC.size() - inC.position());
        } else
            length = 2097152;
        b = ByteBuffer.allocateDirect(length);
        inC.read(b);
        b.flip();
        outC.write(b);
        outC.force(false);
    }
}

From source file:org.uva.itast.blended.omr.OMRUtils.java

/**
 * Mtodo que a partir de un fichero pdf de entrada devuelve el
 * nmero su pginas//from   w w w  . j a  v a 2 s .  co m
 * 
 * @param inputdir
 * @return pdffile.getNumpages();
 * @throws IOException
 */
public static int getNumpagesPDF(String inputdir) throws IOException {
    RandomAccessFile raf = new RandomAccessFile(inputdir, "r"); // se carga
    // la imagen
    // pdf para
    // leerla
    FileChannel channel = raf.getChannel();
    ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
    PDFFile pdffile = new PDFFile(buf); // se crea un objeto de tipo PDFFile
    // para almacenar las pginas
    return pdffile.getNumPages(); // se obtiene el nmero de paginas
}

From source file:it.geosolutions.tools.io.file.IOUtils.java

/**
 * Optimize version of copy method for file channels.
 * // w  w  w  .  j  a  va2  s .c  o  m
 * @param bufferSize
 *            size of the temp buffer to use for this copy.
 * @param source
 *            the source {@link ReadableByteChannel}.
 * @param destination
 *            the destination {@link WritableByteChannel};.
 * @throws IOException
 *             in case something bad happens.
 */
public static void copyFileChannel(int bufferSize, FileChannel source, FileChannel destination)
        throws IOException {

    Objects.notNull(source, destination);
    if (!source.isOpen() || !destination.isOpen())
        throw new IllegalStateException("Source and destination channels must be open.");
    FileLock lock = null;
    try {

        lock = destination.lock();
        final long sourceSize = source.size();
        long pos = 0;
        while (pos < sourceSize) {
            // read and flip
            final long remaining = (sourceSize - pos);
            final int mappedZoneSize = remaining >= bufferSize ? bufferSize : (int) remaining;
            destination.transferFrom(source, pos, mappedZoneSize);
            // update zone
            pos += mappedZoneSize;

        }
    } finally {
        if (lock != null) {
            try {
                lock.release();
            } catch (Throwable t) {
                if (LOGGER.isInfoEnabled())
                    LOGGER.info(t.getLocalizedMessage(), t);
            }
        }

    }
}

From source file:phex.utils.FileUtils.java

/**
 * From Apache Jakarta Commons IO./* w  w w .j a  v  a  2 s  . c  o  m*/
 * 
 * Internal copy file method.
 * 
 * @param srcFile  the validated source file, not null
 * @param destFile  the validated destination file, not null
 * @param preserveFileDate  whether to preserve the file date
 * @throws IOException if an error occurs
 */
private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
    if (destFile.exists() && destFile.isDirectory()) {
        throw new IOException("Destination '" + destFile + "' exists but is a directory");
    }

    FileChannel input = new FileInputStream(srcFile).getChannel();
    try {
        FileChannel output = new FileOutputStream(destFile).getChannel();
        try {
            output.transferFrom(input, 0, input.size());
        } finally {
            IOUtil.closeQuietly(output);
        }
    } finally {
        IOUtil.closeQuietly(input);
    }

    if (srcFile.length() != destFile.length()) {
        throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'");
    }
    if (preserveFileDate) {
        destFile.setLastModified(srcFile.lastModified());
    }
}

From source file:com.opendesign.utils.CmnUtil.java

/**
 * ? /*from   w  ww  . j a  v  a2  s. co  m*/
 * 
 * @param oldFilePath
 * @param newFilePath
 */
public static void fileCopy(String oldFilePath, String newFilePath) {
    File oldFile = new File(oldFilePath);
    File newFile = new File(newFilePath);
    try {
        FileInputStream inputStream = new FileInputStream(oldFile);
        FileOutputStream outputStream = new FileOutputStream(newFile);
        FileChannel fcin = inputStream.getChannel();
        FileChannel fcout = outputStream.getChannel();

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

        fcout.close();
        fcin.close();
        outputStream.close();
        inputStream.close();
    } catch (Exception e) {
    }
}

From source file:com.opendesign.utils.CmnUtil.java

/**
 * ? /*from  w ww  . j  av a  2  s .  co  m*/
 * 
 * @param oldFilePath
 * @param oldFileName
 * @param newFilePath
 * @param newFileName
 */
public static void fileCopy(String oldFilePath, String oldFileName, String newFilePath, String newFileName) {
    File oldFile = new File(oldFilePath, oldFileName);
    File newFile = new File(newFilePath, newFileName);
    try {
        FileInputStream inputStream = new FileInputStream(oldFile);
        FileOutputStream outputStream = new FileOutputStream(newFile);
        FileChannel fcin = inputStream.getChannel();
        FileChannel fcout = outputStream.getChannel();

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

        fcout.close();
        fcin.close();
        outputStream.close();
        inputStream.close();
    } catch (Exception e) {
    }
}

From source file:phex.util.FileUtils.java

/**
 * From Apache Jakarta Commons IO.//from w w  w  .  j  a  v  a 2s  . c  o m
 * <p>
 * Internal copy file method.
 *
 * @param srcFile          the validated source file, not null
 * @param destFile         the validated destination file, not null
 * @param preserveFileDate whether to preserve the file date
 * @throws IOException if an error occurs
 */
private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
    if (destFile.exists() && destFile.isDirectory()) {
        throw new IOException("Destination '" + destFile + "' exists but is a directory");
    }

    FileChannel input = new FileInputStream(srcFile).getChannel();
    try {
        FileChannel output = new FileOutputStream(destFile).getChannel();
        try {
            output.transferFrom(input, 0, input.size());
        } finally {
            IOUtil.closeQuietly(output);
        }
    } finally {
        IOUtil.closeQuietly(input);
    }

    if (srcFile.length() != destFile.length()) {
        throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + '\'');
    }
    if (preserveFileDate) {
        destFile.setLastModified(srcFile.lastModified());
    }
}