8132539: (fs) Files.lines(path).collect() returns wrong value in JDK 9 with certain files
Summary: Do not use the Spliterator approach for lines() if the FileChannel size is thought to be zero.
Reviewed-by: chegar
--- a/jdk/src/java.base/share/classes/java/nio/file/Files.java Wed Aug 12 14:38:09 2015 +0100
+++ b/jdk/src/java.base/share/classes/java/nio/file/Files.java Wed Aug 12 07:55:24 2015 -0700
@@ -3835,7 +3835,9 @@
// Obtaining the size from the FileChannel is much faster
// than obtaining using path.toFile().length()
long length = fc.size();
- if (length <= Integer.MAX_VALUE) {
+ // FileChannel.size() may in certain circumstances return zero
+ // for a non-zero length file so disallow this case.
+ if (length > 0 && length <= Integer.MAX_VALUE) {
Spliterator<String> s = new FileChannelLinesSpliterator(fc, cs, 0, (int) length);
return StreamSupport.stream(s, false)
.onClose(Files.asUncheckedRunnable(fc));
--- a/jdk/test/java/nio/file/Files/StreamTest.java Wed Aug 12 14:38:09 2015 +0100
+++ b/jdk/test/java/nio/file/Files/StreamTest.java Wed Aug 12 07:55:24 2015 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -22,7 +22,7 @@
*/
/* @test
- * @bug 8006884 8019526
+ * @bug 8006884 8019526 8132539
* @library ..
* @build PassThroughFileSystem FaultyFileSystem
* @run testng StreamTest
@@ -685,4 +685,18 @@
// expected
}
}
+
+ public void testProcFile() throws IOException {
+ if (System.getProperty("os.name").equals("Linux")) {
+ Path path = Paths.get("/proc/cpuinfo");
+ if (Files.exists(path)) {
+ String NEW_LINE = System.getProperty("line.separator");
+ String s =
+ Files.lines(path).collect(Collectors.joining(NEW_LINE));
+ if (s.length() == 0) {
+ fail("Files.lines(\"" + path + "\") returns no data");
+ }
+ }
+ }
+ }
}