Java FileChannel Copy copyLarge(FileInputStream in, FileOutputStream out)

Here you can find the source of copyLarge(FileInputStream in, FileOutputStream out)

Description

Copies bytes from a large (over 2GB) FileInputStream to a FileOutputStream.

License

Open Source License

Parameter

Parameter Description
in the source
out the destination

Exception

Parameter Description
IOException if one occurs while reading or writing

Declaration

public static long copyLarge(FileInputStream in, FileOutputStream out) throws IOException 

Method Source Code

//package com.java2s;
/*/*from w ww .jav a  2s.com*/
 * $Id$
 *
 * Copyright (c) 2007-2010 by Joel Uckelman
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License (LGPL) as published by the Free Software Foundation.
 *
 * 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, copies are available
 * at http://www.opensource.org.
 */

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 {
    /**
     * Copies bytes from a large (over 2GB) <code>FileInputStream</code> to a
     * <code>FileOutputStream</code>.
     *
     * This method uses channels. The input file should not be written
     * to during the copy.
     *
     * @param in the source
     * @param out the destination
     * @throws IOException if one occurs while reading or writing
     */
    public static long copyLarge(FileInputStream in, FileOutputStream out) throws IOException {
        final FileChannel inc = in.getChannel();
        return inc.transferTo(0L, inc.size(), out.getChannel());
    }

    /**
     * Copies bytes from a large (over 2GB) <code>InputStream</code> to an
     * <code>OutputStream</code> via a <code>byte</code> buffer. This
     * method buffers input internally, so the input stream should not
     * be a <code>BufferedInputStream</code>.
     *
     * @param in the source
     * @param out the destination
     * @param buffer the buffer
     * @return the number of bytes copied
     * @throws IOException if one occurs while reading or writing
     */
    public static long copyLarge(InputStream in, OutputStream out, byte[] buffer) throws IOException {
        long count = 0;
        int n = 0;
        while ((n = in.read(buffer)) != -1) {
            out.write(buffer, 0, n);
            count += n;
        }
        return count;
    }

    /**
     * Reads from an {@link InputStream} to a byte array. This will always
     * completely fill the byte array, unless there are no more bytes to
     * read from the stream.
     *
     * @param in the input stream from which to read
     * @param buf the byte array to fill
     * @return the number of bytes read, of <code>-1</code> if at the end of
     * the stream
     *
     * @throws IOException if one occurs while reading
     */
    public static int read(InputStream in, byte[] buf) throws IOException {
        int num;
        int off = 0;
        while (off < buf.length && (num = in.read(buf, off, buf.length - off)) != -1) {
            off += num;
        }

        // This will read at least one byte if there are any to be read,
        // so bytes read cannot be zero.
        return off == 0 ? -1 : off;
    }
}

Related

  1. copyFileUsingFileChannels(File source, File dest)
  2. copyFileUsingFileChannels(File source, File dest)
  3. copyFolder(File fin, File fout)
  4. copyFromZip(ZipFile zipFile, String locationInBundle, File destination)
  5. copyInternal(FileInputStream in, FileOutputStream out)
  6. copyNio(final File source_file, final File destination_file)
  7. copyRec(File src, File dst)
  8. copyStream(InputStream in, long length, File out)
  9. copyStreamToFile(InputStream inStream, File file)