Desugared library: fix channel exceptions

Change-Id: I7b52c144b6c281eafaee008c4848fd0c66549bcc
diff --git a/src/library_desugar/java/java/nio/channels/DesugarChannels.java b/src/library_desugar/java/java/nio/channels/DesugarChannels.java
index 4ca851d..63697a3 100644
--- a/src/library_desugar/java/java/nio/channels/DesugarChannels.java
+++ b/src/library_desugar/java/java/nio/channels/DesugarChannels.java
@@ -9,6 +9,8 @@
 import java.io.RandomAccessFile;
 import java.nio.ByteBuffer;
 import java.nio.MappedByteBuffer;
+import java.nio.file.FileAlreadyExistsException;
+import java.nio.file.Files;
 import java.nio.file.NoSuchFileException;
 import java.nio.file.OpenOption;
 import java.nio.file.Path;
@@ -196,7 +198,10 @@
 
     RandomAccessFile randomAccessFile =
         new RandomAccessFile(path.toFile(), getFileAccessModeText(openOptions));
-    if (openOptions.contains(StandardOpenOption.TRUNCATE_EXISTING)) {
+    // TRUNCATE_EXISTING is ignored if the file is not writable.
+    // TRUNCATE_EXISTING is not compatible with APPEND, so we just need to check for WRITE.
+    if (openOptions.contains(StandardOpenOption.TRUNCATE_EXISTING)
+        && openOptions.contains(StandardOpenOption.WRITE)) {
       randomAccessFile.setLength(0);
     }
 
@@ -208,11 +213,22 @@
     // TODO(b/259056135): Consider subclassing UnsupportedOperationException for desugared library.
     // RandomAccessFile does not support APPEND.
     // We could hack a wrapper to support APPEND in simple cases such as Files.write().
-    throw new UnsupportedOperationException();
+    throw new UnsupportedOperationException("APPEND not supported below api 26.");
   }
 
   private static void validateOpenOptions(Path path, Set<? extends OpenOption> openOptions)
