2828
2929import com .android .internal .util .ProcFileReader ;
3030import com .google .android .collect .Lists ;
31- import com .google .android .collect .Maps ;
3231import com .google .android .collect .Sets ;
3332
3433import java .io .BufferedReader ;
3736import java .io .FileReader ;
3837import java .io .IOException ;
3938import java .util .ArrayList ;
40- import java .util .HashMap ;
4139import java .util .HashSet ;
4240import java .util .StringTokenizer ;
4341
@@ -63,22 +61,6 @@ public class NetworkStatsFactory {
6361 /** Path to {@code /proc/net/xt_qtaguid/stats}. */
6462 private final File mStatsXtUid ;
6563
66- /** {@link #mStatsXtUid} and {@link #mStatsXtIfaceAll} headers. */
67- private static final String KEY_IDX = "idx" ;
68- private static final String KEY_IFACE = "iface" ;
69- private static final String KEY_ACTIVE = "active" ;
70- private static final String KEY_UID = "uid_tag_int" ;
71- private static final String KEY_COUNTER_SET = "cnt_set" ;
72- private static final String KEY_TAG_HEX = "acct_tag_hex" ;
73- private static final String KEY_SNAP_RX_BYTES = "snap_rx_bytes" ;
74- private static final String KEY_SNAP_RX_PACKETS = "snap_rx_packets" ;
75- private static final String KEY_SNAP_TX_BYTES = "snap_tx_bytes" ;
76- private static final String KEY_SNAP_TX_PACKETS = "snap_tx_packets" ;
77- private static final String KEY_RX_BYTES = "rx_bytes" ;
78- private static final String KEY_RX_PACKETS = "rx_packets" ;
79- private static final String KEY_TX_BYTES = "tx_bytes" ;
80- private static final String KEY_TX_PACKETS = "tx_packets" ;
81-
8264 public NetworkStatsFactory () {
8365 this (new File ("/proc/" ));
8466 }
@@ -112,44 +94,34 @@ private NetworkStats readNetworkStatsSummarySingleFile() {
11294 final NetworkStats stats = new NetworkStats (SystemClock .elapsedRealtime (), 6 );
11395 final NetworkStats .Entry entry = new NetworkStats .Entry ();
11496
115- // TODO: transition to ProcFileReader
116- // TODO: read directly from proc once headers are added
117- final ArrayList <String > keys = Lists .newArrayList (KEY_IFACE , KEY_ACTIVE , KEY_SNAP_RX_BYTES ,
118- KEY_SNAP_RX_PACKETS , KEY_SNAP_TX_BYTES , KEY_SNAP_TX_PACKETS , KEY_RX_BYTES ,
119- KEY_RX_PACKETS , KEY_TX_BYTES , KEY_TX_PACKETS );
120- final ArrayList <String > values = Lists .newArrayList ();
121- final HashMap <String , String > parsed = Maps .newHashMap ();
122-
123- BufferedReader reader = null ;
97+ ProcFileReader reader = null ;
12498 try {
125- reader = new BufferedReader (new FileReader (mStatsXtIfaceAll ));
99+ reader = new ProcFileReader (new FileInputStream (mStatsXtIfaceAll ));
126100
127- String line ;
128- while ((line = reader .readLine ()) != null ) {
129- splitLine (line , values );
130- parseLine (keys , values , parsed );
131-
132- entry .iface = parsed .get (KEY_IFACE );
101+ while (reader .hasMoreData ()) {
102+ entry .iface = reader .nextString ();
133103 entry .uid = UID_ALL ;
134104 entry .set = SET_DEFAULT ;
135105 entry .tag = TAG_NONE ;
136106
107+ final boolean active = reader .nextInt () != 0 ;
108+
137109 // always include snapshot values
138- entry .rxBytes = getParsedLong ( parsed , KEY_SNAP_RX_BYTES );
139- entry .rxPackets = getParsedLong ( parsed , KEY_SNAP_RX_PACKETS );
140- entry .txBytes = getParsedLong ( parsed , KEY_SNAP_TX_BYTES );
141- entry .txPackets = getParsedLong ( parsed , KEY_SNAP_TX_PACKETS );
110+ entry .rxBytes = reader . nextLong ( );
111+ entry .rxPackets = reader . nextLong ( );
112+ entry .txBytes = reader . nextLong ( );
113+ entry .txPackets = reader . nextLong ( );
142114
143115 // fold in active numbers, but only when active
144- final boolean active = getParsedInt (parsed , KEY_ACTIVE ) != 0 ;
145116 if (active ) {
146- entry .rxBytes += getParsedLong ( parsed , KEY_RX_BYTES );
147- entry .rxPackets += getParsedLong ( parsed , KEY_RX_PACKETS );
148- entry .txBytes += getParsedLong ( parsed , KEY_TX_BYTES );
149- entry .txPackets += getParsedLong ( parsed , KEY_TX_PACKETS );
117+ entry .rxBytes += reader . nextLong ( );
118+ entry .rxPackets += reader . nextLong ( );
119+ entry .txBytes += reader . nextLong ( );
120+ entry .txPackets += reader . nextLong ( );
150121 }
151122
152123 stats .addValues (entry );
124+ reader .finishLine ();
153125 }
154126 } catch (NullPointerException e ) {
155127 throw new IllegalStateException ("problem parsing stats: " + e );
@@ -315,18 +287,6 @@ public NetworkStats readNetworkStatsDetail(int limitUid) throws IllegalStateExce
315287 return stats ;
316288 }
317289
318- @ Deprecated
319- private static int getParsedInt (HashMap <String , String > parsed , String key ) {
320- final String value = parsed .get (key );
321- return value != null ? Integer .parseInt (value ) : 0 ;
322- }
323-
324- @ Deprecated
325- private static long getParsedLong (HashMap <String , String > parsed , String key ) {
326- final String value = parsed .get (key );
327- return value != null ? Long .parseLong (value ) : 0 ;
328- }
329-
330290 /**
331291 * Split given line into {@link ArrayList}.
332292 */
@@ -340,21 +300,6 @@ private static void splitLine(String line, ArrayList<String> outSplit) {
340300 }
341301 }
342302
343- /**
344- * Zip the two given {@link ArrayList} as key and value pairs into
345- * {@link HashMap}.
346- */
347- @ Deprecated
348- private static void parseLine (
349- ArrayList <String > keys , ArrayList <String > values , HashMap <String , String > outParsed ) {
350- outParsed .clear ();
351-
352- final int size = Math .min (keys .size (), values .size ());
353- for (int i = 0 ; i < size ; i ++) {
354- outParsed .put (keys .get (i ), values .get (i ));
355- }
356- }
357-
358303 /**
359304 * Utility method to read a single plain-text {@link Long} from the given
360305 * {@link File}, usually from a {@code /proc/} filesystem.
0 commit comments