test/java/nio/file/Files/BytesAndLines.java

Print this page

        

@@ -20,11 +20,11 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 
 /* @test
- * @bug 7006126
+ * @bug 7006126 8020669
  * @summary Unit test for methods for Files readAllBytes, readAllLines and
  *     and write methods.
  */
 
 import java.nio.file.*;

@@ -40,10 +40,11 @@
 
     public static void main(String[] args) throws IOException {
         testReadAndWriteBytes();
         testReadLines();
         testWriteLines();
+        testReadProcFs();
     }
 
     /**
      * Test readAllBytes(Path) and write(Path, byte[], OpenOption...)
      */

@@ -240,11 +241,33 @@
             } catch (NullPointerException ignore) { }
 
         } finally {
             delete(tmpfile);
         }
+    }
 
+    /**
+     * Test how readAllBytes(Path) and readAllLines(Path,Charset) read from procfs
+     */
+    static void testReadProcFs() throws IOException {
+        if (System.getProperty("os.name").equals("Linux")) {
+
+            // Refer to the Linux proc(5) man page for details about /proc/self/stat and
+            // /proc/self/status files
+            //
+            // procfs reports these files to be zero sized, even though data can be read from them
+
+            String statFile = "/proc/self/stat";
+            Path pathStat = Paths.get(statFile);
+            byte[] data = Files.readAllBytes(pathStat);
+            assertTrue(data.length > 0, "Files.readAllBytes('" + statFile + "') failed to read");
+
+            String statusFile = "/proc/self/status";
+            Path pathStatus = Paths.get(statusFile);
+            List<String> lines = Files.readAllLines(pathStatus, US_ASCII);
+            assertTrue(lines.size() > 0, "Files.readAllLines('" + pathStatus + "') failed to read");
+        }
     }
 
     static void assertTrue(boolean expr, String errmsg) {
         if (!expr)
             throw new RuntimeException(errmsg);