Java FileChannel Copy fCopy(FileInputStream src, File dest)

Here you can find the source of fCopy(FileInputStream src, File dest)

Description

f Copy

License

Open Source License

Declaration

public static void fCopy(FileInputStream src, File dest) throws IOException 

Method Source Code

//package com.java2s;
/**//from  w w w . j a  v  a  2s  .c  o m
(C) Copyright 1998-2007 Hewlett-Packard Development Company, LP
    
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
    
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    
For more information: www.smartfrog.org
*/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;

public class Main {
    public static final int BUF_SIZE = 50000;

    public static void fCopy(FileInputStream src, File dest) throws IOException {
        if (null == src || null == dest) {
            //TODO
        }

        FileChannel srcChannel = src.getChannel();
        FileChannel dstChannel = new FileOutputStream(dest).getChannel();
        dstChannel.transferFrom(srcChannel, 0, srcChannel.size());

        // Close the channels
        srcChannel.close();
        dstChannel.close();
    }

    /**
     * Copies an <code>InputStream</code> to a file 
     * 
     * @param in stream to copy from 
     * @param outputFile file to copy to
     * @return the number of bytes copied
     * @throws IOException if an I/O error occurs (may result in partially done work)  
     */
    public static long fCopy(InputStream in, File outputFile) throws IOException {
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(outputFile);
            return fCopy(in, out);
        } finally {
            if (out != null) {
                try {
                    in.close();
                    out.close();
                } catch (IOException e) {
                }
            }
        }
    }

    /**
     * Copies an <code>InputStream</code> to an <code>OutputStream</code> using a local internal buffer for performance.
     * Compared to {@link #globalBufferCopy(InputStream, OutputStream)} this method allows for better
     * concurrency, but each time it is called generates a buffer which will be garbage.
     * 
     * @param in stream to copy from 
     * @param out stream to copy to
     * @return the number of bytes copied
     * @throws IOException if an I/O error occurs (may result in partially done work)  
     * @see #globalBufferCopy(InputStream, OutputStream)
     */
    public static long fCopy(InputStream in, OutputStream out) throws IOException {
        // we need a buffer of our own, so no one else interferes
        byte[] buf = new byte[BUF_SIZE];
        return copy(in, out, buf);
    }

    /**
     * Copies an <code>InputStream</code> to an <code>OutputStream</code> using the specified buffer. 
     * 
     * @param in stream to copy from 
     * @param out stream to copy to
     * @param copyBuffer buffer used for copying
     * @return the number of bytes copied
     * @throws IOException if an I/O error occurs (may result in partially done work)  
     * @see #globalBufferCopy(InputStream, OutputStream)
     * @see #copy(InputStream, OutputStream)
     */
    public static long copy(InputStream in, OutputStream out, byte[] copyBuffer) throws IOException {
        long bytesCopied = 0;
        int read = -1;

        while ((read = in.read(copyBuffer, 0, copyBuffer.length)) != -1) {
            out.write(copyBuffer, 0, read);
            bytesCopied += read;
        }
        return bytesCopied;
    }
}

Related

  1. copyWithChannels(File aSourceFile, File aTargetFile, boolean aAppend)
  2. directoryCopy(URL source, URL destination)
  3. doCopyDirectory(File srcDir, File destDir, boolean preserveFileDate, List exclusionList)
  4. doCopyFile(File srcFile, File destFile, boolean preserveFileDate)
  5. doCopyFile(File srcFile, File destFile, boolean preserveFileDate)
  6. fileChannelCopy(File src, File dest)
  7. fileCopy(String sourceFolder, String destinationFolder)
  8. fileStreamCopy(FileInputStream in, FileOutputStream out, boolean closeOutput)
  9. nioCopy(File source, File target, FilenameFilter filter)