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

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

Description

Copies bytes from one stream to another

License

Apache License

Parameter

Parameter Description
input The InputStream
output The OutputStream

Return

The number of bytes copied

Declaration

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

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

import java.io.OutputStream;

public class Main {
    /**/*  w  w w  .  jav  a  2 s  . com*/
     *** Copies bytes from one stream to another
     *** 
     * @param input
     *            The InputStream
     *** @param output
     *            The OutputStream
     *** @return The number of bytes copied
     **/
    public static int copyStreams(InputStream input, OutputStream output) throws IOException {
        return copyStreams(input, output, -1);
    }

    /**
     *** Copies bytes from one stream to another
     *** 
     * @param input
     *            The InputStream
     *** @param output
     *            The OutputStream
     *** @param maxLen
     *            The maximum number of bytes to copy
     *** @return The number of bytes copied
     **/
    public static int copyStreams(InputStream input, OutputStream output, int maxLen) throws IOException {

        /* copy nothing? */
        if (maxLen == 0) {
            return 0;
        }

        /* copy bytes */
        int length = 0; // count of bytes copied
        byte tmpBuff[] = new byte[10 * 1024]; // 10K blocks
        while (true) {

            /* read length */
            int readLen;
            if (maxLen >= 0) {
                readLen = maxLen - length;
                if (readLen == 0) {
                    break; // done reading
                } else if (readLen > tmpBuff.length) {
                    readLen = tmpBuff.length; // max block size
                }
            } else {
                readLen = tmpBuff.length;
            }

            /* read input stream */
            int cnt = input.read(tmpBuff, 0, readLen);

            /* copy to output stream */
            if (cnt < 0) {
                if ((maxLen >= 0) && (length != maxLen)) {
                    // Print.logError("Copy size mismatch: " + maxLen + " => " +
                    // length);
                }
                break;
            } else if (cnt > 0) {
                output.write(tmpBuff, 0, cnt);
                length += cnt;
                if ((maxLen >= 0) && (length >= maxLen)) {
                    break; // per 'maxLen', done copying
                }
            } else {
                // Print.logDebug("Read 0 bytes ... continuing");
            }

        }
        output.flush();

        /* return number of bytes copied */
        return length;
    }
}

Related

  1. copyStreams(InputStream from, OutputStream to, int blockSize)
  2. copyStreams(InputStream in, OutputStream out)
  3. copyStreams(InputStream in, OutputStream out)
  4. copyStreams(InputStream in, OutputStream out)
  5. copyStreams(InputStream in, OutputStream out, int buf)
  6. copyStreams(InputStream inputStream, OutputStream outputStream)
  7. copyStreams(InputStream is, OutputStream os)
  8. copyStreams(InputStream source, OutputStream destination)
  9. copyStreams(InputStream source, OutputStream target)