Java InputStream to OutputStream copyStream(InputStream source, OutputStream dest)

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

Description

Copy stream from source to destination

License

Open Source License

Parameter

Parameter Description
source a parameter
dest a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void copyStream(InputStream source, OutputStream dest) throws IOException 

Method Source Code

//package com.java2s;
/**/*from w w w  .j a v a 2s  .co  m*/
 * Copyright (c) 2012 Todoroo Inc
 *
 * See the file "LICENSE" for the full license governing this code.
 */

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

import java.io.OutputStream;

public class Main {
    /**
     * Copy stream from source to destination
     * @param source
     * @param dest
     * @throws IOException
     */
    public static void copyStream(InputStream source, OutputStream dest) throws IOException {
        int bytes;
        byte[] buffer;
        int BUFFER_SIZE = 1024;
        buffer = new byte[BUFFER_SIZE];
        while ((bytes = source.read(buffer)) != -1) {
            if (bytes == 0) {
                bytes = source.read();
                if (bytes < 0)
                    break;
                dest.write(bytes);
                dest.flush();
                continue;
            }

            dest.write(buffer, 0, bytes);
            dest.flush();
        }
    }
}

Related

  1. copyStream(InputStream is, OutputStream os)
  2. copyStream(InputStream is, OutputStream os)
  3. copyStream(InputStream is, OutputStream os, boolean closeInput)
  4. copyStream(InputStream is, OutputStream os, int bufferSize)
  5. copyStream(InputStream is, OutputStream os, long maxLength)
  6. copyStream(InputStream source, OutputStream dest)
  7. copyStream(InputStream source, OutputStream dest)
  8. copyStream(InputStream source, OutputStream destination, byte[] buffer)
  9. copyStream(InputStream sourceInputStream, OutputStream targetOutputStream)