question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ProtocolException: unexpected end of stream while downloading a file

See original GitHub issue

Using a library which uses okhttp to download files, I am usually getting an unexpected end of stream error while downloading large files (thrown at the end of the download). I am using the following code and URL to reproduce the error:

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://speedtest-sfo1.digitalocean.com/100mb.test").build();
client.newCall(request).enqueue(new Callback() {
	public void onFailure(Call call, IOException e) {
		e.printStackTrace();
	}

	public void onResponse(Call call, Response response) throws IOException {
		if (!response.isSuccessful()) {
			throw new IOException("Failed to download file: " + response);
		}

		FileOutputStream fos = new FileOutputStream(downloadDirectory + File.separator + "100mb.test");
		fos.write(response.body().bytes());
		fos.close();
	}
});

URL: http://speedtest-sfo1.digitalocean.com/100mb.test (This is not the only URL the error is thrown on)

The code has been tested on API 22, 25, 26, 27. The error is not thrown always, I tested it 10 times and got the error 6 times, 4 times the file got successfully downloaded and saved…

I have read about the error and I know that it gets thrown when the servers sends incorrect Content-Length, not this time though. The file is exactly 100MB and the server supplies its correct length. I am not sending any extra request headers, the response headers are:

Server => nginx/1.13.3
Date => Sat, 09 Dec 2017 17:19:31 GMT
Content-Type => text/plain
Content-Length => 104857600
Last-Modified => Mon, 27 Nov 2017 09:11:00 GMT
Connection => close
ETag => "5a1bd6a4-6400000"
Access-Control-Allow-Origin => *
Access-Control-Allow-Headers => Origin,X-RequestedWith,Content-Type,Range
Access-Control-Allow-Methods => GET, OPTIONS
Accept-Ranges => bytes
Cache-Control => max-age=0, no-cache, no-store, mustrevalidate, no-transform

The complete error:

Callback failure for call to http://speedtest-sfo1.digitalocean.com/...
java.net.ProtocolException: unexpected end of stream
    at okhttp3.internal.http1.Http1Codec$FixedLengthSource.read(Http1Codec.java:406)
    at okio.Buffer.writeAll(Buffer.java:1005)
    at okio.RealBufferedSource.readByteArray(RealBufferedSource.java:107)
    at okhttp3.ResponseBody.bytes(ResponseBody.java:136)
    at dm.com.downloadmanager.MainActivity$3.onResponse(MainActivity.java:273)
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
    at java.lang.Thread.run(Thread.java:764)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:48 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
Shelmanxiecommented, Dec 5, 2018

hello ,I fix this by changing Apache configuration. just like the picture shows below. image

1reaction
skyzhwcommented, Sep 17, 2021

In a specific series of models + a specific server + a specific Wifi network, the probability of this problem is very high, and it is also related to the network usage at the time. The problem I encountered is that the probability of occurrence is very high in Huawei mobile phones, Alibaba Cloud CDN and my company’s Wifi. After verification, it is found that the default value of the ReceiveBufferSize of the socket in the Huawei system is 2097152, and the default value of the Samsung system is 1048576. The guess is related to the system tuning of this parameter. Adjust this parameter to below 49152, the probability of this problem is very low, and it is almost difficult to reproduce. You can also try if you encounter similar problems. Core code: new OkHttpClient.Builder().socketFactory(), socket.setReceiveBufferSize(49152);

Read more comments on GitHub >

github_iconTop Results From Across the Web

unexpected end of stream, AsyncTask File Download failed on ...
I am downloading audio files by using AsyncTask. Files are downloading on high speed internet like wifi but app gives error with end...
Read more >
Java.Net.ProtocolException: 'unexpected end of stream' I got ...
It is an error thrown by okhttp, have you changed the http client implementation in Project Options => Android Options? https://github.com/ ...
Read more >
ProtocolException: unexpected end of stream while ...
Using a library which uses okhttp to download files, I am usually getting an unexpected end of stream error while downloading large files...
Read more >
What does “unexpected end of stream” exception ... - Quora
It means that it detected the end of a stream before it should have ended. There are many reasons for this. For example,...
Read more >
Multipart form file send writeFully fails with "java.net ...
ProtocolException : unexpected end of stream" ... sending file in form, code below works (file fitting the buff ).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found