Signature streaming from local file, property to disable incremental.

+Tests

Test: atest PackageManagerShellCommandTest PackageManagerShellCommandIncrementalTest
Bug: b/136132412 b/133435829

Change-Id: I826900e120c72e7cdd0549c70da28d817982dcd3
diff --git a/core/java/android/os/incremental/V4Signature.java b/core/java/android/os/incremental/V4Signature.java
index 5fadee4..6450a67 100644
--- a/core/java/android/os/incremental/V4Signature.java
+++ b/core/java/android/os/incremental/V4Signature.java
@@ -16,8 +16,11 @@
 
 package android.os.incremental;
 
+import java.io.ByteArrayOutputStream;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 
 /**
@@ -26,20 +29,36 @@
  * @hide
  */
 public class V4Signature {
+    public static final String EXT = ".idsig";
+
     public final byte[] verityRootHash;
     public final byte[] v3Digest;
     public final byte[] pkcs7SignatureBlock;
 
-    V4Signature(byte[] verityRootHash, byte[] v3Digest, byte[] pkcs7SignatureBlock) {
-        this.verityRootHash = verityRootHash;
-        this.v3Digest = v3Digest;
-        this.pkcs7SignatureBlock = pkcs7SignatureBlock;
+    /**
+     * Construct a V4Signature from .idsig file.
+     */
+    public static V4Signature readFrom(File file) {
+        try (DataInputStream stream = new DataInputStream(new FileInputStream(file))) {
+            return readFrom(stream);
+        } catch (IOException e) {
+            return null;
+        }
     }
 
-    static byte[] readBytes(DataInputStream stream) throws IOException {
-        byte[] result = new byte[stream.readInt()];
-        stream.read(result);
-        return result;
+    /**
+     * Store the V4Signature to a byte-array.
+     */
+    public byte[] toByteArray() {
+        try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
+            try (DataOutputStream steam = new DataOutputStream(byteArrayOutputStream)) {
+                this.writeTo(steam);
+                steam.flush();
+            }
+            return byteArrayOutputStream.toByteArray();
+        } catch (IOException e) {
+            return null;
+        }
     }
 
     static V4Signature readFrom(DataInputStream stream) throws IOException {
@@ -49,9 +68,10 @@
         return new V4Signature(verityRootHash, v3Digest, pkcs7SignatureBlock);
     }
 
-    static void writeBytes(DataOutputStream stream, byte[] bytes) throws IOException {
-        stream.writeInt(bytes.length);
-        stream.write(bytes);
+    V4Signature(byte[] verityRootHash, byte[] v3Digest, byte[] pkcs7SignatureBlock) {
+        this.verityRootHash = verityRootHash;
+        this.v3Digest = v3Digest;
+        this.pkcs7SignatureBlock = pkcs7SignatureBlock;
     }
 
     void writeTo(DataOutputStream stream) throws IOException {
@@ -59,4 +79,15 @@
         writeBytes(stream, this.v3Digest);
         writeBytes(stream, this.pkcs7SignatureBlock);
     }
+
+    private static byte[] readBytes(DataInputStream stream) throws IOException {
+        byte[] result = new byte[stream.readInt()];
+        stream.read(result);
+        return result;
+    }
+
+    private static void writeBytes(DataOutputStream stream, byte[] bytes) throws IOException {
+        stream.writeInt(bytes.length);
+        stream.write(bytes);
+    }
 }