I am downloading a file using an OkHttp GET request:
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
...
OkHttpClient okClient = new OkHttpClient();
Request request = Request.Builder().url(url).get();
Response response = okClient.newCall(request).execute();
I read from the response body and decorating it with a BufferedInputStream
, with a buffer size of 4096:
BufferedInputStream in = new BufferedInputStream(response.body().byteStream(), 4096);
However, when I try to read from the buffer, the first read returns 1179 bytes. After that, I am only able to read 2048 bytes at a time:
byte[] buffer = new byte[4096];
while (true) {
int bytesRead = in.read(buffer); //bytesRead is always 2048, except the first read
if (bytesRead == -1) break;
}
Several related questions:
- What could be causing the first read to return 1179 bytes? Some sort of file header?
- Why are reads from the
InputStream
being paged to a size of 2048 bytes, instead of the value specified by theBufferedInputStream
wrapper? - Is there a way to configure the
OkHttpClient
to read more than 2048 bytes from the buffer?