Snap for 6381861 from 59c7e6cecbb701eac113f105af1dc5ee621a0605 to rvc-preview4-release

Change-Id: I1e5d91bc72d827a59ae7b5561d37bc8492ae1a96
diff --git a/luni/src/test/java/libcore/java/util/ArraysTest.java b/luni/src/test/java/libcore/java/util/ArraysTest.java
index a0122b2..5a2bfe2 100644
--- a/luni/src/test/java/libcore/java/util/ArraysTest.java
+++ b/luni/src/test/java/libcore/java/util/ArraysTest.java
@@ -19,6 +19,7 @@
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -595,6 +596,7 @@
                 .boxed()
                 .collect(Collectors.toList());
 
+            assertEquals(size, destList.size());
             for (int i = 0; i < size; i++) {
                 assertEquals((int) destList.get(i), i);
             }
@@ -681,6 +683,7 @@
                 .boxed()
                 .collect(Collectors.toList());
 
+            assertEquals(size, destList.size());
             for (int i = 0; i < size; i++) {
                 assertEquals((long) destList.get(i), i);
             }
@@ -767,6 +770,7 @@
                 .boxed()
                 .collect(Collectors.toList());
 
+            assertEquals(size, destList.size());
             for (int i = 0; i < size; i++) {
                 assertEquals(destList.get(i), i, 0.001);
             }
@@ -831,6 +835,91 @@
         }
     }
 
+    @Test
+    public void streamObject() {
+      for (int size : TEST_ARRAY_SIZES) {
+        String[] sourceArray = stringTestArray(size);
+
+        // Stream, map, accumulate
+        int sum = Arrays.stream(sourceArray)
+            .mapToInt(i -> Integer.parseInt(i) * 2)
+            .sum();
+        assertEquals(size * (size - 1), sum);
+
+        // Stream, collect as array again
+        String[] destArray = Arrays.stream(sourceArray)
+            .toArray(String[]::new);
+        assertArrayEquals(sourceArray, destArray);
+        assertNotSame(sourceArray, destArray);
+
+        // Stream, collect as list
+        List<String> destList = Arrays.stream(sourceArray)
+            .collect(Collectors.toList());
+
+        assertEquals(size, destList.size());
+        for (int i = 0; i < size; i++) {
+          assertSame(destList.get(i), sourceArray[i]);
+        }
+      }
+    }
+
+    @Test
+    public void streamObjectStartEnd() {
+        final int size = 10;
+        String[] sourceArray = stringTestArray(size);
+        for (int start = 0; start < size - 1; start++) {
+            for (int end = start; end < size; end++) {
+                String[] destArray = Arrays.stream(sourceArray, start, end)
+                    .toArray(String[]::new);
+                int len = end - start;
+                assertEquals(len, destArray.length);
+                if (len > 0) {
+                    assertSame(sourceArray[start], destArray[0]);
+                    assertSame(sourceArray[end - 1], destArray[len - 1]);
+                }
+            }
+        }
+    }
+
+    @Test
+    public void streamObjectStartEnd_Exceptions() {
+        String[] sourceArray = stringTestArray(10);
+        try {
+            long unused = Arrays.stream(sourceArray, -1, 9)
+                .count();
+            fail();
+        } catch (ArrayIndexOutOfBoundsException e) {
+            // Expected
+        }
+        try {
+            long unused = Arrays.stream(sourceArray, 0, 11)
+                .count();
+            fail();
+        } catch (ArrayIndexOutOfBoundsException e) {
+            // Expected
+        }
+        try {
+            long unused = Arrays.stream(sourceArray, 11, 11)
+                .count();
+            fail();
+        } catch (ArrayIndexOutOfBoundsException e) {
+            // Expected
+        }
+        try {
+            long unused = Arrays.stream(sourceArray, 0, -1)
+                .count();
+            fail();
+        } catch (ArrayIndexOutOfBoundsException e) {
+            // Expected
+        }
+        try {
+            long unused = Arrays.stream(sourceArray, 4, 3)
+                .count();
+            fail();
+        } catch (ArrayIndexOutOfBoundsException e) {
+            // Expected
+        }
+    }
 
     private int[] intTestArray(int size) {
         int[] array = new int[size];
@@ -855,4 +944,12 @@
         }
         return array;
     }
+
+    private String[] stringTestArray(int size) {
+        String[] array = new String[size];
+        for (int i = 0; i < size; i++) {
+            array[i] = String.valueOf(i);
+        }
+        return array;
+    }
 }
