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 rem = Math.max(capacity, BUFFER_SIZE - capacity);
3015 if (MAX_BUFFER_SIZE - rem < capacity) {
3016 if (capacity == Integer.MAX_VALUE)
3017 throw new OutOfMemoryError("Required array size too large");
3018 rem = (capacity < MAX_BUFFER_SIZE) ?
3019 MAX_BUFFER_SIZE - capacity :
3020 Integer.MAX_VALUE - capacity;
3021 }
3022 capacity += rem;
3023 buf = Arrays.copyOf(buf, capacity);
3024 buf[nread++] = (byte)n;
3025 rem--;
3026 }
3027 return (capacity == nread) ? buf : Arrays.copyOf(buf, nread);
3028 }
3029
3030 /**
3031 * Reads all the bytes from a file. The method ensures that the file is
3032 * closed when all bytes have been read or an I/O error, or other runtime
3033 * exception, is thrown.
3034 *
3035 * <p> Note that this method is intended for simple cases where it is
3036 * convenient to read all bytes into a byte array. It is not intended for
3037 * reading in large files.
3038 *
3039 * @param path
3040 * the path to the file
3041 *
3042 * @return a byte array containing the bytes read from the file
3043 *
3044 * @throws IOException
3045 * if an I/O error occurs reading from the stream
3046 * @throws OutOfMemoryError
3047 * if an array of the required size cannot be allocated, for
3048 * example the file is larger that {@code 2GB}
3049 * @throws SecurityException
3050 * In the case of the default provider, and a security manager is
3051 * installed, the {@link SecurityManager#checkRead(String) checkRead}
3052 * method is invoked to check read access to the file.
3053 */
3054 public static byte[] readAllBytes(Path path) throws IOException {
3055 try (FileChannel fc = FileChannel.open(path);
3056 InputStream fis = Channels.newInputStream(fc)) {
3057 long size = fc.size();
3058 if (size > (long)Integer.MAX_VALUE)
3059 throw new OutOfMemoryError("Required array size too large");
3060
3061 return read(fis, (int)size);
3062 }
3063 }
3064
3065 /**
3066 * Reads all lines from a file. This method ensures that the file is
3067 * closed when all bytes have been read or an I/O error, or other runtime
3068 * exception, is thrown. Bytes from the file are decoded into characters
3069 * using the specified charset.
3070 *
3071 * <p> This method recognizes the following as line terminators:
3072 * <ul>
3073 * <li> <code>\u000D</code> followed by <code>\u000A</code>,
3074 * CARRIAGE RETURN followed by LINE FEED </li>
3075 * <li> <code>\u000A</code>, LINE FEED </li>
3076 * <li> <code>\u000D</code>, CARRIAGE RETURN </li>
3077 * </ul>
3078 * <p> Additional Unicode line terminators may be recognized in future
3079 * releases.
3080 *
3081 * <p> Note that this method is intended for simple cases where it is
3082 * convenient to read all lines in a single operation. It is not intended
3083 * for reading in large files.
3084 *
3085 * @param path
3086 * the path to the file
3506 * @see #walk(Path, int, FileVisitOption...)
3507 * @since 1.8
3508 */
3509 public static CloseableStream<Path> find(Path start,
3510 int maxDepth,
3511 BiPredicate<Path, BasicFileAttributes> matcher,
3512 FileVisitOption... options)
3513 throws IOException
3514 {
3515 FileTreeIterator iterator = new FileTreeIterator(start, maxDepth, options);
3516
3517 Stream<Path> s = StreamSupport.stream(
3518 Spliterators.spliteratorUnknownSize(iterator, Spliterator.DISTINCT),
3519 false).
3520 filter(entry -> matcher.test(entry.file(), entry.attributes())).
3521 map(entry -> entry.file());
3522 return new DelegatingCloseableStream<>(iterator, s);
3523 }
3524
3525 /**
3526 * Reads all lines from a file as a {@code CloseableStream}. Unlike {@link
3527 * #readAllLines(Path, Charset) readAllLines}, this method does not read
3528 * all lines into a {@code List}, but instead populates lazily as the stream
3529 * is consumed.
3530 *
3531 * <p> Bytes from the file are decoded into characters using the specified
3532 * charset and the same line terminators as specified by {@code
3533 * readAllLines} are supported.
3534 *
3535 * <p> After this method returns, then any subsequent I/O exception that
3536 * occurs while reading from the file or when a malformed or unmappable byte
3537 * sequence is read, is wrapped in an {@link UncheckedIOException} that will
3538 * be thrown form the
3539 * {@link java.util.stream.Stream} method that caused the read to take
3540 * place. In case an {@code IOException} is thrown when closing the file,
3541 * it is also wrapped as an {@code UncheckedIOException}.
3542 *
3543 * <p> When not using the try-with-resources construct, then stream's
3544 * {@link CloseableStream#close close} method should be invoked after
3545 * operation is completed so as to free any resources held for the open
3546 * file.
|