changeset 59721:1644cf7879a5

8183040: update jdk/test/lib/Platform.java to use NIO file Reviewed-by: amenkov, bpb
author iignatyev
date Wed, 10 Jun 2020 13:15:27 -0700
parents c2f8c8f68bbe
children d9daa4ce8017
files test/lib/jdk/test/lib/Platform.java test/lib/jdk/test/lib/SA/SATestUtils.java
diffstat 2 files changed, 28 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/test/lib/jdk/test/lib/Platform.java	Wed Jun 10 14:51:28 2020 -0400
+++ b/test/lib/jdk/test/lib/Platform.java	Wed Jun 10 13:15:27 2020 -0700
@@ -23,9 +23,9 @@
 
 package jdk.test.lib;
 
-import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.security.AccessController;
@@ -239,7 +239,7 @@
         String jdkPath = System.getProperty("java.home");
         Path javaPath = Paths.get(jdkPath + "/bin/java");
         String javaFileName = javaPath.toAbsolutePath().toString();
-        if (!javaPath.toFile().exists()) {
+        if (Files.notExists(javaPath)) {
             throw new FileNotFoundException("Could not find file " + javaFileName);
         }
 
--- a/test/lib/jdk/test/lib/SA/SATestUtils.java	Wed Jun 10 14:51:28 2020 -0400
+++ b/test/lib/jdk/test/lib/SA/SATestUtils.java	Wed Jun 10 13:15:27 2020 -0700
@@ -22,13 +22,15 @@
  */
 package jdk.test.lib.SA;
 
+import jdk.test.lib.JDKToolLauncher;
+import jdk.test.lib.Platform;
+import jtreg.SkippedException;
+
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.RandomAccessFile;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.security.AccessController;
-import java.security.PrivilegedAction;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
 import java.util.ArrayList;
@@ -37,13 +39,6 @@
 import java.util.concurrent.TimeUnit;
 import java.util.zip.GZIPInputStream;
 
-import jdk.test.lib.Asserts;
-import jdk.test.lib.JDKToolLauncher;
-import jdk.test.lib.Platform;
-import jdk.test.lib.process.OutputAnalyzer;
-import jdk.test.lib.process.ProcessTools;
-import jtreg.SkippedException;
-
 public class SATestUtils {
     /**
      * Creates a ProcessBuilder, adding privileges (sudo) if needed.
@@ -168,11 +163,15 @@
      */
     private static boolean canPtraceAttachLinux() throws IOException {
         // SELinux deny_ptrace:
-        File deny_ptrace = new File("/sys/fs/selinux/booleans/deny_ptrace");
-        if (deny_ptrace.exists()) {
-            try (RandomAccessFile file = AccessController.doPrivileged(
-                    (PrivilegedExceptionAction<RandomAccessFile>) () -> new RandomAccessFile(deny_ptrace, "r"))) {
-                if (file.readByte() != '0') {
+        var deny_ptrace = Paths.get("/sys/fs/selinux/booleans/deny_ptrace");
+        if (Files.exists(deny_ptrace)) {
+            try {
+                var bb = AccessController.doPrivileged(
+                    (PrivilegedExceptionAction<byte[]>) () -> Files.readAllBytes(deny_ptrace));
+                if (bb.length == 0) {
+                    throw new Error("deny_ptrace is empty");
+                }
+                if (bb[0] != '0') {
                     return false;
                 }
             } catch (PrivilegedActionException e) {
@@ -186,11 +185,15 @@
         // 1 - restricted ptrace: a process must be a children of the inferior or user is root
         // 2 - only processes with CAP_SYS_PTRACE may use ptrace or user is root
         // 3 - no attach: no processes may use ptrace with PTRACE_ATTACH
-        File ptrace_scope = new File("/proc/sys/kernel/yama/ptrace_scope");
-        if (ptrace_scope.exists()) {
-            try (RandomAccessFile file = AccessController.doPrivileged(
-                    (PrivilegedExceptionAction<RandomAccessFile>) () -> new RandomAccessFile(ptrace_scope, "r"))) {
-                byte yama_scope = file.readByte();
+        var ptrace_scope = Paths.get("/proc/sys/kernel/yama/ptrace_scope");
+        if (Files.exists(ptrace_scope)) {
+            try {
+                var bb = AccessController.doPrivileged(
+                    (PrivilegedExceptionAction<byte[]>) () -> Files.readAllBytes(ptrace_scope));
+                if (bb.length == 0) {
+                    throw new Error("ptrace_scope is empty");
+                }
+                byte yama_scope = bb[0];
                 if (yama_scope == '3') {
                     return false;
                 }
@@ -212,13 +215,8 @@
         for (File gzCore : gzCores) {
             String coreFileName = gzCore.getName().replace(".gz", "");
             System.out.println("Unzipping core into " + coreFileName);
-            try (GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(gzCore));
-                 FileOutputStream fos = new FileOutputStream(coreFileName)) {
-                byte[] buffer = new byte[1024];
-                int length;
-                while ((length = gzis.read(buffer)) > 0) {
-                    fos.write(buffer, 0, length);
-                }
+            try (GZIPInputStream gzis = new GZIPInputStream(Files.newInputStream(gzCore.toPath()))) {
+                Files.copy(gzis, Paths.get(coreFileName));
             } catch (IOException e) {
                 throw new SkippedException("Not able to unzip file: " + gzCore.getAbsolutePath(), e);
             }