DO NOT MERGE - qt-qpr1-dev-plus-aosp-without-vendor@5915889 into stage-aosp-master

Bug: 142003500
Change-Id: I373a34c61c7c3e7673afa334cc138321a6304c65
diff --git a/src/main/java/com/android/apksig/internal/util/RandomAccessFileDataSource.java b/src/main/java/com/android/apksig/internal/util/FileChannelDataSource.java
similarity index 88%
rename from src/main/java/com/android/apksig/internal/util/RandomAccessFileDataSource.java
rename to src/main/java/com/android/apksig/internal/util/FileChannelDataSource.java
index 9c75d26..e4a421a 100644
--- a/src/main/java/com/android/apksig/internal/util/RandomAccessFileDataSource.java
+++ b/src/main/java/com/android/apksig/internal/util/FileChannelDataSource.java
@@ -27,7 +27,7 @@
 /**
  * {@link DataSource} backed by a {@link FileChannel} for {@link RandomAccessFile} access.
  */
-public class RandomAccessFileDataSource implements DataSource {
+public class FileChannelDataSource implements DataSource {
 
     private static final int MAX_READ_CHUNK_SIZE = 1024 * 1024;
 
@@ -36,28 +36,24 @@
     private final long mSize;
 
     /**
-     * Constructs a new {@code RandomAccessFileDataSource} based on the data contained in the
+     * Constructs a new {@code FileChannelDataSource} based on the data contained in the
      * whole file. Changes to the contents of the file, including the size of the file,
      * will be visible in this data source.
      */
-    public RandomAccessFileDataSource(RandomAccessFile file) {
-        mChannel = file.getChannel();
+    public FileChannelDataSource(FileChannel channel) {
+        mChannel = channel;
         mOffset = 0;
         mSize = -1;
     }
 
     /**
-     * Constructs a new {@code RandomAccessFileDataSource} based on the data contained in the
+     * Constructs a new {@code FileChannelDataSource} based on the data contained in the
      * specified region of the provided file. Changes to the contents of the file will be visible in
      * this data source.
      *
      * @throws IndexOutOfBoundsException if {@code offset} or {@code size} is negative.
      */
-    public RandomAccessFileDataSource(RandomAccessFile file, long offset, long size) {
-        this(file.getChannel(), offset, size);
-    }
-
-    private RandomAccessFileDataSource(FileChannel channel, long offset, long size) {
+    public FileChannelDataSource(FileChannel channel, long offset, long size) {
         if (offset < 0) {
             throw new IndexOutOfBoundsException("offset: " + size);
         }
@@ -83,14 +79,14 @@
     }
 
     @Override
-    public RandomAccessFileDataSource slice(long offset, long size) {
+    public FileChannelDataSource slice(long offset, long size) {
         long sourceSize = size();
         checkChunkValid(offset, size, sourceSize);
         if ((offset == 0) && (size == sourceSize)) {
             return this;
         }
 
-        return new RandomAccessFileDataSource(mChannel, mOffset + offset, size);
+        return new FileChannelDataSource(mChannel, mOffset + offset, size);
     }
 
     @Override
diff --git a/src/main/java/com/android/apksig/util/DataSources.java b/src/main/java/com/android/apksig/util/DataSources.java
index 00b89d7..1f0b40b 100644
--- a/src/main/java/com/android/apksig/util/DataSources.java
+++ b/src/main/java/com/android/apksig/util/DataSources.java
@@ -17,9 +17,10 @@
 package com.android.apksig.util;
 
 import com.android.apksig.internal.util.ByteBufferDataSource;
-import com.android.apksig.internal.util.RandomAccessFileDataSource;
+import com.android.apksig.internal.util.FileChannelDataSource;
 import java.io.RandomAccessFile;
 import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
 
 /**
  * Utility methods for working with {@link DataSource} abstraction.
@@ -44,10 +45,7 @@
      * file, including changes to size of file, will be visible in the data source.
      */
     public static DataSource asDataSource(RandomAccessFile file) {
-        if (file == null) {
-            throw new NullPointerException();
-        }
-        return new RandomAccessFileDataSource(file);
+        return asDataSource(file.getChannel());
     }
 
     /**
@@ -55,9 +53,28 @@
      * Changes to the file will be visible in the data source.
      */
     public static DataSource asDataSource(RandomAccessFile file, long offset, long size) {
-        if (file == null) {
+        return asDataSource(file.getChannel(), offset, size);
+    }
+
+    /**
+     * Returns a {@link DataSource} backed by the provided {@link FileChannel}. Changes to the
+     * file, including changes to size of file, will be visible in the data source.
+     */
+    public static DataSource asDataSource(FileChannel channel) {
+        if (channel == null) {
             throw new NullPointerException();
         }
-        return new RandomAccessFileDataSource(file, offset, size);
+        return new FileChannelDataSource(channel);
+    }
+
+    /**
+     * Returns a {@link DataSource} backed by the provided region of the {@link FileChannel}.
+     * Changes to the file will be visible in the data source.
+     */
+    public static DataSource asDataSource(FileChannel channel, long offset, long size) {
+        if (channel == null) {
+            throw new NullPointerException();
+        }
+        return new FileChannelDataSource(channel, offset, size);
     }
 }
diff --git a/src/test/java/com/android/apksig/internal/util/RandomAccessFileDataSourceTest.java b/src/test/java/com/android/apksig/internal/util/FileChannelDataSourceTest.java
similarity index 93%
rename from src/test/java/com/android/apksig/internal/util/RandomAccessFileDataSourceTest.java
rename to src/test/java/com/android/apksig/internal/util/FileChannelDataSourceTest.java
index 304f1bf..9578926 100644
--- a/src/test/java/com/android/apksig/internal/util/RandomAccessFileDataSourceTest.java
+++ b/src/test/java/com/android/apksig/internal/util/FileChannelDataSourceTest.java
@@ -31,14 +31,14 @@
 import org.junit.runners.JUnit4;
 
 @RunWith(JUnit4.class)
-public class RandomAccessFileDataSourceTest {
+public class FileChannelDataSourceTest {
     @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();
 
     @Test
     public void testFeedsCorrectData_whenFilePartiallyReadFromBeginning() throws Exception {
         byte[] fullFileContent = createFileContent(1024 * 1024 + 987654);
         RandomAccessFile raf = createRaf(fullFileContent);
-        DataSource rafDataSource = new RandomAccessFileDataSource(raf);
+        DataSource rafDataSource = new FileChannelDataSource(raf.getChannel());
 
         ByteArrayDataSink dataSink = new ByteArrayDataSink();
 
@@ -56,7 +56,7 @@
     public void testFeedsCorrectData_whenFilePartiallyReadWithOffset() throws Exception {
         byte[] fullFileContent = createFileContent(1024 * 1024 + 987654);
         RandomAccessFile raf = createRaf(fullFileContent);
-        DataSource rafDataSource = new RandomAccessFileDataSource(raf);
+        DataSource rafDataSource = new FileChannelDataSource(raf.getChannel());
 
         ByteArrayDataSink dataSink = new ByteArrayDataSink();
 
@@ -75,7 +75,7 @@
     public void testFeedsCorrectData_whenSeveralMbRead() throws Exception {
         byte[] fullFileContent = createFileContent(3 * 1024 * 1024 + 987654);
         RandomAccessFile raf = createRaf(fullFileContent);
-        DataSource rafDataSource = new RandomAccessFileDataSource(raf);
+        DataSource rafDataSource = new FileChannelDataSource(raf.getChannel());
 
         ByteArrayDataSink dataSink = new ByteArrayDataSink();
 
diff --git a/src/test/java/com/android/apksig/util/DataSourceFromRAFChunkTest.java b/src/test/java/com/android/apksig/util/DataSourceFromRAFChunkTest.java
index 8116012..e83c906 100644
--- a/src/test/java/com/android/apksig/util/DataSourceFromRAFChunkTest.java
+++ b/src/test/java/com/android/apksig/util/DataSourceFromRAFChunkTest.java
@@ -24,15 +24,23 @@
 import java.nio.file.Files;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.junit.runners.JUnit4;
+import org.junit.runners.Parameterized;
 
 /**
  * Tests for the {@link DataSource} returned by
  * {@link DataSources#asDataSource(RandomAccessFile, long, long)}.
  */
-@RunWith(JUnit4.class)
+@RunWith(Parameterized.class)
 public class DataSourceFromRAFChunkTest extends DataSourceTestBase {
 
+    @Parameterized.Parameters(name = "{0}")
+    public static DataSourceFromRAFFactory[] data() {
+        return DataSourceFromRAFFactory.values();
+    }
+
+    @Parameterized.Parameter
+    public DataSourceFromRAFFactory factory;
+
     @Test
     public void testFileSizeChangesNotVisible() throws Exception {
         try (CloseableWithDataSource c = createDataSource("abcdefg")) {
@@ -82,7 +90,7 @@
         }
 
         return CloseableWithDataSource.of(
-                DataSources.asDataSource(f, 2, contents.length),
+                factory.create(f, 2, contents.length),
                 new DataSourceFromRAFTest.TmpFileCloseable(tmp, f));
     }
 }
diff --git a/src/test/java/com/android/apksig/util/DataSourceFromRAFFactory.java b/src/test/java/com/android/apksig/util/DataSourceFromRAFFactory.java
new file mode 100644
index 0000000..eec0c11
--- /dev/null
+++ b/src/test/java/com/android/apksig/util/DataSourceFromRAFFactory.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.apksig.util;
+
+import java.io.RandomAccessFile;
+
+enum DataSourceFromRAFFactory {
+    RANDOM_ACCESS_FILE {
+        @Override DataSource create(RandomAccessFile file) {
+            return DataSources.asDataSource(file);
+        }
+
+        @Override DataSource create(RandomAccessFile file, long offset, long size) {
+            return DataSources.asDataSource(file, offset, size);
+        }
+    },
+    FILE_CHANNEL {
+        @Override DataSource create(RandomAccessFile file) {
+            return DataSources.asDataSource(file.getChannel());
+        }
+
+        @Override DataSource create(RandomAccessFile file, long offset, long size) {
+            return DataSources.asDataSource(file.getChannel(), offset, size);
+        }
+    };
+
+    abstract DataSource create(RandomAccessFile file);
+    abstract DataSource create(RandomAccessFile file, long offset, long size);
+}
diff --git a/src/test/java/com/android/apksig/util/DataSourceFromRAFTest.java b/src/test/java/com/android/apksig/util/DataSourceFromRAFTest.java
index 36ef760..50de579 100644
--- a/src/test/java/com/android/apksig/util/DataSourceFromRAFTest.java
+++ b/src/test/java/com/android/apksig/util/DataSourceFromRAFTest.java
@@ -26,15 +26,23 @@
 import java.nio.file.Files;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.junit.runners.JUnit4;
+import org.junit.runners.Parameterized;
 
 /**
  * Tests for the {@link DataSource} returned by
  * {@link DataSources#asDataSource(java.io.RandomAccessFile)}.
  */
-@RunWith(JUnit4.class)
+@RunWith(Parameterized.class)
 public class DataSourceFromRAFTest extends DataSourceTestBase {
 
+    @Parameterized.Parameters(name = "{0}")
+    public static DataSourceFromRAFFactory[] data() {
+        return DataSourceFromRAFFactory.values();
+    }
+
+    @Parameterized.Parameter
+    public DataSourceFromRAFFactory factory;
+
     @Test
     public void testFileSizeChangesVisible() throws Exception {
         try (CloseableWithDataSource c = createDataSource("abcdefg")) {
@@ -78,7 +86,7 @@
         }
 
         return CloseableWithDataSource.of(
-                DataSources.asDataSource(f),
+                factory.create(f),
                 new TmpFileCloseable(tmp, f));
     }