Java InputStream to OutputStream copyStream(InputStream in, OutputStream out, int bufferSize)

Here you can find the source of copyStream(InputStream in, OutputStream out, int bufferSize)

Description

Copies the input stream bytes into the output stream bytes, using the specified buffer size when transfering the bytes.

License

Apache License

Declaration

public static void copyStream(InputStream in, OutputStream out, int bufferSize) throws IOException 

Method Source Code

//package com.java2s;
/**/*  w ww.j  av a2 s  .  c om*/
 *    Copyright 2011 meltmedia
 *
 *    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.
 */

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

public class Main {
    /**
     * Copies the input stream bytes into the output stream bytes, using the specified
     * buffer size when transfering the bytes.  This method does not close the streams
     * when it completes.
     */
    public static void copyStream(InputStream in, OutputStream out, int bufferSize) throws IOException {
        byte[] buffer = new byte[bufferSize];
        int read = 0;
        while ((read = in.read(buffer)) >= 0) {
            out.write(buffer, 0, read);
        }
    }

    /**
     * Copies the content of a reader into a writer, using the specified buffer size while
     * transfering the characters.
     */
    public static void copyStream(Reader reader, Writer writer, int buffer_length) throws IOException {
        char[] buffer = new char[buffer_length];
        for (int read = reader.read(buffer); read > -1; read = reader.read(buffer))
            writer.write(buffer, 0, read);
    }
}

Related

  1. copyStream(InputStream in, OutputStream out)
  2. copyStream(InputStream in, OutputStream out)
  3. copyStream(InputStream in, OutputStream out)
  4. copyStream(InputStream in, OutputStream out, boolean closeIn, boolean closeOut)
  5. copyStream(InputStream in, OutputStream out, boolean closeOut)
  6. copyStream(InputStream in, OutputStream out, int bufsize)
  7. copyStream(InputStream in, OutputStream out, int len)
  8. copyStream(InputStream in, OutputStream out, JarEntry entry)
  9. copyStream(InputStream in, OutputStream out, long maxLen)