|
77 | 77 | import java.util.stream.Stream; |
78 | 78 | import java.util.stream.StreamSupport; |
79 | 79 |
|
| 80 | +import swingjs.JSFileSystem.JSPath; |
| 81 | + |
80 | 82 | /** |
81 | 83 | * This class consists exclusively of static methods that operate on files, |
82 | 84 | * directories, or other types of files. |
@@ -3125,40 +3127,41 @@ private static byte[] read(InputStream source, int initialSize) throws IOExcepti |
3125 | 3127 | return (capacity == nread) ? buf : Arrays.copyOf(buf, nread); |
3126 | 3128 | } |
3127 | 3129 |
|
3128 | | - /** |
3129 | | - * Reads all the bytes from a file. The method ensures that the file is |
3130 | | - * closed when all bytes have been read or an I/O error, or other runtime |
3131 | | - * exception, is thrown. |
3132 | | - * |
3133 | | - * <p> Note that this method is intended for simple cases where it is |
3134 | | - * convenient to read all bytes into a byte array. It is not intended for |
3135 | | - * reading in large files. |
3136 | | - * |
3137 | | - * @param path |
3138 | | - * the path to the file |
3139 | | - * |
3140 | | - * @return a byte array containing the bytes read from the file |
3141 | | - * |
3142 | | - * @throws IOException |
3143 | | - * if an I/O error occurs reading from the stream |
3144 | | - * @throws OutOfMemoryError |
3145 | | - * if an array of the required size cannot be allocated, for |
3146 | | - * example the file is larger that {@code 2GB} |
3147 | | - * @throws SecurityException |
3148 | | - * In the case of the default provider, and a security manager is |
3149 | | - * installed, the {@link SecurityManager#checkRead(String) checkRead} |
3150 | | - * method is invoked to check read access to the file. |
3151 | | - */ |
3152 | | - public static byte[] readAllBytes(Path path) throws IOException { |
3153 | | - try (SeekableByteChannel sbc = Files.newByteChannel(path); |
3154 | | - InputStream in = Channels.newInputStream(sbc)) { |
3155 | | - long size = sbc.size(); |
3156 | | - if (size > (long)MAX_BUFFER_SIZE) |
3157 | | - throw new OutOfMemoryError("Required array size too large"); |
3158 | | - |
3159 | | - return read(in, (int)size); |
3160 | | - } |
3161 | | - } |
| 3130 | + /** |
| 3131 | + * Reads all the bytes from a file. The method ensures that the file is closed |
| 3132 | + * when all bytes have been read or an I/O error, or other runtime exception, is |
| 3133 | + * thrown. |
| 3134 | + * |
| 3135 | + * <p> |
| 3136 | + * Note that this method is intended for simple cases where it is convenient to |
| 3137 | + * read all bytes into a byte array. It is not intended for reading in large |
| 3138 | + * files. |
| 3139 | + * |
| 3140 | + * @param path the path to the file |
| 3141 | + * |
| 3142 | + * @return a byte array containing the bytes read from the file |
| 3143 | + * |
| 3144 | + * @throws IOException if an I/O error occurs reading from the stream |
| 3145 | + * @throws OutOfMemoryError if an array of the required size cannot be |
| 3146 | + * allocated, for example the file is larger that |
| 3147 | + * {@code 2GB} |
| 3148 | + * @throws SecurityException In the case of the default provider, and a security |
| 3149 | + * manager is installed, the |
| 3150 | + * {@link SecurityManager#checkRead(String) checkRead} |
| 3151 | + * method is invoked to check read access to the file. |
| 3152 | + */ |
| 3153 | + public static byte[] readAllBytes(Path path) throws IOException { |
| 3154 | + byte[] bytes = ((JSPath) path).秘bytes; |
| 3155 | + if (bytes == null) { |
| 3156 | + try (SeekableByteChannel sbc = Files.newByteChannel(path); InputStream in = Channels.newInputStream(sbc)) { |
| 3157 | + long size = sbc.size(); |
| 3158 | + if (size > (long) MAX_BUFFER_SIZE) // 2.1 GB; no reason that could not be higher? |
| 3159 | + throw new OutOfMemoryError("Required array size too large"); |
| 3160 | + ((JSPath) path).秘bytes = bytes = read(in, (int) size); |
| 3161 | + } |
| 3162 | + } |
| 3163 | + return bytes; |
| 3164 | + } |
3162 | 3165 |
|
3163 | 3166 | /** |
3164 | 3167 | * Read all lines from a file. This method ensures that the file is |
|
0 commit comments