Hi I'm running a simple download test to see how fast my connection is. I record the trafficStats rx value, then I download a 5MB file, record the rx value afterwards then calculate the difference. I know this file is exactly 5MB.
Here is the code:
URL url = new URL(urlString);
URLConnection urlConnection = url.openConnection();
urlConnection.setConnectTimeout(connectTimeout);
urlConnection.setReadTimeout(readTimeout);
InputStream is = urlConnection.getInputStream();
// BufferedInputStream bis = new BufferedInputStream(is);
InputStream bis = new BufferedInputStream(is, 8192);
int inputFileSize = 1024;
byte[] baf = new byte[inputFileSize];
totalRxBeforeTest = TrafficStats.getUidRxBytes(uid);
int current = 0;
while ((current = bis.read(baf)) != -1) {
}
totalRxAfterTest = TrafficStats.getUidRxBytes(uid);
rxDiff = totalRxAfterTest - totalRxBeforeTest;
The issue is, that I should be getting a value of around 5242880 bytes (5MB), but instead I'm getting values like 5414730, 5408715, 5413867, 5426647. (From 4 test runs) I would expect there to be some overhead but this is an extra 168KB!! That seams like a lot of overhead for a 5MB file. I'm using TrafficStats by uid as well, so it should just be network stats from my particular application.
Any idea what could account for this extra traffic?