Pivot network statistics to use DataInput/Output.
Using these generalized interfaces is more flexible, since it enables us to pivot the implementation being used internally. In particular, an upcoming CL will pivot them to use a more efficient alternative. This is a no-op refactoring. Bug: 176777285 Test: atest FrameworksNetTests CtsNetTestCases Change-Id: Ibd4717174cf1f136e9d5d80172ecb6e493265306
This commit is contained in:
@@ -45,8 +45,8 @@ import com.android.internal.util.IndentingPrintWriter;
|
||||
import libcore.util.EmptyArray;
|
||||
|
||||
import java.io.CharArrayWriter;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.ProtocolException;
|
||||
@@ -162,7 +162,7 @@ public class NetworkStatsHistory implements Parcelable {
|
||||
out.writeLong(totalBytes);
|
||||
}
|
||||
|
||||
public NetworkStatsHistory(DataInputStream in) throws IOException {
|
||||
public NetworkStatsHistory(DataInput in) throws IOException {
|
||||
final int version = in.readInt();
|
||||
switch (version) {
|
||||
case VERSION_INIT: {
|
||||
@@ -204,7 +204,7 @@ public class NetworkStatsHistory implements Parcelable {
|
||||
}
|
||||
}
|
||||
|
||||
public void writeToStream(DataOutputStream out) throws IOException {
|
||||
public void writeToStream(DataOutput out) throws IOException {
|
||||
out.writeInt(VERSION_ADD_ACTIVE);
|
||||
out.writeLong(bucketDuration);
|
||||
writeVarLongArray(out, bucketStart, bucketCount);
|
||||
@@ -768,7 +768,7 @@ public class NetworkStatsHistory implements Parcelable {
|
||||
*/
|
||||
public static class DataStreamUtils {
|
||||
@Deprecated
|
||||
public static long[] readFullLongArray(DataInputStream in) throws IOException {
|
||||
public static long[] readFullLongArray(DataInput in) throws IOException {
|
||||
final int size = in.readInt();
|
||||
if (size < 0) throw new ProtocolException("negative array size");
|
||||
final long[] values = new long[size];
|
||||
@@ -781,7 +781,7 @@ public class NetworkStatsHistory implements Parcelable {
|
||||
/**
|
||||
* Read variable-length {@link Long} using protobuf-style approach.
|
||||
*/
|
||||
public static long readVarLong(DataInputStream in) throws IOException {
|
||||
public static long readVarLong(DataInput in) throws IOException {
|
||||
int shift = 0;
|
||||
long result = 0;
|
||||
while (shift < 64) {
|
||||
@@ -797,7 +797,7 @@ public class NetworkStatsHistory implements Parcelable {
|
||||
/**
|
||||
* Write variable-length {@link Long} using protobuf-style approach.
|
||||
*/
|
||||
public static void writeVarLong(DataOutputStream out, long value) throws IOException {
|
||||
public static void writeVarLong(DataOutput out, long value) throws IOException {
|
||||
while (true) {
|
||||
if ((value & ~0x7FL) == 0) {
|
||||
out.writeByte((int) value);
|
||||
@@ -809,7 +809,7 @@ public class NetworkStatsHistory implements Parcelable {
|
||||
}
|
||||
}
|
||||
|
||||
public static long[] readVarLongArray(DataInputStream in) throws IOException {
|
||||
public static long[] readVarLongArray(DataInput in) throws IOException {
|
||||
final int size = in.readInt();
|
||||
if (size == -1) return null;
|
||||
if (size < 0) throw new ProtocolException("negative array size");
|
||||
@@ -820,7 +820,7 @@ public class NetworkStatsHistory implements Parcelable {
|
||||
return values;
|
||||
}
|
||||
|
||||
public static void writeVarLongArray(DataOutputStream out, long[] values, int size)
|
||||
public static void writeVarLongArray(DataOutput out, long[] values, int size)
|
||||
throws IOException {
|
||||
if (values == null) {
|
||||
out.writeInt(-1);
|
||||
|
||||
@@ -20,8 +20,8 @@ import android.net.NetworkIdentity;
|
||||
import android.service.NetworkIdentitySetProto;
|
||||
import android.util.proto.ProtoOutputStream;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
|
||||
@@ -44,7 +44,7 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
|
||||
public NetworkIdentitySet() {
|
||||
}
|
||||
|
||||
public NetworkIdentitySet(DataInputStream in) throws IOException {
|
||||
public NetworkIdentitySet(DataInput in) throws IOException {
|
||||
final int version = in.readInt();
|
||||
final int size = in.readInt();
|
||||
for (int i = 0; i < size; i++) {
|
||||
@@ -89,7 +89,7 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
|
||||
}
|
||||
}
|
||||
|
||||
public void writeToStream(DataOutputStream out) throws IOException {
|
||||
public void writeToStream(DataOutput out) throws IOException {
|
||||
out.writeInt(VERSION_ADD_DEFAULT_NETWORK);
|
||||
out.writeInt(size());
|
||||
for (NetworkIdentity ident : this) {
|
||||
@@ -143,7 +143,7 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void writeOptionalString(DataOutputStream out, String value) throws IOException {
|
||||
private static void writeOptionalString(DataOutput out, String value) throws IOException {
|
||||
if (value != null) {
|
||||
out.writeByte(1);
|
||||
out.writeUTF(value);
|
||||
@@ -152,7 +152,7 @@ public class NetworkIdentitySet extends HashSet<NetworkIdentity> implements
|
||||
}
|
||||
}
|
||||
|
||||
private static String readOptionalString(DataInputStream in) throws IOException {
|
||||
private static String readOptionalString(DataInput in) throws IOException {
|
||||
if (in.readByte() != 0) {
|
||||
return in.readUTF();
|
||||
} else {
|
||||
|
||||
@@ -63,12 +63,15 @@ import com.google.android.collect.Lists;
|
||||
import com.google.android.collect.Maps;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.DataInput;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutput;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.ProtocolException;
|
||||
import java.time.ZonedDateTime;
|
||||
@@ -82,7 +85,7 @@ import java.util.Objects;
|
||||
* Collection of {@link NetworkStatsHistory}, stored based on combined key of
|
||||
* {@link NetworkIdentitySet}, UID, set, and tag. Knows how to persist itself.
|
||||
*/
|
||||
public class NetworkStatsCollection implements FileRotator.Reader {
|
||||
public class NetworkStatsCollection implements FileRotator.Reader, FileRotator.Writer {
|
||||
/** File header magic number: "ANET" */
|
||||
private static final int FILE_MAGIC = 0x414E4554;
|
||||
|
||||
@@ -431,10 +434,10 @@ public class NetworkStatsCollection implements FileRotator.Reader {
|
||||
|
||||
@Override
|
||||
public void read(InputStream in) throws IOException {
|
||||
read(new DataInputStream(in));
|
||||
read((DataInput) new DataInputStream(in));
|
||||
}
|
||||
|
||||
public void read(DataInputStream in) throws IOException {
|
||||
private void read(DataInput in) throws IOException {
|
||||
// verify file magic header intact
|
||||
final int magic = in.readInt();
|
||||
if (magic != FILE_MAGIC) {
|
||||
@@ -468,7 +471,13 @@ public class NetworkStatsCollection implements FileRotator.Reader {
|
||||
}
|
||||
}
|
||||
|
||||
public void write(DataOutputStream out) throws IOException {
|
||||
@Override
|
||||
public void write(OutputStream out) throws IOException {
|
||||
write((DataOutput) new DataOutputStream(out));
|
||||
out.flush();
|
||||
}
|
||||
|
||||
private void write(DataOutput out) throws IOException {
|
||||
// cluster key lists grouped by ident
|
||||
final HashMap<NetworkIdentitySet, ArrayList<Key>> keysByIdent = Maps.newHashMap();
|
||||
for (Key key : mStats.keySet()) {
|
||||
@@ -497,8 +506,6 @@ public class NetworkStatsCollection implements FileRotator.Reader {
|
||||
history.writeToStream(out);
|
||||
}
|
||||
}
|
||||
|
||||
out.flush();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
|
||||
@@ -42,7 +42,6 @@ import com.google.android.collect.Sets;
|
||||
import libcore.io.IoUtils;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -375,7 +374,7 @@ public class NetworkStatsRecorder {
|
||||
|
||||
@Override
|
||||
public void write(OutputStream out) throws IOException {
|
||||
mCollection.write(new DataOutputStream(out));
|
||||
mCollection.write(out);
|
||||
mCollection.reset();
|
||||
}
|
||||
}
|
||||
@@ -412,7 +411,7 @@ public class NetworkStatsRecorder {
|
||||
|
||||
@Override
|
||||
public void write(OutputStream out) throws IOException {
|
||||
mTemp.write(new DataOutputStream(out));
|
||||
mTemp.write(out);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user