Java InputStream to OutputStream copyStream(InputStream is, OutputStream os)

Here you can find the source of copyStream(InputStream is, OutputStream os)

Description

Copy contents of one stream onto another

License

Open Source License

Parameter

Parameter Description
is input stream
os output stream

Declaration

public static long copyStream(InputStream is, OutputStream os) throws IOException 

Method Source Code


//package com.java2s;
/*/*from   w ww .  ja v a  2s  .com*/
 * ? Copyright IBM Corp. 2012-2013
 * 
 * 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.IOException;
import java.io.InputStream;

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

public class Main {
    /**
     * Copy contents of one stream onto another
     * @param is input stream
     * @param os output stream
     * @ibm-api
     */
    public static long copyStream(InputStream is, OutputStream os) throws IOException {
        return copyStream(is, os, 8192);
    }

    /**
     * Copy contents of one stream onto another
     * @param is input stream
     * @param os output stream
     * @param bufferSize size of buffer to use for copy
     * 
     * Note: there are cases where InputStream.available() returns > 0 but in
     * actual fact the stream won't be able to read anything, so we need to
     * handle the fact that InputStream.read() may return -1
     * @ibm-api
     */
    public static long copyStream(InputStream is, OutputStream os, int bufferSize) throws IOException {
        byte[] buffer = new byte[bufferSize];
        long totalBytes = 0;
        int readBytes;
        while ((readBytes = is.read(buffer)) > 0) {
            os.write(buffer, 0, readBytes);
            totalBytes += readBytes;
        }
        return totalBytes;
    }

    /**
     * Copy contents of one stream onto another
     * @param is input stream
     * @param os output stream
     * @ibm-api
     */
    public static long copyStream(Reader is, Writer os) throws IOException {
        return copyStream(is, os, 8192);
    }

    /**
     * Copy contents of one character stream onto another
     * @param is input stream
     * @param os output stream
     * @param bufferSize size of buffer to use for copy
     * 
     * Note: there are cases where InputStream.available() returns > 0 but in
     * actual fact the stream won't be able to read anything, so we need to
     * handle the fact that InputStream.read() may return -1
     * @ibm-api
     */
    public static long copyStream(Reader is, Writer os, int bufferSize) throws IOException {
        char[] buffer = new char[bufferSize];
        long totalBytes = 0;
        int readBytes;
        while ((readBytes = is.read(buffer)) > 0) {
            os.write(buffer, 0, readBytes);
            totalBytes += readBytes;
        }
        return totalBytes;
    }
}

Related

  1. copyStream(InputStream is, OutputStream os)
  2. copyStream(InputStream is, OutputStream os)
  3. copyStream(InputStream is, OutputStream os)
  4. copyStream(InputStream is, OutputStream os)
  5. copyStream(InputStream is, OutputStream os)
  6. copyStream(InputStream is, OutputStream os)
  7. CopyStream(InputStream is, OutputStream os)
  8. copyStream(InputStream is, OutputStream os)
  9. copyStream(InputStream is, OutputStream os, boolean closeInput)