Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URLConnection low level byte counting

I'm trying to obtain the lowest level of byte counting possible with URLConnection. I already went to count the data passing by the two streams, with CountingInputStream and CountingOutputStream from the Apache Commons IO but the byte counting I get there is equal to the body size I'm sending + response body size I'm receiving back.

In HttpClient, I had a MeasuringClientConnManager with what I presume being a more low level byte counting.

To get the most exact value, I'm comparing the counting on those libraries with those methods, using TrafficStats:

public static long getCurrentRx(Context context){
    int uid = context.getApplicationInfo().uid;
    return TrafficStats.getUidRxBytes(uid);
}

public static long getCurrentTx(Context context){
    int uid = context.getApplicationInfo().uid;
    return TrafficStats.getUidTxBytes(uid);
}

public static long getCurrentNetworkUsage(Context context){
    return getCurrentRx(context) + getCurrentTx(context);
}
public static long getDiffNetworkUsage(Context context, long previousNetworkUsage){
    return getCurrentNetworkUsage(context) - previousNetworkUsage;
}

Using those methods I obtained much closer values with HttpClient than URLConnection.

Two questions:

  1. How come on URLConnection, I don't see on the OutputStream some byte size indication the headers are sent? How come I see there only the body size? Where are the headers? The url call? The DNS resolving bytes?

  2. Is there a way to measure every byte that a call to the URLConnection library sends on the network interface? The lowest level, the better!

like image 599
galex Avatar asked Nov 08 '15 16:11

galex


1 Answers

URLConnection is designed to abstract out the reading (and writing) of the headers. The input and output streams of that class only represent the body; there are specific routines for manipulating the headers so that those don't have to be directly written.

If you look under the hood in the Sun/Oracle JRE, you'll see that their implementation of HttpURLConnection is using their own version of an HTTP client. That client is doing the full reading and writing of the entire content.

You might be able to write your own implementation of URLConnection that mimics what Sun/Oracle (and Apache) are doing, but at the same time tracks the number of bytes read/written. I wager that's a bit complicated however.

like image 51
Steven M. Wurster Avatar answered Nov 20 '22 04:11

Steven M. Wurster