diff options
5 files changed, 33 insertions, 28 deletions
| diff --git a/core/java/android/net/NetworkStatsHistory.java b/core/java/android/net/NetworkStatsHistory.java index bf25602041cf..f41306301d42 100644 --- a/core/java/android/net/NetworkStatsHistory.java +++ b/core/java/android/net/NetworkStatsHistory.java @@ -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); diff --git a/services/core/java/com/android/server/net/NetworkIdentitySet.java b/services/core/java/com/android/server/net/NetworkIdentitySet.java index 2326ad39fd27..bce80696f72c 100644 --- a/services/core/java/com/android/server/net/NetworkIdentitySet.java +++ b/services/core/java/com/android/server/net/NetworkIdentitySet.java @@ -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 { diff --git a/services/core/java/com/android/server/net/NetworkStatsCollection.java b/services/core/java/com/android/server/net/NetworkStatsCollection.java index c4beddd42eaf..6aefe41891f9 100644 --- a/services/core/java/com/android/server/net/NetworkStatsCollection.java +++ b/services/core/java/com/android/server/net/NetworkStatsCollection.java @@ -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 diff --git a/services/core/java/com/android/server/net/NetworkStatsRecorder.java b/services/core/java/com/android/server/net/NetworkStatsRecorder.java index ce741693cb4b..978ae87d39d5 100644 --- a/services/core/java/com/android/server/net/NetworkStatsRecorder.java +++ b/services/core/java/com/android/server/net/NetworkStatsRecorder.java @@ -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);          }      } diff --git a/tests/net/java/com/android/server/net/NetworkStatsCollectionTest.java b/tests/net/java/com/android/server/net/NetworkStatsCollectionTest.java index 89146f945e1f..435c3c0af817 100644 --- a/tests/net/java/com/android/server/net/NetworkStatsCollectionTest.java +++ b/tests/net/java/com/android/server/net/NetworkStatsCollectionTest.java @@ -64,7 +64,6 @@ import org.junit.runner.RunWith;  import java.io.ByteArrayInputStream;  import java.io.ByteArrayOutputStream; -import java.io.DataOutputStream;  import java.io.File;  import java.io.FileOutputStream;  import java.io.InputStream; @@ -124,7 +123,7 @@ public class NetworkStatsCollectionTest {          // now export into a unified format          final ByteArrayOutputStream bos = new ByteArrayOutputStream(); -        collection.write(new DataOutputStream(bos)); +        collection.write(bos);          // clear structure completely          collection.reset(); @@ -152,7 +151,7 @@ public class NetworkStatsCollectionTest {          // now export into a unified format          final ByteArrayOutputStream bos = new ByteArrayOutputStream(); -        collection.write(new DataOutputStream(bos)); +        collection.write(bos);          // clear structure completely          collection.reset(); @@ -180,7 +179,7 @@ public class NetworkStatsCollectionTest {          // now export into a unified format          final ByteArrayOutputStream bos = new ByteArrayOutputStream(); -        collection.write(new DataOutputStream(bos)); +        collection.write(bos);          // clear structure completely          collection.reset(); |