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

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

Description

copy Stream

License

Open Source License

Declaration

public static void copyStream(InputStream is, OutputStream os) 

Method Source Code


//package com.java2s;

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

public class Main {
    public static void copyStream(InputStream is, OutputStream os) {
        try {/*from  w  ww  . j  a  va 2s .c om*/
            byte buf[] = new byte[8192];
            int rd = -1;

            while ((rd = is.read(buf)) != -1)
                os.write(buf, 0, rd);
        } catch (Exception e) {
            throw new RuntimeException("Stream copy failed", e);
        } finally {
            closeStream(os);
            closeStream(is);
        }
    }

    public static void closeStream(InputStream is) {
        if (is == null)
            return;
        try {
            is.close();
        } catch (IOException e) {
        }
    }

    public static void closeStream(OutputStream os) {
        if (os == null)
            return;
        try {
            os.close();
        } catch (IOException e) {
        }
    }
}

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)