code: public static long copyStream(InputStream input, OutputStream output, boolean closeStreams)
              throws RuntimeException {
          long numBytesCopied = 0;
          int bufferSize = BUFFER_SIZE;
          try {
              // make sure we buffer the input
              input = new BufferedInputStream(input, bufferSize);
              byte[] buffer = new byte[bufferSize];
              for (int bytesRead = input.read(buffer); bytesRead != -1; bytesRead = input.read(buffer)) {
                  output.write(buffer, 0, bytesRead);
                  numBytesCopied += bytesRead;
              }
              output.flush();
          } catch (IOException ioe) {
              throw new RuntimeException("Stream data cannot be copied", ioe);
          } finally {
              if (closeStreams) {
                  try {
                      output.close();
                  } catch (IOException ioe2) {
                      // what to do?
                  }
                  try {
                      input.close();
                  } catch (IOException ioe2) {
                      // what to do?
                  }
              }
          }
          return numBytesCopied;
      }
docs: Copies one stream to another, optionally closing the streams.
  
  @param input the data to copy
  @param output where to copy the data
  @param closeStreams if true input and output will be closed when the method returns
  @return the number of bytes copied
  @throws RuntimeException if the copy failed