Java FileChannel Copy copy(FileInputStream in, FileOutputStream out)

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

Description

Copies bytes from a 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 int copy(FileInputStream in, FileOutputStream out) throws IOException 

Method Source Code

//package com.java2s;
/*/* ww w . ja va 2 s . c o  m*/
 * $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 <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 int copy(FileInputStream in, FileOutputStream out) throws IOException {
        final long count = copyLarge(in, out);
        return count > Integer.MAX_VALUE ? -1 : (int) count;
    }

    /**
     * Copies bytes from an <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 int copy(InputStream in, OutputStream out, byte[] buffer) throws IOException {
        final long count = copyLarge(in, out, buffer);
        return count > Integer.MAX_VALUE ? -1 : (int) count;
    }

    /**
     * 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. copy(FileInputStream inputStream, FileOutputStream outputStream)
  2. copy(FileInputStream iStream, FileOutputStream oStream)
  3. copy(final File aCopyFrom, final File aCopyTo)
  4. copy(final File fromFile, final File toFile)