try {
HttpResponse response = client.getStreamResponse(song);
is = new BufferedInputStream(response.getEntity().getContent(), INPUT_BUFFER_LEN);
os = new FileOutputStream(tempFile);
bus.post(new DownloadSongProgressChangedEvent(this, song, 0, 1));
byte[] buffer = new byte[INPUT_BUFFER_LEN];
long len = response.getEntity().getContentLength();
int read;
int readTotal = 0;
float lastReportedProgress = 0;
while ((read = is.read(buffer)) != -1) {
os.write(buffer, 0, read);
readTotal += read;
// Only emit events for >1% change
float newProgress = (float) readTotal / len;
if (((newProgress - 0.01f) >= lastReportedProgress) || newProgress == 1.0f) {
bus.post(new DownloadSongProgressChangedEvent(this,
song,
readTotal,
(int) len));
lastReportedProgress = newProgress;
}