FileChannel#open test
Bug:222647019
Change-Id: I3384e4d65b3e6931a717243b84c286efffb7795c
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesTest.java
index 7b78157..effd169 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FilesTest.java
@@ -16,15 +16,20 @@
import com.android.tools.r8.utils.StringUtils;
import java.io.IOException;
import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
+import java.nio.file.LinkOption;
+import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
import org.junit.Assume;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -40,6 +45,19 @@
"String written: Hello World",
"bytes read: 11",
"String read: Hello World",
+ "bytes read: 11",
+ "String read: Hello World",
+ "null",
+ "true",
+ "unsupported");
+ private static final String EXPECTED_RESULT_24_26 =
+ StringUtils.lines(
+ "bytes written: 11",
+ "String written: Hello World",
+ "bytes read: 11",
+ "String read: Hello World",
+ "unsupported",
+ "unsupported",
"null",
"true",
"unsupported");
@@ -49,6 +67,8 @@
"String written: Hello World",
"bytes read: 11",
"String read: Hello World",
+ "bytes read: 11",
+ "String read: Hello World",
"true",
"true",
"true");
@@ -69,8 +89,11 @@
}
private String getExpectedResult() {
- return parameters.getApiLevel().isGreaterThanOrEqualTo(AndroidApiLevel.O)
- ? EXPECTED_RESULT_26
+ if (parameters.getApiLevel().isGreaterThanOrEqualTo(AndroidApiLevel.O)) {
+ return EXPECTED_RESULT_26;
+ }
+ return parameters.getApiLevel().isGreaterThanOrEqualTo(AndroidApiLevel.N)
+ ? EXPECTED_RESULT_24_26
: EXPECTED_RESULT;
}
@@ -124,8 +147,12 @@
public static void main(String[] args) throws Throwable {
Path path = Files.createTempFile("example", ".txt");
- readWrite(path);
+ readWriteThroughFilesAPI(path);
+ readThroughFileChannelAPI(path);
+ attributeAccess(path);
+ }
+ private static void attributeAccess(Path path) throws IOException {
PosixFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class);
if (view != null) {
System.out.println(
@@ -154,7 +181,7 @@
}
}
- private static void readWrite(Path path) throws IOException {
+ private static void readWriteThroughFilesAPI(Path path) throws IOException {
try (SeekableByteChannel channel =
Files.newByteChannel(path, StandardOpenOption.READ, StandardOpenOption.WRITE)) {
String toWrite = "Hello World";
@@ -173,5 +200,26 @@
System.out.println("String read: " + new String(byteBuffer2.array()));
}
}
+
+ private static void readThroughFileChannelAPI(Path path) throws IOException {
+ try {
+ Set<OpenOption> openOptions = new HashSet<>();
+ openOptions.add(LinkOption.NOFOLLOW_LINKS);
+ try (FileChannel channel = FileChannel.open(path, openOptions)) {
+ String toWrite = "Hello World";
+
+ // Read the String toWrite from the channel.
+ channel.position(0);
+ ByteBuffer byteBuffer2 = ByteBuffer.allocate(toWrite.length());
+ int read = channel.read(byteBuffer2);
+ System.out.println("bytes read: " + read);
+ System.out.println("String read: " + new String(byteBuffer2.array()));
+ }
+ } catch (NoClassDefFoundError err) {
+ // TODO(b/222647019): FileChannel#open is not supported in between 24 and 26.
+ System.out.println("unsupported");
+ System.out.println("unsupported");
+ }
+ }
}
}