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.mitre.ccv.mapred.GenerateFeatureVectors.java

/**
 * Returns a read only {@link MappedByteBuffer}.
 * //  w w w  .  ja  v a  2s . c  o m
 * @param input full <b>local</b>  path 
 * @throws java.io.IOException
 */
public static MappedByteBuffer getMappedByteBuffer(String input) throws IOException {
    FileInputStream fins = new FileInputStream(input);
    FileChannel channel = fins.getChannel();
    return channel.map(FileChannel.MapMode.READ_ONLY, 0, (int) channel.size());
}

From source file:Main.java

public static File copyFileTo(File src, File dst) {
    FileChannel in = null;
    FileChannel out = null;//ww w . ja v a2 s  .c  om
    FileInputStream inStream = null;
    FileOutputStream outStream = null;
    try {
        inStream = new FileInputStream(src);
        outStream = new FileOutputStream(dst);
        in = inStream.getChannel();
        out = outStream.getChannel();
        in.transferTo(0, in.size(), out);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
            if (inStream != null) {
                inStream.close();
            }
            if (outStream != null) {
                outStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return dst;
}

From source file:org.wso2.carbon.esb.vfs.transport.test.ESBJAVA3470.java

/**
 * Copy the given source file to the given destination
 *
 * @param sourceFile/*from w  w w . j  a  v a2 s .  c  om*/
 *                 source file
 * @param destFile
 *                 destination file
 * @throws IOException
 */
public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        destFile.createNewFile();
    }
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;

    try {
        fileInputStream = new FileInputStream(sourceFile);
        fileOutputStream = new FileOutputStream(destFile);

        FileChannel source = fileInputStream.getChannel();
        FileChannel destination = fileOutputStream.getChannel();
        destination.transferFrom(source, 0, source.size());
    } finally {
        IOUtils.closeQuietly(fileInputStream);
        IOUtils.closeQuietly(fileOutputStream);
    }
}

From source file:org.easysoa.registry.frascati.FileUtils.java

/**
 * Copy a file in an other file//  ww  w.ja  v  a2  s .c o m
 * 
 * @param source The source file
 * @param target The target file
 * @throws Exception If a problem occurs
 */
