java - OkHttp3 body stream is empty in multithreaded environment -
i have class downloads file on http , saves file.
the class called own threadexecutor run 2 downloads in parallel.
unfortunately, response.body().source()
of request empty if data downloaded (checked charlesproxy), size of buffer
0.
seems okio
or okhttp
have problems being called 2 different threads because 1 thread in threadpool don't see behavior.
the network stack ohttp based (version 3.8.1).
private string downloaddatatofile(@nonnull string resourcename, @nonnull file localfile, @nonnull transfercanceller transfercanceller, response response, call call) throws ioexception, smartdriveexception { bufferedsource bufferedsource = null; outputstream outputstream = null; try { bufferedsource = response.body().source(); timber.i("start download " + url + " -> " + localfile); smartdrivefileutils.deleteifexists(localfile); // delete old file in case have have half downloaded createdirectoryifneeded(localfile); if (!localfile.createnewfile()) { timber.e("could not create new file download"); } outputstream = new fileoutputstream(localfile, false); long payloadsize = response.body().contentlength(); byte[] buffer = new byte[transfer_buffer_size]; try { while (!transfercanceller.iscancelled() && payloadsize != 0) { int read = bufferedsource.read(buffer); if (read == -1) { break; } outputstream.write(buffer, 0, read); } } catch (eofexception exception) { ignoreokhttpreadingfromemptystreamexception(exception); } return ontransferfinished(resourcename, localfile, transfercanceller, response, call); } { io.closequietly(bufferedsource); io.closequietly(outputstream); } }
Comments
Post a Comment