diff --git a/luni/src/test/java/libcore/java/util/stream/StreamTest.java b/luni/src/test/java/libcore/java/util/stream/StreamTest.java
new file mode 100644
index 0000000..5f8f254
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/stream/StreamTest.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2020 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 libcore.java.util.stream;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertSame;
+
+import java.util.stream.Stream;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/**
+ * Streams tests required for Mainline coverage.
+ *
+ * TODO(b/153297830): Use existing CtsLibcoreOjTestCases for coverage instead.
+ */
+
+@RunWith(JUnit4.class)
+public class StreamTest {
+  private static final int[] TEST_ARRAY_SIZES = { 0, 1, 2, 10, 100, 1000 };
+
+  /**
+   * Stream<T>.of() has two overloads, Stream.of(T t) and Stream.of(T... values)
+   *
+   * The first builds a Stream<T> whose functionality is tested in CtsLibcoreOjTestCases, so we
+   * just check the contents are as expected
+   *
+   * The second is a thin wrapper around Arrays.Stream(), which is tested in ArraysTest,
+   * so again we just check the contents are as expected.
+   *
+   */
+  @Test
+  public void streamOfSingleObject() {
+    String object = "string";
+    String[] array = Stream.of(object).toArray(String[]::new);
+    assertEquals(1, array.length);
+    assertSame(object, array[0]);
+  }
+
+  @Test
+  public void streamOfObjects() {
+    for (int size : TEST_ARRAY_SIZES) {
+      String[] sourceArray = stringTestArray(size);
+
+      // Stream.of(T[] t) is equivalent to Stream.of(T... t)
+      String[] destArray = Stream.of(sourceArray)
+          .toArray(String[]::new);
+      assertNotSame(sourceArray, destArray);
+      assertArrayEquals(sourceArray, destArray);
+    }
+  }
+
+  private String[] stringTestArray(int size) {
+    String[] array = new String[size];
+    for (int i = 0; i < size; i++) {
+      array[i] = String.valueOf(i);
+    }
+    return array;
+  }
+}
diff --git a/luni/src/test/java/libcore/java/util/zip/ZipInputStreamTest.java b/luni/src/test/java/libcore/java/util/zip/ZipInputStreamTest.java
index 8485874..8a33f6f 100644
--- a/luni/src/test/java/libcore/java/util/zip/ZipInputStreamTest.java
+++ b/luni/src/test/java/libcore/java/util/zip/ZipInputStreamTest.java
@@ -143,6 +143,19 @@
         zis.close();
     }
 
+    private static final byte[] ZIP_WITH_DATA_DESCRIPTOR = new byte[] {
+(byte) 80, 75, 3, 4, 10, 0, 8, 0, 0, 0, -51, 90, -121, 80, -20, 62, -84, -103, 2, 0, 0, 0, 2, 0, 0, 0, 8, 0, 28, 0, 116, 101, 115, 116, 46, 116, 120, 116, 85, 84, 9, 0, 3, 97, 84, -116, 94, 102, 84, -116, 94, 117, 120, 11, 0, 1, 4, -119, 66, 0, 0, 4, 83, 95, 1, 0, 72, 10, 80, 75, 7, 8, -20, 62, -84, -103, 2, 0, 0, 0, 2, 0, 0, 0, 80, 75, 1, 2, 30, 3, 10, 0, 0, 0, 0, 0, -51, 90, -121, 80, -20, 62, -84, -103, 2, 0, 0, 0, 2, 0, 0, 0, 8, 0, 24, 0, 0, 0, 0, 0, 1, 0, 0, 0, -92, -127, 0, 0, 0, 0, 116, 101, 115, 116, 46, 116, 120, 116, 85, 84, 5, 0, 3, 97, 84, -116, 94, 117, 120, 11, 0, 1, 4, -119, 66, 0, 0, 4, 83, 95, 1, 0, 80, 75, 5, 6, 0, 0, 0, 0, 1, 0, 1, 0, 78, 0, 0, 0, 84, 0, 0, 0, 0, 0 };
+
+    public void testDataDescriptorOnStoredEntry() throws Exception {
+        ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(
+                ZIP_WITH_DATA_DESCRIPTOR));
+
+        ZipEntry entry = zis.getNextEntry();
+        assertEquals("test.txt", entry.getName());
+
+        zis.close();
+    }
+
     private static byte[] zip(String[] names, byte[] bytes) throws IOException {
         ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
         ZipOutputStream zippedOut = new ZipOutputStream(bytesOut);
diff --git a/ojluni/src/main/java/java/util/zip/ZipInputStream.java b/ojluni/src/main/java/java/util/zip/ZipInputStream.java
index 0413f47..aaebb49 100644
--- a/ojluni/src/main/java/java/util/zip/ZipInputStream.java
+++ b/ojluni/src/main/java/java/util/zip/ZipInputStream.java
@@ -323,11 +323,16 @@
         e.method = get16(tmpbuf, LOCHOW);
         e.xdostime = get32(tmpbuf, LOCTIM);
         if ((flag & 8) == 8) {
-            /* "Data Descriptor" present */
-            if (e.method != DEFLATED) {
-                throw new ZipException(
-                        "only DEFLATED entries can have EXT descriptor");
-            }
+            // Android-Changed: Remove the requirement that only DEFLATED entries
+            // can have data descriptors. This is not required by the ZIP spec and
+            // is inconsistent with the behaviour of ZipFile and versions of Android
+            // prior to Android N.
+            //
+            // /* "Data Descriptor" present */
+            // if (e.method != DEFLATED) {
+            //     throw new ZipException(
+            //             "only DEFLATED entries can have EXT descriptor");
+            // }
         } else {
             e.crc = get32(tmpbuf, LOCCRC);
             e.csize = get32(tmpbuf, LOCSIZ);