code: public static void copyStream( StreamMonitor monitor, URL sourceURL, int expected,
                                     InputStream source, OutputStream destination, boolean closeStreams
      )
          throws IOException, NullArgumentException
      {
          NullArgumentException.validateNotNull( source, "source" );
          NullArgumentException.validateNotNull( destination, "destination" );
  
          int length;
          int count = 0; // cumulative total read
          byte[] buffer = new byte[BUFFER_SIZE];
          BufferedOutputStream dest;
          if( destination instanceof BufferedOutputStream )
          {
              dest = (BufferedOutputStream) destination;
          }
          else
          {
              dest = new BufferedOutputStream( destination );
          }
          BufferedInputStream src;
          if( source instanceof BufferedInputStream )
          {
              src = (BufferedInputStream) source;
          }
          else
          {
              src = new BufferedInputStream( source );
          }
          try
          {
              while( ( length = src.read( buffer ) ) >= 0 )
              {
                  count = count + length;
                  dest.write( buffer, 0, length );
                  if( null != monitor )
                  {
                      monitor.notifyUpdate( sourceURL, expected, count );
                  }
              }
              dest.flush();
          }
          finally
          {
              if( closeStreams )
              {
                  closeStreams( src, dest );
              }
              if( null != monitor )
              {
                  monitor.notifyCompletion( sourceURL );
              }
          }
      }
docs: Copy a stream.
  
  @param sourceURL    the source url
  @param expected     the expected size in bytes
  @param source       the source input stream
  @param destination  the destination output stream
  @param closeStreams TRUE if the streams should be closed on completion
  @param monitor      The StreamMonitor to report progress to.
  
  @throws IOException           if an IO error occurs
  @throws NullArgumentException if either the src or dest arguments are null.