60 import java.nio.file.spi.FileTypeDetector;
61 import java.security.AccessController;
62 import java.security.PrivilegedAction;
63 import java.util.ArrayList;
64 import java.util.Arrays;
65 import java.util.Collections;
66 import java.util.EnumSet;
67 import java.util.HashSet;
68 import java.util.Iterator;
69 import java.util.List;
70 import java.util.Map;
71 import java.util.Objects;
72 import java.util.ServiceLoader;
73 import java.util.Set;
74 import java.util.Spliterator;
75 import java.util.Spliterators;
76 import java.util.function.BiPredicate;
77 import java.util.stream.Stream;
78 import java.util.stream.StreamSupport;
79
80 import sun.nio.fs.AbstractFileSystemProvider;
81
82 /**
83 * This class consists exclusively of static methods that operate on files,
84 * directories, or other types of files.
85 *
86 * <p> In most cases, the methods defined here will delegate to the associated
87 * file system provider to perform the file operations.
88 *
89 * @since 1.7
90 */
91
92 public final class Files {
93 private Files() { }
94
95 /**
96 * Returns the {@code FileSystemProvider} to delegate to.
97 */
98 private static FileSystemProvider provider(Path path) {
99 return path.getFileSystem().provider();
3049 }
3050 }
3051
3052 // attempt to create target file. If it fails with
3053 // FileAlreadyExistsException then it may be because the security
3054 // manager prevented us from deleting the file, in which case we just
3055 // throw the SecurityException.
3056 OutputStream ostream;
3057 try {
3058 ostream = newOutputStream(target, StandardOpenOption.CREATE_NEW,
3059 StandardOpenOption.WRITE);
3060 } catch (FileAlreadyExistsException x) {
3061 if (se != null)
3062 throw se;
3063 // someone else won the race and created the file
3064 throw x;
3065 }
3066
3067 // do the copy
3068 try (OutputStream out = ostream) {
3069 return in.transferTo(out);
3070 }
3071 }
3072
3073 /**
3074 * Copies all bytes from a file to an output stream.
3075 *
3076 * <p> If an I/O error occurs reading from the file or writing to the output
3077 * stream, then it may do so after some bytes have been read or written.
3078 * Consequently the output stream may be in an inconsistent state. It is
3079 * strongly recommended that the output stream be promptly closed if an I/O
3080 * error occurs.
3081 *
3082 * <p> This method may block indefinitely writing to the output stream (or
3083 * reading from the file). The behavior for the case that the output stream
3084 * is <i>asynchronously closed</i> or the thread interrupted during the copy
3085 * is highly output stream and file system provider specific and therefore
3086 * not specified.
3087 *
3088 * <p> Note that if the given output stream is {@link java.io.Flushable}
3089 * then its {@link java.io.Flushable#flush flush} method may need to invoked
3091 *
3092 * @param source
3093 * the path to the file
3094 * @param out
3095 * the output stream to write to
3096 *
3097 * @return the number of bytes read or written
3098 *
3099 * @throws IOException
3100 * if an I/O error occurs when reading or writing
3101 * @throws SecurityException
3102 * In the case of the default provider, and a security manager is
3103 * installed, the {@link SecurityManager#checkRead(String) checkRead}
3104 * method is invoked to check read access to the file.
3105 */
3106 public static long copy(Path source, OutputStream out) throws IOException {
3107 // ensure not null before opening file
3108 Objects.requireNonNull(out);
3109
3110 try (InputStream in = newInputStream(source)) {
3111 return in.transferTo(out);
3112 }
3113 }
3114
3115 /**
3116 * The maximum size of array to allocate.
3117 * Some VMs reserve some header words in an array.
3118 * Attempts to allocate larger arrays may result in
3119 * OutOfMemoryError: Requested array size exceeds VM limit
3120 */
3121 private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
3122
3123 /**
3124 * Reads all the bytes from an input stream. Uses {@code initialSize} as a hint
3125 * about how many bytes the stream will have.
3126 *
3127 * @param source
3128 * the input stream to read from
3129 * @param initialSize
3130 * the initial size of the byte array to allocate
3131 *
|
60 import java.nio.file.spi.FileTypeDetector;
61 import java.security.AccessController;
62 import java.security.PrivilegedAction;
63 import java.util.ArrayList;
64 import java.util.Arrays;
65 import java.util.Collections;
66 import java.util.EnumSet;
67 import java.util.HashSet;
68 import java.util.Iterator;
69 import java.util.List;
70 import java.util.Map;
71 import java.util.Objects;
72 import java.util.ServiceLoader;
73 import java.util.Set;
74 import java.util.Spliterator;
75 import java.util.Spliterators;
76 import java.util.function.BiPredicate;
77 import java.util.stream.Stream;
78 import java.util.stream.StreamSupport;
79
80 import jdk.internal.io.IOSupport;
81
82 import sun.nio.fs.AbstractFileSystemProvider;
83
84 /**
85 * This class consists exclusively of static methods that operate on files,
86 * directories, or other types of files.
87 *
88 * <p> In most cases, the methods defined here will delegate to the associated
89 * file system provider to perform the file operations.
90 *
91 * @since 1.7
92 */
93
94 public final class Files {
95 private Files() { }
96
97 /**
98 * Returns the {@code FileSystemProvider} to delegate to.
99 */
100 private static FileSystemProvider provider(Path path) {
101 return path.getFileSystem().provider();
3051 }
3052 }
3053
3054 // attempt to create target file. If it fails with
3055 // FileAlreadyExistsException then it may be because the security
3056 // manager prevented us from deleting the file, in which case we just
3057 // throw the SecurityException.
3058 OutputStream ostream;
3059 try {
3060 ostream = newOutputStream(target, StandardOpenOption.CREATE_NEW,
3061 StandardOpenOption.WRITE);
3062 } catch (FileAlreadyExistsException x) {
3063 if (se != null)
3064 throw se;
3065 // someone else won the race and created the file
3066 throw x;
3067 }
3068
3069 // do the copy
3070 try (OutputStream out = ostream) {
3071 return IOSupport.copy(in, out);
3072 }
3073 }
3074
3075 /**
3076 * Copies all bytes from a file to an output stream.
3077 *
3078 * <p> If an I/O error occurs reading from the file or writing to the output
3079 * stream, then it may do so after some bytes have been read or written.
3080 * Consequently the output stream may be in an inconsistent state. It is
3081 * strongly recommended that the output stream be promptly closed if an I/O
3082 * error occurs.
3083 *
3084 * <p> This method may block indefinitely writing to the output stream (or
3085 * reading from the file). The behavior for the case that the output stream
3086 * is <i>asynchronously closed</i> or the thread interrupted during the copy
3087 * is highly output stream and file system provider specific and therefore
3088 * not specified.
3089 *
3090 * <p> Note that if the given output stream is {@link java.io.Flushable}
3091 * then its {@link java.io.Flushable#flush flush} method may need to invoked
3093 *
3094 * @param source
3095 * the path to the file
3096 * @param out
3097 * the output stream to write to
3098 *
3099 * @return the number of bytes read or written
3100 *
3101 * @throws IOException
3102 * if an I/O error occurs when reading or writing
3103 * @throws SecurityException
3104 * In the case of the default provider, and a security manager is
3105 * installed, the {@link SecurityManager#checkRead(String) checkRead}
3106 * method is invoked to check read access to the file.
3107 */
3108 public static long copy(Path source, OutputStream out) throws IOException {
3109 // ensure not null before opening file
3110 Objects.requireNonNull(out);
3111
3112 try (InputStream in = newInputStream(source)) {
3113 return IOSupport.copy(in, out);
3114 }
3115 }
3116
3117 /**
3118 * The maximum size of array to allocate.
3119 * Some VMs reserve some header words in an array.
3120 * Attempts to allocate larger arrays may result in
3121 * OutOfMemoryError: Requested array size exceeds VM limit
3122 */
3123 private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
3124
3125 /**
3126 * Reads all the bytes from an input stream. Uses {@code initialSize} as a hint
3127 * about how many bytes the stream will have.
3128 *
3129 * @param source
3130 * the input stream to read from
3131 * @param initialSize
3132 * the initial size of the byte array to allocate
3133 *
|