--- old/src/share/classes/java/nio/file/Files.java 2013-07-21 01:54:40.253764845 +0400 +++ new/src/share/classes/java/nio/file/Files.java 2013-07-21 01:54:39.601773042 +0400 @@ -25,10 +25,10 @@ package java.nio.file; -import java.nio.ByteBuffer; import java.nio.file.attribute.*; import java.nio.file.spi.FileSystemProvider; import java.nio.file.spi.FileTypeDetector; +import java.nio.channels.Channels; import java.nio.channels.FileChannel; import java.nio.channels.SeekableByteChannel; import java.io.Closeable; @@ -38,6 +38,7 @@ import java.io.Writer; import java.io.BufferedReader; import java.io.BufferedWriter; +import java.io.ByteArrayOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.IOException; @@ -2994,17 +2995,16 @@ if (size > (long)Integer.MAX_VALUE) throw new OutOfMemoryError("Required array size too large"); - byte[] arr = new byte[(int)size]; - ByteBuffer bb = ByteBuffer.wrap(arr); - while (bb.hasRemaining()) { - if (fc.read(bb) < 0) { - // truncated - break; - } + try (InputStream fis = Channels.newInputStream(fc); + ByteArrayOutputStream bos = new ByteArrayOutputStream((int)size) { + @Override + public byte[] toByteArray() { + return (buf.length == count) ? buf : Arrays.copyOf(buf, count); + } + }) { + copy(fis, bos); + return bos.toByteArray(); } - - int nread = bb.position(); - return (nread == size) ? arr : Arrays.copyOf(arr, nread); } } --- /dev/null 2013-07-12 15:40:14.284240297 +0400 +++ new/test/java/nio/file/Files/ReadAllBytesProc.java 2013-07-21 01:54:41.421750160 +0400 @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2013, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* @test + * @bug 8020669 + * @summary j.n.f.Files.readAllBytes() should read from special files (like + * /proc/self/stat under Linux), even though OS reports them to be zero sized + */ + +import java.io.IOException; +import java.nio.file.*; + +public class ReadAllBytesProc { + + private static final String specFile = "/proc/self/stat"; + + public static void main(String[] args) { + try { + Path path = Paths.get(specFile); + byte[] data = Files.readAllBytes(path); + if (data.length == 0) + throw new RuntimeException("Failed to read content of '" + specFile + "'"); + } catch (IOException ex) { + System.err.println("Cannot perform test on this platform"); + } + } +}