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 InputStream to read from
output the OutputStream to write to

Exception

Parameter Description
IOException if an I/O error occurs

Return

the number of bytes copied

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 MadRobot.//from w ww.  ja v a2 s .c om
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/

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

import java.io.OutputStream;

public class Main {
    public static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

    /**
     * Copy bytes from a large (over 2GB) InputStream to an OutputStream.
     * 
     * This method buffers the input internally, so there is no need to use a BufferedInputStream.
     * 
     * @param input
     *            the InputStream to read from
     * @param output
     *            the OutputStream to write to
     * @return the number of bytes copied
     * @throws IOException
     *             if an I/O error occurs
     */
    private 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)
  7. copyLarge(InputStream input, OutputStream output)
  8. copyLarge(InputStream input, OutputStream output)
  9. copyLarge(InputStream input, OutputStream output)