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:
Jeff Sharkey
2021-01-04 22:35:57 -07:00
parent be4248226c
commit 1f69d15939
4 changed files with 30 additions and 24 deletions

View File

@@ -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 {

View File

@@ -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

View File

@@ -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);
}
}