< prev index next >
src/java.base/share/classes/java/nio/file/Files.java
Print this page
@@ -74,11 +74,12 @@
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.BiPredicate;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
-
+import jdk.internal.misc.JavaLangAccess;
+import jdk.internal.misc.SharedSecrets;
import sun.nio.fs.AbstractFileSystemProvider;
/**
* This class consists exclusively of static methods that operate on files,
* directories, or other types of files.
@@ -88,10 +89,23 @@
*
* @since 1.7
*/
public final class Files {
+ /**
+ * The maximum size of array to allocate.
+ * Some VMs reserve some header words in an array.
+ * Attempts to allocate larger arrays may result in
+ * OutOfMemoryError: Requested array size exceeds VM limit
+ */
+ private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
+
+ // buffer size used for reading and writing
+ private static final int BUFFER_SIZE = 8192;
+
+ private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
+
private Files() { }
/**
* Returns the {@code FileSystemProvider} to delegate to.
*/
@@ -2793,13 +2807,10 @@
}
// -- Utility methods for simple usages --
- // buffer size used for reading and writing
- private static final int BUFFER_SIZE = 8192;
-
/**
* Opens a file for reading, returning a {@code BufferedReader} that may be
* used to read text from the file in an efficient manner. Bytes from the
* file are decoded into characters using the specified charset. Reading
* commences at the beginning of the file.
@@ -3112,21 +3123,10 @@
return in.transferTo(out);
}
}
/**
- * The maximum size of array to allocate.
- * Some VMs reserve some header words in an array.
- * Attempts to allocate larger arrays may result in
- * OutOfMemoryError: Requested array size exceeds VM limit
- */
- private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
-
- private static final jdk.internal.misc.JavaLangAccess JLA =
- jdk.internal.misc.SharedSecrets.getJavaLangAccess();
-
- /**
* Reads all the bytes from an input stream. Uses {@code initialSize} as a hint
* about how many bytes the stream will have.
*
* @param source
* the input stream to read from
@@ -3271,15 +3271,11 @@
public static String readString(Path path, Charset cs) throws IOException {
Objects.requireNonNull(path);
Objects.requireNonNull(cs);
byte[] ba = readAllBytes(path);
- try {
return JLA.newStringNoRepl(ba, cs);
- } catch (IllegalArgumentException e) {
- throw new IOException(e);
- }
}
/**
* Read all lines from a file. This method ensures that the file is
* closed when all bytes have been read or an I/O error, or other runtime
@@ -3618,24 +3614,21 @@
* invoked to check delete access if the file is opened with the
* {@code DELETE_ON_CLOSE} option.
*
* @since 11
*/
- public static Path writeString(Path path, CharSequence csq, Charset cs, OpenOption... options)
+ public static Path writeString(Path path, CharSequence csq, Charset cs,
+ OpenOption... options)
throws IOException
{
// ensure the text is not null before opening file
Objects.requireNonNull(path);
Objects.requireNonNull(csq);
Objects.requireNonNull(cs);
- try {
byte[] bytes = JLA.getBytesNoRepl(String.valueOf(csq), cs);
write(path, bytes, options);
- } catch (IllegalArgumentException e) {
- throw new IOException(e);
- }
return path;
}
// -- Stream APIs --
< prev index next >