Java InputStream to OutputStream copyStream(InputStream source, OutputStream destination, byte[] buffer)

Here you can find the source of copyStream(InputStream source, OutputStream destination, byte[] buffer)

Description

Reads all data (until EOF is reached) from the given source to the destination stream.

License

EUPL

Parameter

Parameter Description
source The stream providing the data.
destination The stream that takes the data. If this is null, all data from source will be read and discarded.
buffer The buffer to use for forwarding. If it is null, the method allocates a buffer.

Declaration

private static void copyStream(InputStream source, OutputStream destination, byte[] buffer) throws IOException 

Method Source Code

//package com.java2s;
/*/*from   w w  w .  j av a  2  s.  co m*/
 * Copyright 2003 Federal Chancellery Austria
 * MOA-ID has been developed in a cooperation between BRZ, the Federal
 * Chancellery Austria - ICT staff unit, and Graz University of Technology.
 *
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
 * the European Commission - subsequent versions of the EUPL (the "Licence");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 * http://www.osor.eu/eupl/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 *
 * This product combines work with different licenses. See the "NOTICE" text
 * file for details on the various modules and licenses.
 * The "NOTICE" text file is part of the distribution. Any derivative works
 * that you distribute must include a readable copy of the "NOTICE" text file.
 */

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

public class Main {
    /**
     * Reads all data (until EOF is reached) from the given source to the 
     * destination stream. If the destination stream is null, all data is dropped.
     * It uses the given buffer to read data and forward it. If the buffer is 
     * null, this method allocates a buffer.
     *
     * @param source The stream providing the data.
     * @param destination The stream that takes the data. If this is null, all
     *                    data from source will be read and discarded.
     * @param buffer The buffer to use for forwarding. If it is null, the method
     *               allocates a buffer.
     * @exception IOException If reading from the source or writing to the 
     *                        destination fails.
     */
    private static void copyStream(InputStream source, OutputStream destination, byte[] buffer) throws IOException {
        if (source == null) {
            throw new NullPointerException("Argument \"source\" must not be null.");
        }
        if (buffer == null) {
            buffer = new byte[8192];
        }

        if (destination != null) {
            int bytesRead;
            while ((bytesRead = source.read(buffer)) >= 0) {
                destination.write(buffer, 0, bytesRead);
            }
        } else {
            while (source.read(buffer) >= 0)
                ;
        }
    }
}

Related

  1. copyStream(InputStream is, OutputStream os, int bufferSize)
  2. copyStream(InputStream is, OutputStream os, long maxLength)
  3. copyStream(InputStream source, OutputStream dest)
  4. copyStream(InputStream source, OutputStream dest)
  5. copyStream(InputStream source, OutputStream dest)
  6. copyStream(InputStream sourceInputStream, OutputStream targetOutputStream)
  7. copyStream(InputStream sourceStream, OutputStream destinationStream)
  8. copyStream(InputStream sourceStream, OutputStream destinationStream, boolean closeInput, boolean closeOutput)
  9. copyStream(InputStream src, OutputStream dest)