-      throws NoSuchFileException {
+      throws IOException {
+    if (Files.exists(path)) {
+      if (openOptions.contains(StandardOpenOption.CREATE_NEW)
+          && openOptions.contains(StandardOpenOption.WRITE)) {
+        throw new FileAlreadyExistsException(path.toString());
+      }
+    } else {
+      if (!(openOptions.contains(StandardOpenOption.CREATE)
+          || openOptions.contains(StandardOpenOption.CREATE_NEW))) {
+        throw new NoSuchFileException(path.toString());
+      }
+    }
     // Validations that resemble sun.nio.fs.UnixChannelFactory#newFileChannel.
     if (openOptions.contains(StandardOpenOption.READ)
         && openOptions.contains(StandardOpenOption.APPEND)) {
@@ -222,13 +238,11 @@
         && openOptions.contains(StandardOpenOption.TRUNCATE_EXISTING)) {
       throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");
     }
-    if (openOptions.contains(StandardOpenOption.APPEND) && !path.toFile().exists()) {
-      throw new NoSuchFileException(path.toString());
-    }
   }
 
   private static String getFileAccessModeText(Set<? extends OpenOption> options) {
-    if (!options.contains(StandardOpenOption.WRITE)) {
+    if (!options.contains(StandardOpenOption.WRITE)
+        && !options.contains(StandardOpenOption.APPEND)) {
       return "r";
     }
     if (options.contains(StandardOpenOption.SYNC)) {
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesAttributesTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesAttributesTest.java
index 7d9506c..2ccaa08 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesAttributesTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesAttributesTest.java
@@ -173,6 +173,7 @@
       attributeAccess(path);
       System.out.println("ABSENT FILE");
       Path notExisting = Paths.get("notExisting.txt");
+      Files.deleteIfExists(notExisting);
       attributeViewAccess(notExisting);
       attributeAccess(notExisting);
     }
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesInOutTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesInOutTest.java
new file mode 100644
index 0000000..f813ac3
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesInOutTest.java
@@ -0,0 +1,302 @@
+// Copyright (c) 2022, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package com.android.tools.r8.desugar.desugaredlibrary.jdk11;
+
+import static com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification.DEFAULT_SPECIFICATIONS;
+import static com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification.JDK11_PATH;
+
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestRuntime.CfVm;
+import com.android.tools.r8.ToolHelper;
+import com.android.tools.r8.ToolHelper.DexVm.Version;
+import com.android.tools.r8.desugar.desugaredlibrary.DesugaredLibraryTestBase;
+import com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification;
+import com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification;
+import com.android.tools.r8.utils.StringUtils;
+import com.google.common.collect.ImmutableList;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.channels.SeekableByteChannel;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
+import java.util.List;
+import java.util.function.Supplier;
+import org.junit.Assume;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class FilesInOutTest extends DesugaredLibraryTestBase {
+
+  private static final String EXPECTED_RESULT =
+      StringUtils.lines(
+          "PRESENT FILE",
+          "buffRead:cHello!",
+          "inStream[READ]:cHello",
+          "outStream[READ]:class java.lang.IllegalArgumentException :: READ not allowed",
+          "buffWrite[READ]:class java.lang.IllegalArgumentException :: READ not allowed",
+          "newByte[READ]:c6",
+          "inStream[WRITE]:class java.lang.UnsupportedOperationException :: 'WRITE' not allowed",
+          "outStream[WRITE]:cwGame over!",
+          "buffWrite[WRITE]:cwHello!",
+          "newByte[WRITE]:c6",
+          "inStream[APPEND]:class java.lang.UnsupportedOperationException :: 'APPEND' not allowed",
+          "outStream[APPEND]:%s",
+          "buffWrite[APPEND]:%s",
+          "newByte[APPEND]:%s",
+          "inStream[TRUNCATE_EXISTING]:cHello",
+          "outStream[TRUNCATE_EXISTING]:cwGame over!",
+          "buffWrite[TRUNCATE_EXISTING]:cwclass java.lang.IndexOutOfBoundsException :: index 0,"
+              + " size 0",
+          "newByte[TRUNCATE_EXISTING]:c6",
+          "inStream[CREATE]:cHello",
+          "outStream[CREATE]:cwGame over!",
+          "buffWrite[CREATE]:cwHello!",
+          "newByte[CREATE]:c6",
+          "inStream[CREATE_NEW]:cHello",
+          "outStream[CREATE_NEW]:class java.nio.file.FileAlreadyExistsException :: example",
+          "buffWrite[CREATE_NEW]:class java.nio.file.FileAlreadyExistsException :: example",
+          "newByte[CREATE_NEW]:c6",
+          "inStream[DELETE_ON_CLOSE]:cHello",
+          "outStream[DELETE_ON_CLOSE]:%s",
+          "buffWrite[DELETE_ON_CLOSE]:%s",
+          "newByte[DELETE_ON_CLOSE]:c6",
+          "inStream[SPARSE]:cHello",
+          "outStream[SPARSE]:cwGame over!",
+          "buffWrite[SPARSE]:cwHello!",
+          "newByte[SPARSE]:c6",
+          "inStream[SYNC]:cHello",
+          "outStream[SYNC]:cwGame over!",
+          "buffWrite[SYNC]:cwHello!",
+          "newByte[SYNC]:c6",
+          "inStream[DSYNC]:cHello",
+          "outStream[DSYNC]:cwGame over!",
+          "buffWrite[DSYNC]:cwHello!",
+          "newByte[DSYNC]:c6",
+          "ABSENT FILE",
+          "buffRead:class java.nio.file.NoSuchFileException :: notExisting",
+          "inStream[READ]:class java.nio.file.NoSuchFileException :: notExisting",
+          "outStream[READ]:class java.lang.IllegalArgumentException :: READ not allowed",
+          "buffWrite[READ]:class java.lang.IllegalArgumentException :: READ not allowed",
+          "newByte[READ]:class java.nio.file.NoSuchFileException :: notExisting",
+          "inStream[WRITE]:class java.lang.UnsupportedOperationException :: 'WRITE' not allowed",
+          "outStream[WRITE]:class java.nio.file.NoSuchFileException :: notExisting",
+          "buffWrite[WRITE]:class java.nio.file.NoSuchFileException :: notExisting",
+          "newByte[WRITE]:class java.nio.file.NoSuchFileException :: notExisting",
+          "inStream[APPEND]:class java.lang.UnsupportedOperationException :: 'APPEND' not allowed",
+          "outStream[APPEND]:class java.nio.file.NoSuchFileException :: notExisting",
+          "buffWrite[APPEND]:class java.nio.file.NoSuchFileException :: notExisting",
+          "newByte[APPEND]:class java.nio.file.NoSuchFileException :: notExisting",
+          "inStream[TRUNCATE_EXISTING]:class java.nio.file.NoSuchFileException :: notExisting",
+          "outStream[TRUNCATE_EXISTING]:class java.nio.file.NoSuchFileException :: notExisting",
+          "buffWrite[TRUNCATE_EXISTING]:class java.nio.file.NoSuchFileException :: notExisting",
+          "newByte[TRUNCATE_EXISTING]:class java.nio.file.NoSuchFileException :: notExisting",
+          "inStream[CREATE]:%s",
+          "outStream[CREATE]:cwGame over!",
+          "buffWrite[CREATE]:cwclass java.lang.IndexOutOfBoundsException :: index 0, size 0",
+          "newByte[CREATE]:%s",
+          "inStream[CREATE_NEW]:%s",
+          "outStream[CREATE_NEW]:cwGame over!",
+          "buffWrite[CREATE_NEW]:cwclass java.lang.IndexOutOfBoundsException :: index 0, size 0",
+          "newByte[CREATE_NEW]:%s",
+          "inStream[DELETE_ON_CLOSE]:class java.nio.file.NoSuchFileException :: notExisting",
+          "outStream[DELETE_ON_CLOSE]:class java.nio.file.NoSuchFileException :: notExisting",
+          "buffWrite[DELETE_ON_CLOSE]:class java.nio.file.NoSuchFileException :: notExisting",
+          "newByte[DELETE_ON_CLOSE]:class java.nio.file.NoSuchFileException :: notExisting",
+          "inStream[SPARSE]:class java.nio.file.NoSuchFileException :: notExisting",
+          "outStream[SPARSE]:class java.nio.file.NoSuchFileException :: notExisting",
+          "buffWrite[SPARSE]:class java.nio.file.NoSuchFileException :: notExisting",
+          "newByte[SPARSE]:class java.nio.file.NoSuchFileException :: notExisting",
+          "inStream[SYNC]:class java.nio.file.NoSuchFileException :: notExisting",
+          "outStream[SYNC]:class java.nio.file.NoSuchFileException :: notExisting",
+          "buffWrite[SYNC]:class java.nio.file.NoSuchFileException :: notExisting",
+          "newByte[SYNC]:class java.nio.file.NoSuchFileException :: notExisting",
+          "inStream[DSYNC]:class java.nio.file.NoSuchFileException :: notExisting",
+          "outStream[DSYNC]:class java.nio.file.NoSuchFileException :: notExisting",
+          "buffWrite[DSYNC]:class java.nio.file.NoSuchFileException :: notExisting",
+          "newByte[DSYNC]:class java.nio.file.NoSuchFileException :: notExisting");
+  private static final String[] EXPECTED_RESULT_NO_DESUGARING =
+      new String[] {
+        "cwHello!Game over!",
+        "cwHello!",
+        "c6",
+        "cwclass java.nio.file.NoSuchFileException :: example",
+        "cwclass java.nio.file.NoSuchFileException :: example",
+        "class java.nio.file.NoSuchFileException :: notExisting",
+        "class java.nio.file.NoSuchFileException :: notExisting",
+        "class java.nio.file.NoSuchFileException :: notExisting",
+        "class java.nio.file.NoSuchFileException :: notExisting"
+      };
+
+  private static final String[] EXPECTED_RESULT_DESUGARING =
+      new String[] {
+        // APPEND not supported.
+        "class java.lang.UnsupportedOperationException :: APPEND not supported below api 26.",
+        "class java.lang.UnsupportedOperationException :: APPEND not supported below api 26.",
+        "class java.lang.UnsupportedOperationException :: APPEND not supported below api 26.",
+        // DELETE_ON_CLOSE not supported.
+        "cwGame over!",
+        "cwHello!",
+        // In some cases the desugaring version raises FileNotFoundException instead of
+        // NoSuchFileException on the same file.
+        "class java.io.FileNotFoundException :: notExisting",
+        "class java.io.FileNotFoundException :: notExisting",
+        "class java.io.FileNotFoundException :: notExisting",
+        "class java.io.FileNotFoundException :: notExisting"
+      };
+
+  private final TestParameters parameters;
+  private final LibraryDesugaringSpecification libraryDesugaringSpecification;
+  private final CompilationSpecification compilationSpecification;
+
+  @Parameters(name = "{0}, spec: {1}, {2}")
+  public static List<Object[]> data() {
+    return buildParameters(
+        // Skip Android 4.4.4 due to missing libjavacrypto.
+        getTestParameters()
+            .withCfRuntime(CfVm.JDK11)
+            .withDexRuntime(Version.V4_0_4)
+            .withDexRuntimesStartingFromIncluding(Version.V5_1_1)
+            .withAllApiLevels()
+            .build(),
+        ImmutableList.of(JDK11_PATH),
+        DEFAULT_SPECIFICATIONS);
+  }
+
+  public FilesInOutTest(
+      TestParameters parameters,
+      LibraryDesugaringSpecification libraryDesugaringSpecification,
+      CompilationSpecification compilationSpecification) {
+    this.parameters = parameters;
+    this.libraryDesugaringSpecification = libraryDesugaringSpecification;
+    this.compilationSpecification = compilationSpecification;
+  }
+
+  private String getExpectedResult() {
+    if (parameters.isCfRuntime()
+        || libraryDesugaringSpecification.usesPlatformFileSystem(parameters)) {
+      return String.format(EXPECTED_RESULT, EXPECTED_RESULT_NO_DESUGARING);
+    }
+    return String.format(EXPECTED_RESULT, EXPECTED_RESULT_DESUGARING);
+  }
+
+  @Test
+  public void test() throws Throwable {
+    if (parameters.isCfRuntime()) {
+      // Reference runtime, we use Jdk 11 since this is Jdk 11 desugared library, not that Jdk 8
+      // behaves differently on this test.
+      Assume.assumeTrue(parameters.isCfRuntime(CfVm.JDK11) && !ToolHelper.isWindows());
+      testForJvm()
+          .addInnerClasses(getClass())
+          .run(parameters.getRuntime(), TestClass.class)
+          .assertSuccessWithOutput(getExpectedResult());
+      return;
+    }
+    testForDesugaredLibrary(parameters, libraryDesugaringSpecification, compilationSpecification)
+        .addInnerClasses(getClass())
+        .addKeepMainRule(TestClass.class)
+        .compile()
+        .withArt6Plus64BitsLib()
+        .run(parameters.getRuntime(), TestClass.class)
+        .assertSuccessWithOutput(getExpectedResult());
+  }
+
+  public static class TestClass {
+
+    public static void main(String[] args) throws Throwable {
+      System.out.println("PRESENT FILE");
+      Supplier<Path> presentFileSupplier =
+          () -> {
+            try {
+              Path path = Files.createTempFile("example_", ".txt");
+              Files.write(path, "Hello!".getBytes(StandardCharsets.UTF_8));
+              return path;
+            } catch (IOException t) {
+              // For the test it should never happen so this is enough.
+              throw new RuntimeException(t);
+            }
+          };
+      testProperties(presentFileSupplier);
+      System.out.println("ABSENT FILE");
+      testProperties(() -> Paths.get("notExisting_XXX.txt"));
+    }
+
+    private static void printError(Throwable t) {
+      String[] split =
+          t.getMessage() == null ? new String[] {"no-message_XXX"} : t.getMessage().split("/");
+      split = split[split.length - 1].split("_");
+      if (t instanceof IndexOutOfBoundsException) {
+        // IndexOutOfBoundsException are printed slightly differently across platform and it's not
+        // really relevant for the test.
+        split = new String[] {"index 0, size 0"};
+      }
+      System.out.println(t.getClass() + " :: " + split[0]);
+    }
+
+    private static void testProperties(Supplier<Path> pathSupplier) throws IOException {
+      Path path = pathSupplier.get();
+      System.out.print("buffRead:");
+      try (BufferedReader bufferedReader = Files.newBufferedReader(path)) {
+        System.out.print("c");
+        System.out.println(bufferedReader.readLine());
+      } catch (Throwable t) {
+        printError(t);
+      }
+      Files.deleteIfExists(path);
+      for (StandardOpenOption value : StandardOpenOption.values()) {
+        path = pathSupplier.get();
+        System.out.print("inStream[" + value + "]:");
+        try (InputStream inputStream = Files.newInputStream(path, value)) {
+          System.out.print("c");
+          byte[] read = new byte[5];
+          inputStream.read(read);
+          System.out.println(new String(read));
+        } catch (Throwable t) {
+          printError(t);
+        }
+        Files.deleteIfExists(path);
+        path = pathSupplier.get();
+        System.out.print("outStream[" + value + "]:");
+        try (OutputStream outputStream = Files.newOutputStream(path, value)) {
+          System.out.print("c");
+          outputStream.write("Game over!".getBytes(StandardCharsets.UTF_8));
+          System.out.print("w");
+          System.out.println(Files.readAllLines(path).get(0));
+        } catch (Throwable t) {
+          printError(t);
+        }
+        Files.deleteIfExists(path);
+        path = pathSupplier.get();
+        System.out.print("buffWrite[" + value + "]:");
+        try (BufferedWriter bufferedWriter = Files.newBufferedWriter(path, value)) {
+          System.out.print("c");
+          bufferedWriter.write("Game over!");
+          System.out.print("w");
+          System.out.println(Files.readAllLines(path).get(0));
+        } catch (Throwable t) {
+          printError(t);
+        }
+        Files.deleteIfExists(path);
+        path = pathSupplier.get();
+        System.out.print("newByte[" + value + "]:");
+        try (SeekableByteChannel seekableByteChannel = Files.newByteChannel(path, value)) {
+          System.out.print("c");
+          System.out.println(seekableByteChannel.size());
+        } catch (Throwable t) {
+          printError(t);
+        }
+        Files.deleteIfExists(path);
+      }
+    }
+  }
+}