public static final void copyTo(File source, File target) throws Exception {
    if (source == null || target == null) {
        throw new IllegalArgumentException("Source and target files must not be null");
    }
    // Input and outputs channels
    log.debug("source file = " + source);
    log.debug("target file = " + target);
    FileChannel in = null;
    FileChannel out = null;
    try {
        // Init
        in = new FileInputStream(source).getChannel();
        out = new FileOutputStream(target).getChannel();
        // File copy
        in.transferTo(0, in.size(), out);
    } catch (Exception ex) {
        throw ex;
    } finally {
        // finally close all streams
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:Main.java

public static void fastFileCopy(File source, File target) {
    FileChannel in = null;
    FileChannel out = null;/* w ww  . jav  a2  s . c o m*/
    long start = System.currentTimeMillis();
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream(source);
        in = fis.getChannel();
        fos = new FileOutputStream(target);
        out = fos.getChannel();

        long size = in.size();
        long transferred = in.transferTo(0, size, out);

        while (transferred != size) {
            transferred += in.transferTo(transferred, size - transferred, out);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        close(fis);
        close(fos);
        close(in);
        close(out);
    }
    long end = System.currentTimeMillis();

    Log.d(target.getAbsolutePath(), nf.format(source.length()) + "B: "
            + ((end - start > 0) ? nf.format(source.length() / (end - start)) : 0) + " KB/s");
}

From source file:dk.netarkivet.common.utils.StreamUtils.java

/**
 * Will copy everything from input stream to output stream, closing input
 * stream afterwards./*from w ww. j av a2  s  . co m*/
 *
 * @param in  Inputstream to copy from
 * @param out Outputstream to copy to
 * @throws ArgumentNotValid if either parameter is null
 * @throws IOFailure if a read or write error happens during copy
 */
public static void copyInputStreamToOutputStream(InputStream in, OutputStream out) {
    ArgumentNotValid.checkNotNull(in, "InputStream in");
    ArgumentNotValid.checkNotNull(out, "OutputStream out");

    try {
        try {
            if (in instanceof FileInputStream && out instanceof FileOutputStream) {
                FileChannel inChannel = ((FileInputStream) in).getChannel();
                FileChannel outChannel = ((FileOutputStream) out).getChannel();
                long transferred = 0;
                final long fileLength = inChannel.size();
                do {
                    transferred += inChannel.transferTo(transferred,
                            Math.min(Constants.IO_CHUNK_SIZE, fileLength - transferred), outChannel);
                } while (transferred < fileLength);
            } else {
                byte[] buf = new byte[Constants.IO_BUFFER_SIZE];
                int bytesRead;
                while ((bytesRead = in.read(buf)) != -1) {
                    out.write(buf, 0, bytesRead);
                }
            }
            out.flush();
        } finally {
            in.close();
        }
    } catch (IOException e) {
        String errMsg = "Trouble copying inputstream " + in + " to outputstream " + out;
        log.warn(errMsg, e);
        throw new IOFailure(errMsg, e);
    }
}

From source file:ValidateLicenseHeaders.java

/**
 * Add the default jboss lgpl header//from  w  w w .j a v  a  2  s  .  co  m
 */
static void addDefaultHeader(File javaFile) throws IOException {
    if (addDefaultHeader) {
        FileInputStream fis = new FileInputStream(javaFile);
        FileChannel fc = fis.getChannel();
        int size = (int) fc.size();
        ByteBuffer contents = ByteBuffer.allocate(size);
        fc.read(contents);
        fis.close();

        ByteBuffer hdr = ByteBuffer.wrap(DEFAULT_HEADER.getBytes());
        FileOutputStream fos = new FileOutputStream(javaFile);
        fos.write(hdr.array());
        fos.write(contents.array());
        fos.close();
    }

    noheaders.add(javaFile);
}

From source file:com.twentyoneechoes.borges.util.Utils.java

public static void backupDatabase(Context ctx) {
    try {/*  w  w w  .j  av  a 2  s .c  om*/
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + ctx.getApplicationContext().getPackageName()
                    + "//databases//borges.db";
            String backupDBPath = "borges.db";
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);

            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(currentDB).getChannel();
                FileChannel dst = new FileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            }
        }
    } catch (Exception e) {
        Log.i(Utils.class.getSimpleName(), "Unable to backup database");
    }
}

From source file:org.apache.hadoop.hdfs.server.namenode.TestFSEditLogLoader.java

/**
 * Return the length of bytes in the given file after subtracting
 * the trailer of 0xFF (OP_INVALID)s.//  ww w .  j a v  a  2  s  . com
 * This seeks to the end of the file and reads chunks backwards until
 * it finds a non-0xFF byte.
 * @throws IOException if the file cannot be read
 */
private static long getNonTrailerLength(File f) throws IOException {
    final int chunkSizeToRead = 256 * 1024;
    FileInputStream fis = new FileInputStream(f);
    try {

        byte buf[] = new byte[chunkSizeToRead];

        FileChannel fc = fis.getChannel();
        long size = fc.size();
        long pos = size - (size % chunkSizeToRead);

        while (pos >= 0) {
            fc.position(pos);

            int readLen = (int) Math.min(size - pos, chunkSizeToRead);
            IOUtils.readFully(fis, buf, 0, readLen);
            for (int i = readLen - 1; i >= 0; i--) {
                if (buf[i] != FSEditLogOpCodes.OP_INVALID.getOpCode()) {
                    return pos + i + 1; // + 1 since we count this byte!
                }
            }

            pos -= chunkSizeToRead;
        }
        return 0;
    } finally {
        fis.close();
    }
}

From source file:org.codehaus.preon.Codecs.java

public static <T> T decode(Codec<T> codec, Builder builder, File file)
        throws FileNotFoundException, IOException, DecodingException {
    FileInputStream in = null;//from  w w  w .j a  v a2 s.c o m
    FileChannel channel = null;
    try {
        in = new FileInputStream(file);
        channel = in.getChannel();
        int fileSize = (int) channel.size();
        ByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, fileSize);
        return decode(codec, buffer, builder);
    } finally {
        if (channel != null) {
            channel.close();
        }
    }
}