8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.nio.file;
27
28 import java.nio.ByteBuffer;
29 import java.nio.file.attribute.*;
30 import java.nio.file.spi.FileSystemProvider;
31 import java.nio.file.spi.FileTypeDetector;
32 import java.nio.channels.FileChannel;
33 import java.nio.channels.SeekableByteChannel;
34 import java.io.Closeable;
35 import java.io.InputStream;
36 import java.io.OutputStream;
37 import java.io.Reader;
38 import java.io.Writer;
39 import java.io.BufferedReader;
40 import java.io.BufferedWriter;
41 import java.io.InputStreamReader;
42 import java.io.OutputStreamWriter;
43 import java.io.IOException;
44 import java.io.UncheckedIOException;
45 import java.util.*;
46 import java.util.function.BiPredicate;
47 import java.util.stream.CloseableStream;
48 import java.util.stream.DelegatingStream;
49 import java.util.stream.Stream;
50 import java.util.stream.StreamSupport;
51 import java.security.AccessController;
2948 *
2949 * @return the number of bytes read or written
2950 *
2951 * @throws IOException
2952 * if an I/O error occurs when reading or writing
2953 * @throws SecurityException
2954 * In the case of the default provider, and a security manager is
2955 * installed, the {@link SecurityManager#checkRead(String) checkRead}
2956 * method is invoked to check read access to the file.
2957 */
2958 public static long copy(Path source, OutputStream out) throws IOException {
2959 // ensure not null before opening file
2960 Objects.requireNonNull(out);
2961
2962 try (InputStream in = newInputStream(source)) {
2963 return copy(in, out);
2964 }
2965 }
2966
2967 /**
2968 * Read all the bytes from a file. The method ensures that the file is
2969 * closed when all bytes have been read or an I/O error, or other runtime
2970 * exception, is thrown.
2971 *
2972 * <p> Note that this method is intended for simple cases where it is
2973 * convenient to read all bytes into a byte array. It is not intended for
2974 * reading in large files.
2975 *
2976 * @param path
2977 * the path to the file
2978 *
2979 * @return a byte array containing the bytes read from the file
2980 *
2981 * @throws IOException
2982 * if an I/O error occurs reading from the stream
2983 * @throws OutOfMemoryError
2984 * if an array of the required size cannot be allocated, for
2985 * example the file is larger that {@code 2GB}
2986 * @throws SecurityException
2987 * In the case of the default provider, and a security manager is
2988 * installed, the {@link SecurityManager#checkRead(String) checkRead}
2989 * method is invoked to check read access to the file.
2990 */
2991 public static byte[] readAllBytes(Path path) throws IOException {
2992 try (FileChannel fc = FileChannel.open(path)) {
2993 long size = fc.size();
2994 if (size > (long)Integer.MAX_VALUE)
2995 throw new OutOfMemoryError("Required array size too large");
2996
2997 byte[] arr = new byte[(int)size];
2998 ByteBuffer bb = ByteBuffer.wrap(arr);
2999 while (bb.hasRemaining()) {
3000 if (fc.read(bb) < 0) {
3001 // truncated
3002 break;
3003 }
3004 }
3005
3006 int nread = bb.position();
3007 return (nread == size) ? arr : Arrays.copyOf(arr, nread);
3008 }
3009 }
3010
3011 /**
3012 * Read all lines from a file. This method ensures that the file is
3013 * closed when all bytes have been read or an I/O error, or other runtime
3014 * exception, is thrown. Bytes from the file are decoded into characters
3015 * using the specified charset.
3016 *
3017 * <p> This method recognizes the following as line terminators:
3018 * <ul>
3019 * <li> <code>\u000D</code> followed by <code>\u000A</code>,
3020 * CARRIAGE RETURN followed by LINE FEED </li>
3021 * <li> <code>\u000A</code>, LINE FEED </li>
3022 * <li> <code>\u000D</code>, CARRIAGE RETURN </li>
3023 * </ul>
3024 * <p> Additional Unicode line terminators may be recognized in future
3025 * releases.
3026 *
3027 * <p> Note that this method is intended for simple cases where it is
3028 * convenient to read all lines in a single operation. It is not intended
3029 * for reading in large files.
3030 *
3031 * @param path
3032 * the path to the file
3452 * @see #walk(Path, int, FileVisitOption...)
3453 * @since 1.8
3454 */
3455 public static CloseableStream<Path> find(Path start,
3456 int maxDepth,
3457 BiPredicate<Path, BasicFileAttributes> matcher,
3458 FileVisitOption... options)
3459 throws IOException
3460 {
3461 FileTreeIterator iterator = new FileTreeIterator(start, maxDepth, options);
3462
3463 Stream<Path> s = StreamSupport.stream(
3464 Spliterators.spliteratorUnknownSize(iterator, Spliterator.DISTINCT),
3465 false).
3466 filter(entry -> matcher.test(entry.file(), entry.attributes())).
3467 map(entry -> entry.file());
3468 return new DelegatingCloseableStream<>(iterator, s);
3469 }
3470
3471 /**
3472 * Read all lines from a file as a {@code CloseableStream}. Unlike {@link
3473 * #readAllLines(Path, Charset) readAllLines}, this method does not read
3474 * all lines into a {@code List}, but instead populates lazily as the stream
3475 * is consumed.
3476 *
3477 * <p> Bytes from the file are decoded into characters using the specified
3478 * charset and the same line terminators as specified by {@code
3479 * readAllLines} are supported.
3480 *
3481 * <p> After this method returns, then any subsequent I/O exception that
3482 * occurs while reading from the file or when a malformed or unmappable byte
3483 * sequence is read, is wrapped in an {@link UncheckedIOException} that will
3484 * be thrown form the
3485 * {@link java.util.stream.Stream} method that caused the read to take
3486 * place. In case an {@code IOException} is thrown when closing the file,
3487 * it is also wrapped as an {@code UncheckedIOException}.
3488 *
3489 * <p> When not using the try-with-resources construct, then stream's
3490 * {@link CloseableStream#close close} method should be invoked after
3491 * operation is completed so as to free any resources held for the open
3492 * file.
|
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.nio.file;
27
28 import java.nio.file.attribute.*;
29 import java.nio.file.spi.FileSystemProvider;
30 import java.nio.file.spi.FileTypeDetector;
31 import java.nio.channels.Channels;
32 import java.nio.channels.FileChannel;
33 import java.nio.channels.SeekableByteChannel;
34 import java.io.Closeable;
35 import java.io.InputStream;
36 import java.io.OutputStream;
37 import java.io.Reader;
38 import java.io.Writer;
39 import java.io.BufferedReader;
40 import java.io.BufferedWriter;
41 import java.io.InputStreamReader;
42 import java.io.OutputStreamWriter;
43 import java.io.IOException;
44 import java.io.UncheckedIOException;
45 import java.util.*;
46 import java.util.function.BiPredicate;
47 import java.util.stream.CloseableStream;
48 import java.util.stream.DelegatingStream;
49 import java.util.stream.Stream;
50 import java.util.stream.StreamSupport;
51 import java.security.AccessController;
2948 *
2949 * @return the number of bytes read or written
2950 *
2951 * @throws IOException
2952 * if an I/O error occurs when reading or writing
2953 * @throws SecurityException
2954 * In the case of the default provider, and a security manager is
2955 * installed, the {@link SecurityManager#checkRead(String) checkRead}
2956 * method is invoked to check read access to the file.
2957 */
2958 public static long copy(Path source, OutputStream out) throws IOException {
2959 // ensure not null before opening file
2960 Objects.requireNonNull(out);
2961
2962 try (InputStream in = newInputStream(source)) {
2963 return copy(in, out);
2964 }
2965 }
2966
2967 /**
2968 * The maximum size of array to allocate.
2969 * Some VMs reserve some header words in an array.
2970 * Attempts to allocate larger arrays may result in
2971 * OutOfMemoryError: Requested array size exceeds VM limit
2972 */
2973 private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
2974
2975 /**
2976 * Reads all the bytes from an input stream. Uses {@code initialSize} as a hint
2977 * about how many bytes the stream will have.
2978 *
2979 * @param source
2980 * the input stream to read from
2981 * @param initialSize
2982 * the initial size of the byte array to allocate
2983 *
2984 * @return a byte array containing the bytes read from the file
2985 *
2986 * @throws IOException
2987 * if an I/O error occurs reading from the stream
2988 * @throws OutOfMemoryError
2989 * if an array of the required size cannot be allocated
2990 */
2991 private static byte[] read(InputStream source, int initialSize)
2992 throws IOException
2993 {
2994 int capacity = initialSize;
2995 byte[] buf = new byte[capacity];
2996 int nread = 0;
2997 int rem = buf.length;
2998 int n;
2999 for (;;) {
3000 // read to EOF which may read more or less than initialSize (eg: file
3001 // is truncated while we are reading)
3002 while ((n = source.read(buf, nread, rem)) > 0) {
3003 nread += n;
3004 rem -= n;
3005 assert rem >= 0;
3006 }
3007
3008 // if last call to source.read() returned -1, we are done
3009 // otherwise, try to read one more byte; if that failed we're done too
3010 if (n < 0 || (n = source.read()) < 0)
3011 break;
3012
3013 // one more byte was read; need to allocate a bigger buffer
3014 int newCapacity = capacity << 1;
3015 if (newCapacity < BUFFER_SIZE) {
3016 if (newCapacity >= 0) {
3017 newCapacity = BUFFER_SIZE;
3018 } else { // overflow
3019 if (capacity == Integer.MAX_VALUE)
3020 throw new OutOfMemoryError("Required array size too large");
3021
3022 newCapacity = (capacity < MAX_BUFFER_SIZE) ?
3023 MAX_BUFFER_SIZE :
3024 Integer.MAX_VALUE;
3025 }
3026 }
3027 rem = newCapacity - capacity - 1;
3028 buf = Arrays.copyOf(buf, newCapacity);
3029 buf[nread++] = (byte)n;
3030 capacity = newCapacity;
3031 }
3032 return (capacity == nread) ? buf : Arrays.copyOf(buf, nread);
3033 }
3034
3035 /**
3036 * Reads all the bytes from a file. The method ensures that the file is
3037 * closed when all bytes have been read or an I/O error, or other runtime
3038 * exception, is thrown.
3039 *
3040 * <p> Note that this method is intended for simple cases where it is
3041 * convenient to read all bytes into a byte array. It is not intended for
3042 * reading in large files.
3043 *
3044 * @param path
3045 * the path to the file
3046 *
3047 * @return a byte array containing the bytes read from the file
3048 *
3049 * @throws IOException
3050 * if an I/O error occurs reading from the stream
3051 * @throws OutOfMemoryError
3052 * if an array of the required size cannot be allocated, for
3053 * example the file is larger that {@code 2GB}
3054 * @throws SecurityException
3055 * In the case of the default provider, and a security manager is
3056 * installed, the {@link SecurityManager#checkRead(String) checkRead}
3057 * method is invoked to check read access to the file.
3058 */
3059 public static byte[] readAllBytes(Path path) throws IOException {
3060 try (FileChannel fc = FileChannel.open(path);
3061 InputStream fis = Channels.newInputStream(fc)) {
3062 long size = fc.size();
3063 if (size > (long)Integer.MAX_VALUE)
3064 throw new OutOfMemoryError("Required array size too large");
3065
3066 return read(fis, (int)size);
3067 }
3068 }
3069
3070 /**
3071 * Reads all lines from a file. This method ensures that the file is
3072 * closed when all bytes have been read or an I/O error, or other runtime
3073 * exception, is thrown. Bytes from the file are decoded into characters
3074 * using the specified charset.
3075 *
3076 * <p> This method recognizes the following as line terminators:
3077 * <ul>
3078 * <li> <code>\u000D</code> followed by <code>\u000A</code>,
3079 * CARRIAGE RETURN followed by LINE FEED </li>
3080 * <li> <code>\u000A</code>, LINE FEED </li>
3081 * <li> <code>\u000D</code>, CARRIAGE RETURN </li>
3082 * </ul>
3083 * <p> Additional Unicode line terminators may be recognized in future
3084 * releases.
3085 *
3086 * <p> Note that this method is intended for simple cases where it is
3087 * convenient to read all lines in a single operation. It is not intended
3088 * for reading in large files.
3089 *
3090 * @param path
3091 * the path to the file
3511 * @see #walk(Path, int, FileVisitOption...)
3512 * @since 1.8
3513 */
3514 public static CloseableStream<Path> find(Path start,
3515 int maxDepth,
3516 BiPredicate<Path, BasicFileAttributes> matcher,
3517 FileVisitOption... options)
3518 throws IOException
3519 {
3520 FileTreeIterator iterator = new FileTreeIterator(start, maxDepth, options);
3521
3522 Stream<Path> s = StreamSupport.stream(
3523 Spliterators.spliteratorUnknownSize(iterator, Spliterator.DISTINCT),
3524 false).
3525 filter(entry -> matcher.test(entry.file(), entry.attributes())).
3526 map(entry -> entry.file());
3527 return new DelegatingCloseableStream<>(iterator, s);
3528 }
3529
3530 /**
3531 * Reads all lines from a file as a {@code CloseableStream}. Unlike {@link
3532 * #readAllLines(Path, Charset) readAllLines}, this method does not read
3533 * all lines into a {@code List}, but instead populates lazily as the stream
3534 * is consumed.
3535 *
3536 * <p> Bytes from the file are decoded into characters using the specified
3537 * charset and the same line terminators as specified by {@code
3538 * readAllLines} are supported.
3539 *
3540 * <p> After this method returns, then any subsequent I/O exception that
3541 * occurs while reading from the file or when a malformed or unmappable byte
3542 * sequence is read, is wrapped in an {@link UncheckedIOException} that will
3543 * be thrown form the
3544 * {@link java.util.stream.Stream} method that caused the read to take
3545 * place. In case an {@code IOException} is thrown when closing the file,
3546 * it is also wrapped as an {@code UncheckedIOException}.
3547 *
3548 * <p> When not using the try-with-resources construct, then stream's
3549 * {@link CloseableStream#close close} method should be invoked after
3550 * operation is completed so as to free any resources held for the open
3551 * file.
|