public static void copyStreamSynchronously(InputStream in,OutputStream out,boolean closeOutputStream) throws IOException {
  byte[] buffer=new byte[1024 * 20];
  try {
    int length;
    while ((length=in.read(buffer)) != -1) {
      out.write(buffer,0,length);
    }
    out.flush();
  }
  finally {
    if (closeOutputStream && out != null) {
      try {
        out.close();
      }
 catch (      IOException ex) {
      }
    }
    if (in != null) {
      try {
        in.close();
      }
 catch (      IOException ex) {
      }
    }
  }
}

public static void copyStreamSynchronously ( InputStream in , OutputStream out , boolean closeOutputStream ) throws IOException { byte [ ] buffer = new byte [ 1024 * 20 ] ; try { int length ; while ( ( length = in . read ( buffer ) ) != - 1 ) { out . write ( buffer , 0 , length ) ; } out . flush ( ) ; } finally { if ( closeOutputStream && out != null ) { try { out . close ( ) ; } catch ( IOException ex ) { } } if ( in != null ) { try { in . close ( ) ; } catch ( IOException ex ) { } } } }
copies the contents read from the input stream to the output stream in the current thread . both streams will be closed , even in case of a failure .
