Java InputStream to OutputStream inputStreamToOutputStream(final InputStream in, final OutputStream out, int buffer)

Here you can find the source of inputStreamToOutputStream(final InputStream in, final OutputStream out, int buffer)

Description

Copies the content of an InputStream to an OutputStream.

License

Open Source License

Parameter

Parameter Description
in <code>InputStream</code> from which data is read.
out <code>OutputStream</code> to which data is written.
buffer the number of bytes to buffer reading.

Exception

Parameter Description
IOException an exception

Return

number of bytes read/written.

Declaration

public static long inputStreamToOutputStream(final InputStream in, final OutputStream out, int buffer)
        throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2014 Alexander Kerner. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License./* w  w  w .jav a 2s .  c o  m*/
 ******************************************************************************/

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

import java.io.OutputStream;

public class Main {
    /**
     * The JCL book specifies the default buffer size as 8K characters.
     */
    public static final int DEFAULT_BUFFER = 8192;

    /**
     * Copies the content of an <code>InputStream</code> to an
     * <code>OutputStream</code>. Will flush the <code>OutputStream</code>, but
     * won't close <code>InputStream</code> or <code>OutputStream</code>.
     *
     * @param in
     *            <code>InputStream</code> from which data is read.
     * @param out
     *            <code>OutputStream</code> to which data is written.
     * @return number of bytes read/written.
     * @throws IOException
     */
    public static long inputStreamToOutputStream(final InputStream in, final OutputStream out) throws IOException {
        return inputStreamToOutputStream(in, out, DEFAULT_BUFFER);
    }

    /**
     * Copies the content of an <code>InputStream</code> to an
     * <code>OutputStream</code>. Will flush the <code>OutputStream</code>, but
     * won't close <code>InputStream</code> or <code>OutputStream</code>.
     *
     * @param in
     *            <code>InputStream</code> from which data is read.
     * @param out
     *            <code>OutputStream</code> to which data is written.
     * @param buffer
     *            the number of bytes to buffer reading.
     * @return number of bytes read/written.
     * @throws IOException
     */
    public static long inputStreamToOutputStream(final InputStream in, final OutputStream out, int buffer)
            throws IOException {
        if (buffer < 1)
            buffer = DEFAULT_BUFFER;
        final byte[] byteBuffer = new byte[buffer];
        long count = 0;
        int n = 0;
        while ((n = in.read(byteBuffer)) != -1) {
            out.write(byteBuffer, 0, n);
            count += n;
        }
        out.flush();
        return count;
    }
}

Related

  1. copyStreamToFile(InputStream stream, String outputFilePath)
  2. copyStreamToFileBinary(final InputStream stream, final File output)
  3. copyStreamToStream(final InputStream input, final OutputStream output)
  4. copyStreamToStream(InputStream in, OutputStream out)
  5. inputStreamToOutputStream(final InputStream in, final OutputStream out)
  6. inputStreamToOutputStream(final InputStream inputStream, final OutputStream outputStream)
  7. inputStreamToOutputStream(InputStream inStream, OutputStream outStream)
  8. inputStreamToOutputStream(InputStream is, OutputStream os)