protected void updateProgressListener ( ProgressListener progressListener , Response response ) { InputStream responseStream = response . getResponseByteStream ( ) ; int bytesDownloaded = 0 ; long totalBytesExpected = response . getContentLength ( ) ; // Reading 2 KiB at a time to be consistent with the upload segment size in ProgressRequestBody final int segmentSize = 2048 ; byte [ ] responseBytes ; if ( totalBytesExpected > Integer . MAX_VALUE ) { logger . warn ( "The response body for " + getUrl ( ) + " is too large to hold in a byte array. Only the first 2 GiB will be available." ) ; responseBytes = new byte [ Integer . MAX_VALUE ] ; } else { responseBytes = new byte [ ( int ) totalBytesExpected ] ; } // For every 2 KiB downloaded: // 1) Call the user's ProgressListener // 2) Append the downloaded bytes into a byte array int nextByte ; try { while ( ( nextByte = responseStream . read ( ) ) != - 1 ) { if ( bytesDownloaded % segmentSize == 0 ) { progressListener . onProgress ( bytesDownloaded , totalBytesExpected ) ; } responseBytes [ bytesDownloaded ] = ( byte ) nextByte ; bytesDownloaded += 1 ; } } catch ( IOException e ) { logger . error ( "IO Exception: " + e . getMessage ( ) ) ; } // Transfer the downloaded data to the Response object so that the user can later retrieve it if ( response instanceof ResponseImpl ) { ( ( ResponseImpl ) response ) . setResponseBytes ( responseBytes ) ; } }
As a download request progresses periodically call the user s ProgressListener