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.apache.hadoop.hdfs.server.datanode.FSDataset.java

/**
 * Copies a file as fast as possible. Tries to do a hardlink instead of a copy
 * if the hardlink parameter is specified.
 *
 * @param src//from ww w. jav  a 2  s.c  o m
 *          the source file for copying
 * @param dst
 *          the destination file for copying
 * @param hardlink
 *          whether or not to attempt a hardlink
 * @throws IOException
 */
public void copyFile(File src, File dst, boolean hardlink) throws IOException {

    if (src == null || dst == null) {
        throw new IOException("src/dst file is null");
    }

    try {
        if (hardlink && shouldHardLinkBlockCopy) {
            // Remove destination before hard linking, since this file might already
            // exist and a hardlink would fail as a result.
            if (dst.exists()) {
                if (!dst.delete()) {
                    throw new IOException("Deletion of file : " + dst + " failed");
                }
            }
            NativeIO.link(src, dst);
            DataNode.LOG.info("Hard Link Created from : " + src + " to " + dst);
            return;
        }
    } catch (IOException e) {
        DataNode.LOG
                .warn("Hard link failed from : " + src + " to " + dst + " continuing with regular file copy");
    }

    FileChannel input = null;
    FileChannel output = null;
    try {
        // This improves copying performance a lot, it uses native buffers
        // for copying.
        input = new FileInputStream(src).getChannel();
        output = new FileOutputStream(dst).getChannel();
        if (input == null || output == null) {
            throw new IOException("Could not create file channels for src : " + src + " dst : " + dst);
        }
        long bytesLeft = input.size();
        long position = 0;
        while (bytesLeft > 0) {
            long bytesWritten = output.transferFrom(input, position, bytesLeft);
            bytesLeft -= bytesWritten;
            position += bytesWritten;
        }
        if (datanode.syncOnClose) {
            output.force(true);
        }
    } finally {
        if (input != null) {
            input.close();
        }
        if (output != null) {
            output.close();
        }
    }
}

From source file:es.pode.publicacion.negocio.servicios.SrvPublicacionServiceImpl.java

/**
 * Mueve el contenido de un directorio a otro directorio.
 * // w w w . j  ava2  s.  com
 * @param oldDir
 *            Directorio origen.
 * @param newDir
 *            Directorio destino.
 * @return devuelve el tamanio del directorio movido.
 * @throws Exception
 * 
 */
protected Long handleMoveDir(File oldDir, File newDir) throws IOException {
    long longitudTransferida = 0;
    if (oldDir.isDirectory()) {
        newDir.mkdirs();
        String list[] = oldDir.list();

        for (int i = 0; i < list.length; i++) {
            String dest1 = newDir.getAbsolutePath() + "/" + list[i];
            String src1 = oldDir.getAbsolutePath() + "/" + list[i];
            longitudTransferida += handleMoveDir(new File(src1), new File(dest1)).longValue();
        }
    } else {
        FileInputStream fin = new FileInputStream(oldDir);
        FileOutputStream fos = new FileOutputStream(newDir);

        FileChannel sourceChannel = fin.getChannel();
        FileChannel targetChannel = fos.getChannel();
        longitudTransferida = sourceChannel.size();
        sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);
        sourceChannel.close();
        targetChannel.close();
    }
    return new Long(longitudTransferida);
}

From source file:com.zoffcc.applications.zanavi.Navit.java

private static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!sourceFile.exists()) {
        return;//  w w w .  ja  v a 2s. co m
    }
    if (!destFile.exists()) {
        destFile.createNewFile();
    }
    FileChannel source = null;
    FileChannel destination = null;
    source = new FileInputStream(sourceFile).getChannel();
    destination = new FileOutputStream(destFile).getChannel();
    if (destination != null && source != null) {
        destination.transferFrom(source, 0, source.size());
    }
    if (source != null) {
        source.close();
    }
    if (destination != null) {
        destination.close();
    }

}

From source file:com.zoffcc.applications.zanavi.Navit.java

private void import_map_points_from_sdcard() {
    String orig_file = NAVIT_DATA_SHARE_DIR + Navit_DEST_FILENAME;
    String dest_file_dir = CFG_FILENAME_PATH + "../export/";

    try {//from www  .ja  va  2  s  .co m
        File source = new File(dest_file_dir + Navit_DEST_FILENAME);
        File destination = new File(orig_file);

        if (source.exists()) {
            FileInputStream fi = new FileInputStream(source);
            FileOutputStream fo = new FileOutputStream(destination);
            FileChannel src = fi.getChannel();
            FileChannel dst = fo.getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
            fi.close();
            fo.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    read_map_points();
}

From source file:com.zoffcc.applications.zanavi.Navit.java

private void export_map_points_to_sdcard() {
    String orig_file = NAVIT_DATA_SHARE_DIR + Navit_DEST_FILENAME;
    String dest_file_dir = CFG_FILENAME_PATH + "../export/";

    try {/*w w w .  j  av a2  s .  c  o  m*/
        File dir = new File(dest_file_dir);
        dir.mkdirs();
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        File source = new File(orig_file);
        File destination = new File(dest_file_dir + Navit_DEST_FILENAME);

        if (source.exists()) {
            FileInputStream fi = new FileInputStream(source);
            FileOutputStream fo = new FileOutputStream(destination);
            FileChannel src = fi.getChannel();
            FileChannel dst = fo.getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
            fi.close();
            fo.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.sakaiproject.assignment.tool.AssignmentAction.java

private byte[] readIntoBytes(InputStream zin, String fName, long length) throws IOException {

    byte[] buffer = new byte[4096];

    File f = File.createTempFile("asgnup", "tmp");

    FileOutputStream fout = new FileOutputStream(f);
    try {// ww w  .  ja  v  a2 s .  c  o m
        int len;
        while ((len = zin.read(buffer)) > 0) {
            fout.write(buffer, 0, len);
        }
        zin.close();
    } finally {
        try {
            fout.close(); // The file channel needs to be closed before the deletion.
        } catch (IOException ioException) {
            M_log.warn(this + "readIntoBytes: problem closing FileOutputStream " + ioException.getMessage());
        }
    }

    FileInputStream fis = new FileInputStream(f);
    FileChannel fc = fis.getChannel();
    byte[] data = null;
    try {
        data = new byte[(int) (fc.size())]; // fc.size returns the size of the file which backs the channel
        ByteBuffer bb = ByteBuffer.wrap(data);
        fc.read(bb);
    } finally {
        try {
            fc.close(); // The file channel needs to be closed before the deletion.
        } catch (IOException ioException) {
            M_log.warn(this + "readIntoBytes: problem closing FileChannel " + ioException.getMessage());
        }

        try {
            fis.close(); // The file inputstream needs to be closed before the deletion.
        } catch (IOException ioException) {
            M_log.warn(this + "readIntoBytes: problem closing FileInputStream " + ioException.getMessage());
        }
    }

    //remove the file
    f.delete();

    return data;
}

From source file:com.clark.func.Functions.java

/**
 * Internal copy file method./*from  w w w.ja  v  a  2s.  c  o m*/
 * 
 * @param srcFile
 *            the validated source file, must not be <code>null</code>
 * @param destFile
 *            the validated destination file, must not be <code>null</code>
 * @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");
    }

    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) > FIFTY_MB ? FIFTY_MB : (size - pos);
            pos += output.transferFrom(input, pos, count);
        }
    } finally {
        closeQuietly(output);
        closeQuietly(fos);
        closeQuietly(input);
        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());
    }
}