Skip to content

fix(android): stream full file body, not just first 8 KB segment - #474

Open
HuuNguyen312 wants to merge 1 commit into
RonRadtke:masterfrom
HuuNguyen312:fix/download-interrupted-okio-sink
Open

fix(android): stream full file body, not just first 8 KB segment#474
HuuNguyen312 wants to merge 1 commit into
RonRadtke:masterfrom
HuuNguyen312:fix/download-interrupted-okio-sink

Conversation

@HuuNguyen312

Copy link
Copy Markdown

Problem

On Android, downloading any file larger than 8 KB to disk fails with
"Download interrupted." (0.24.10 regression). Files ≤ 8 KB pass.

Root cause

ProgressReportingSource.read() writes each chunk to the destination file via
ofStream but never forwards the bytes into the Okio sink. Buffered
consumers (byteStream() / source().read()) read from that sink, so after the
first 8 KB segment the buffer is empty and they hit a false EOF.

isDownloadComplete() then compares a truncated bytesDownloaded (8192) against
the real Content-Length → not equal → "Download interrupted.".

Fix

Forward the chunk into the sink too, so buffered reads advance to the real end of
stream. One line:

if (read > 0) {
    ofStream.write(bytes, 0, (int) read);
    sink.write(bytes, 0, (int) read); // honor Okio Source contract
}   

Verified

Real device: a 284 KB PDF over CloudFront previously aborted at exactly 8192 bytes
(tempBytes: 8192); with the fix it streams the full body and completes.

ProgressReportingSource.read() wrote each chunk to the destination file
via ofStream but never forwarded the bytes into the Okio sink. Buffered
consumers (byteStream() / source().read()) read from that sink, so after
the first 8 KB segment the buffer was empty and they saw a false EOF.

isDownloadComplete() then compared a truncated bytesDownloaded (8192)
against the real Content-Length and reported "Download interrupted." for
any file larger than one segment. Files <= 8 KB happened to pass.

Forward the chunk into the sink as well so buffered reads advance to the
real end of stream.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant