code: public static void pipeStream(InputStream in, OutputStream out, int bufSize)
              throws IOException {
          try {
              byte[] buf = new byte[bufSize];
              int len;
              while ( ( len = in.read( buf ) ) > 0 ) {
                  out.write( buf, 0, len );
              }
          } finally {
              try {
                  in.close();
                  out.close();
              } catch (IOException e) {
                  System.err.println("WARNING: Could not close stream.");
              }
          }
      }
docs: Copies the contents of an InputStream to an OutputStream, then closes
  both.
  
  @param in The source stream.
  @param out The target stram.
  @param bufSize Number of bytes to attempt to copy at a time.
  @throws IOException If any sort of read/write error occurs on either
  stream.