Java InputStream Copy copyLarge(InputStream input, OutputStream output)

Here you can find the source of copyLarge(InputStream input, OutputStream output)

Description

Copy bytes from a large (over 2GB) InputStream to an OutputStream.

License

Open Source License

Parameter

Parameter Description
input the <code>InputStream</code> to read from
output the <code>OutputStream</code> to write to

Exception

Parameter Description
NullPointerException if the input or output is null
IOException if an I/O error occurs

Return

the number of bytes copied

Declaration

public static long copyLarge(InputStream input, OutputStream output) throws IOException 

Method Source Code

//package com.java2s;
/* //from www.  j a v a2  s .c  o m
 * SQLFileutils.java
 * 
 * Copyright (C) 2011
 * 
 * This file is part of Proyecto persistenceGeo
 * 
 * This software is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As a special exception, if you link this library with other files to
 * produce an executable, this library does not by itself cause the
 * resulting executable to be covered by the GNU General Public License.
 * This exception does not however invalidate any other reasons why the
 * executable file might be covered by the GNU General Public License.
 * 
 * Authors:: Alejandro D?az Torres (mailto:adiaz@emergya.com)
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    /**
     * The default buffer size to use.
     */
    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

    /**
     * Copy bytes from a large (over 2GB) <code>InputStream</code> to an
     * <code>OutputStream</code>.
     * <p>
     * This method buffers the input internally, so there is no need to use a
     * <code>BufferedInputStream</code>.
     * 
     * @param input
     *            the <code>InputStream</code> to read from
     * @param output
     *            the <code>OutputStream</code> to write to
     * @return the number of bytes copied
     * @throws NullPointerException
     *             if the input or output is null
     * @throws IOException
     *             if an I/O error occurs
     * @since Commons IO 1.3
     */
    public static long copyLarge(InputStream input, OutputStream output) throws IOException {
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        long count = 0;
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }
}

Related

  1. copyLarge(InputStream input, OutputStream output)
  2. copyLarge(InputStream input, OutputStream output)
  3. copyLarge(InputStream input, OutputStream output)
  4. copyLarge(InputStream input, OutputStream output)
  5. copyLarge(InputStream input, OutputStream output)
  6. copyLarge(InputStream input, OutputStream output, byte[] buffer)
  7. copyLarge(InputStream input, OutputStream output, byte[] buffer)
  8. copyLarge(InputStream input, OutputStream output, byte[] buffer)
  9. copyLarge(InputStream input, OutputStream output, final long inputOffset, final long length, byte[] buffer)