Java InputStream to OutputStream copyStream(InputStream input, OutputStream output)

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

Description

Copies data from one stream to another.

License

Open Source License

Parameter

Parameter Description
input The input stream.
output The output stream.

Exception

Parameter Description
IOException An exception if an error occurs while processing either ofthe streams.

Return

The number of bytes copied.

Declaration

static public int copyStream(InputStream input, OutputStream output) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:// w w  w  .ja  v a 2 s.co m
 * IBM Corporation - initial API and implementation
 * yyyymmdd bug      Email and other contact information
 * -------- -------- -----------------------------------------------------------
 * 20060330   124667 kathy@ca.ibm.com - Kathy Chan
 * 20060421   136761 rsinha@ca.ibm.com - Rupam Kuehner
 * 20060424   115690 sengpl@ca.ibm.com - Seng Phung-Lu
 * 20060503   126819 rsinha@ca.ibm.com - Rupam Kuehner
 * 20080122   215866 trungha@ca.ibm.com - Trung Ha
 * 20080303   218696 ericdp@ca.ibm.com - Eric D. Peters, APIs using EJBArtifactEdit not able to deal with some EJB 3.0 beans properly
 * 20080626   236645 kathy@ca.ibm.com - Kathy Chan
 * 20110824   355771 kchong@ca.ibm.com - Keith Chong, Remove unused/dead code as reported in build reports 
 *******************************************************************************/

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

public class Main {
    /**
     * Copies data from one stream to another.
     * 
     * @param input
     *            The input stream.
     * @param output
     *            The output stream.
     * @return The number of bytes copied.
     * @throws IOException
     *             An exception if an error occurs while processing either of
     *             the streams.
     */
    static public int copyStream(InputStream input, OutputStream output) throws IOException {
        int t = 0;
        byte[] buffer = new byte[1024];
        int n = input.read(buffer);
        while (n >= 0) {
            output.write(buffer, 0, n);
            t += n;
            n = input.read(buffer);
        }
        return t;
    }
}

Related

  1. copyStream(InputStream in, OutputStream out, String end)
  2. copyStream(InputStream input, OutputStream output)
  3. copyStream(InputStream input, OutputStream output)
  4. copyStream(InputStream input, OutputStream output)
  5. copyStream(InputStream input, OutputStream output)
  6. copyStream(InputStream input, OutputStream output)
  7. copyStream(InputStream input, OutputStream output)
  8. copyStream(InputStream input, OutputStream output)
  9. copyStream(InputStream input, OutputStream output)