I’m trying to download a large text file using Retrofit, but I need to stream the file in chunks to avoid loading it entirely into memory. I’m working with large text files and want to ensure the file is streamed properly, but the entire response text is being loaded into memory.
I need to stream/load/read the data using responseBody.byteStream(). However, the data is still being loaded entirely into memory for text files, and the Content-Type: application/octet-stream is not bypassing this issue.
Here’s my setup:
@Headers("Accept-Encoding: *") // Avoid compression
@Streaming // Allows downloading the file in chunks, not loading it all in memory
@GET
suspend fun downloadImageResponse(
@Url imageUrl: String,
@Header("Range") range: String? = null // Optional Range header
): Response<ResponseBody>
Based on your request to extract and summarize the log details, here’s the key information from the provided log:
Extracted Log:
-
Request Details (GET request)
- Method:
GET
- URL:
http://192.168.232.85:3080/uploads/58286695/slick.txt
- HTTP Version:
HTTP/1.1
- Method:
-
Request Headers:
Accept-Encoding: *
(No compression)Host: 192.168.232.85:3080
Connection: Keep-Alive
Cookie: connect.sid=s%3AUr0COp_g9F-upWtqxdvTVHsgOFDhWazH.1rLvEfFqSKNG0BJTBEET3Pp%2F9grkWsk0wjkmZpajx%2Fo
User-Agent: okhttp/4.12.0
-
Response Details (200 OK):
- HTTP Status:
200 OK
- URL:
http://192.168.232.85:3080/uploads/58286695/slick.txt
- Response Time:
31ms
- HTTP Status:
-
Response Headers:
X-Powered-By: Express
Accept-Ranges: bytes
(Indicating support for range requests)Cache-Control: public, max-age=0
Last-Modified: Sun, 08 Dec 2024 10:33:35 GMT
ETag: W/"5-193a5d4efcb"
Content-Type: text/plain; charset=UTF-8
(Indicating plain text file)Content-Length: 5
(Indicating the file size is 5 bytes)Date: Sun, 08 Dec 2024 10:36:57 GMT
Connection: keep-alive
Keep-Alive: timeout=5
-
Response Body (Log Output):
- Body Content:
Hi da................
(This is the content of the text file being downloaded)
- Body Content: