| 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | package org.apache.commons.io.file; |
| 19 | |
| 20 | import java.io.BufferedReader; |
| 21 | import java.io.BufferedWriter; |
| 22 | import java.io.IOException; |
| 23 | import java.io.InputStream; |
| 24 | import java.io.OutputStream; |
| 25 | import java.io.Reader; |
| 26 | import java.io.UncheckedIOException; |
| 27 | import java.nio.channels.SeekableByteChannel; |
| 28 | import java.nio.charset.Charset; |
| 29 | import java.nio.file.CopyOption; |
| 30 | import java.nio.file.DirectoryStream; |
| 31 | import java.nio.file.FileStore; |
| 32 | import java.nio.file.FileVisitOption; |
| 33 | import java.nio.file.FileVisitor; |
| 34 | import java.nio.file.Files; |
| 35 | import java.nio.file.LinkOption; |
| 36 | import java.nio.file.OpenOption; |
| 37 | import java.nio.file.Path; |
| 38 | import java.nio.file.attribute.BasicFileAttributes; |
| 39 | import java.nio.file.attribute.FileAttribute; |
| 40 | import java.nio.file.attribute.FileTime; |
| 41 | import java.nio.file.attribute.PosixFilePermission; |
| 42 | import java.nio.file.attribute.UserPrincipal; |
| 43 | import java.util.List; |
| 44 | import java.util.Map; |
| 45 | import java.util.Set; |
| 46 | import java.util.function.BiPredicate; |
| 47 | import java.util.stream.Stream; |
| 48 | |
| 49 | import org.apache.commons.io.function.Uncheck; |
| 50 | |
| 51 | /** |
| 52 | * Delegates to {@link Files} to uncheck calls by throwing {@link UncheckedIOException} instead of {@link IOException}. |
| 53 | * |
| 54 | * @see Files |
| 55 | * @see IOException |
| 56 | * @see UncheckedIOException |
| 57 | * @since 2.12.0 |
| 58 | */ |
| 59 | public final class FilesUncheck { |
| 60 | |
| 61 | /** |
| 62 | * Delegates to {@link Files#copy(InputStream, Path, CopyOption...)} throwing {@link UncheckedIOException} instead of |
| 63 | * {@link IOException}. |
| 64 | * |
| 65 | * @param in See delegate. |
| 66 | * @param target See delegate. |
| 67 | * @param options See delegate. |
| 68 | * @return See delegate. |
| 69 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 70 | * @see Files#copy(InputStream, Path,CopyOption...) |
| 71 | */ |
| 72 | public static long copy(final InputStream in, final Path target, final CopyOption... options) { |
| 73 | return Uncheck.apply(Files::copy, in, target, options); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Delegates to {@link Files#copy(Path, OutputStream)} throwing {@link UncheckedIOException} instead of |
| 78 | * {@link IOException}. |
| 79 | * |
| 80 | * @param source See delegate. See delegate. |
| 81 | * @param out See delegate. See delegate. |
| 82 | * @return See delegate. See delegate. |
| 83 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 84 | */ |
| 85 | public static long copy(final Path source, final OutputStream out) { |
| 86 | return Uncheck.apply(Files::copy, source, out); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Delegates to {@link Files#copy(Path, Path, CopyOption...)} throwing {@link UncheckedIOException} instead of |
| 91 | * {@link IOException}. |
| 92 | * |
| 93 | * @param source See delegate. |
| 94 | * @param target See delegate. |
| 95 | * @param options See delegate. |
| 96 | * @return See delegate. |
| 97 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 98 | */ |
| 99 | public static Path copy(final Path source, final Path target, final CopyOption... options) { |
| 100 | return Uncheck.apply(Files::copy, source, target, options); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Delegates to {@link Files#createDirectories(Path, FileAttribute...)} throwing {@link UncheckedIOException} instead of |
| 105 | * {@link IOException}. |
| 106 | * |
| 107 | * @param dir See delegate. |
| 108 | * @param attrs See delegate. |
| 109 | * @return See delegate. |
| 110 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 111 | */ |
| 112 | public static Path createDirectories(final Path dir, final FileAttribute<?>... attrs) { |
| 113 | return Uncheck.apply(Files::createDirectories, dir, attrs); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Delegates to {@link Files#createDirectory(Path, FileAttribute...)} throwing {@link UncheckedIOException} instead of |
| 118 | * {@link IOException}. |
| 119 | * |
| 120 | * @param dir See delegate. |
| 121 | * @param attrs See delegate. |
| 122 | * @return See delegate. |
| 123 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 124 | */ |
| 125 | public static Path createDirectory(final Path dir, final FileAttribute<?>... attrs) { |
| 126 | return Uncheck.apply(Files::createDirectory, dir, attrs); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Delegates to {@link Files#createFile(Path, FileAttribute...)} throwing {@link UncheckedIOException} instead of |
| 131 | * {@link IOException}. |
| 132 | * |
| 133 | * @param path See delegate. |
| 134 | * @param attrs See delegate. |
| 135 | * @return See delegate. |
| 136 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 137 | */ |
| 138 | public static Path createFile(final Path path, final FileAttribute<?>... attrs) { |
| 139 | return Uncheck.apply(Files::createFile, path, attrs); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Delegates to {@link Files#createLink(Path, Path)} throwing {@link UncheckedIOException} instead of |
| 144 | * {@link IOException}. |
| 145 | * |
| 146 | * @param link See delegate. |
| 147 | * @param existing See delegate. |
| 148 | * @return See delegate. |
| 149 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 150 | */ |
| 151 | public static Path createLink(final Path link, final Path existing) { |
| 152 | return Uncheck.apply(Files::createLink, link, existing); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Delegates to {@link Files#createSymbolicLink(Path, Path, FileAttribute...)} throwing {@link UncheckedIOException} |
| 157 | * instead of {@link IOException}. |
| 158 | * |
| 159 | * @param link See delegate. |
| 160 | * @param target See delegate. |
| 161 | * @param attrs See delegate. |
| 162 | * @return See delegate. |
| 163 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 164 | */ |
| 165 | public static Path createSymbolicLink(final Path link, final Path target, final FileAttribute<?>... attrs) { |
| 166 | return Uncheck.apply(Files::createSymbolicLink, link, target, attrs); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Delegates to {@link Files#createTempDirectory(Path, String, FileAttribute...)} throwing {@link UncheckedIOException} |
| 171 | * instead of {@link IOException}. |
| 172 | * |
| 173 | * @param dir See delegate. |
| 174 | * @param prefix See delegate. |
| 175 | * @param attrs See delegate. |
| 176 | * @return See delegate. |
| 177 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 178 | */ |
| 179 | public static Path createTempDirectory(final Path dir, final String prefix, final FileAttribute<?>... attrs) { |
| 180 | return Uncheck.apply(Files::createTempDirectory, dir, prefix, attrs); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Delegates to {@link Files#createTempDirectory(String, FileAttribute...)} throwing {@link UncheckedIOException} |
| 185 | * instead of {@link IOException}. |
| 186 | * |
| 187 | * @param prefix See delegate. |
| 188 | * @param attrs See delegate. |
| 189 | * @return See delegate. |
| 190 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 191 | */ |
| 192 | public static Path createTempDirectory(final String prefix, final FileAttribute<?>... attrs) { |
| 193 | return Uncheck.apply(Files::createTempDirectory, prefix, attrs); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Delegates to {@link Files#createTempFile(Path, String, String, FileAttribute...)} throwing |
| 198 | * {@link UncheckedIOException} instead of {@link IOException}. |
| 199 | * |
| 200 | * @param dir See delegate. |
| 201 | * @param prefix See delegate. |
| 202 | * @param suffix See delegate. |
| 203 | * @param attrs See delegate. |
| 204 | * @return See delegate. |
| 205 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 206 | */ |
| 207 | public static Path createTempFile(final Path dir, final String prefix, final String suffix, final FileAttribute<?>... attrs) { |
| 208 | return Uncheck.apply(Files::createTempFile, dir, prefix, suffix, attrs); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Delegates to {@link Files#createTempFile(String, String, FileAttribute...)} throwing {@link UncheckedIOException} |
| 213 | * instead of {@link IOException}. |
| 214 | * |
| 215 | * @param prefix See delegate. |
| 216 | * @param suffix See delegate. |
| 217 | * @param attrs See delegate. |
| 218 | * @return See delegate. |
| 219 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 220 | */ |
| 221 | public static Path createTempFile(final String prefix, final String suffix, final FileAttribute<?>... attrs) { |
| 222 | return Uncheck.apply(Files::createTempFile, prefix, suffix, attrs); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Delegates to {@link Files#delete(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. |
| 227 | * |
| 228 | * @param path See delegate. |
| 229 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 230 | */ |
| 231 | public static void delete(final Path path) { |
| 232 | Uncheck.accept(Files::delete, path); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Delegates to {@link Files#deleteIfExists(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. |
| 237 | * |
| 238 | * @param path See delegate. |
| 239 | * @return See delegate. |
| 240 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 241 | */ |
| 242 | public static boolean deleteIfExists(final Path path) { |
| 243 | return Uncheck.apply(Files::deleteIfExists, path); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Delegates to {@link Files#find(Path, int, BiPredicate, FileVisitOption...)} throwing {@link UncheckedIOException} instead of {@link IOException}. |
| 248 | * <p> |
| 249 | * The returned {@link Stream} wraps a {@link DirectoryStream}. When you require timely disposal of file system resources, use a {@code try}-with-resources |
| 250 | * block to ensure invocation of the stream's {@link Stream#close()} method after the stream operations are completed. |
| 251 | * </p> |
| 252 | * |
| 253 | * @param start See delegate. |
| 254 | * @param maxDepth See delegate. |
| 255 | * @param matcher See delegate. |
| 256 | * @param options See delegate. |
| 257 | * @return See delegate. |
| 258 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 259 | * @since 2.14.0 |
| 260 | */ |
| 261 | public static Stream<Path> find(final Path start, final int maxDepth, final BiPredicate<Path, BasicFileAttributes> matcher, |
| 262 | final FileVisitOption... options) { |
| 263 | return Uncheck.apply(Files::find, start, maxDepth, matcher, options); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Delegates to {@link Files#getAttribute(Path, String, LinkOption...)} throwing {@link UncheckedIOException} instead of |
| 268 | * {@link IOException}. |
| 269 | * |
| 270 | * @param path See delegate. |
| 271 | * @param attribute See delegate. |
| 272 | * @param options See delegate. |
| 273 | * @return See delegate. |
| 274 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 275 | */ |
| 276 | public static Object getAttribute(final Path path, final String attribute, final LinkOption... options) { |
| 277 | return Uncheck.apply(Files::getAttribute, path, attribute, options); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Delegates to {@link Files#getFileStore(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. |
| 282 | * |
| 283 | * @param path See delegate. |
| 284 | * @return See delegate. |
| 285 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 286 | */ |
| 287 | public static FileStore getFileStore(final Path path) { |
| 288 | return Uncheck.apply(Files::getFileStore, path); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Delegates to {@link Files#getLastModifiedTime(Path, LinkOption...)} throwing {@link UncheckedIOException} instead of |
| 293 | * {@link IOException}. |
| 294 | * |
| 295 | * @param path See delegate. |
| 296 | * @param options See delegate. |
| 297 | * @return See delegate. |
| 298 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 299 | */ |
| 300 | public static FileTime getLastModifiedTime(final Path path, final LinkOption... options) { |
| 301 | return Uncheck.apply(Files::getLastModifiedTime, path, options); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Delegates to {@link Files#getOwner(Path, LinkOption...)} throwing {@link UncheckedIOException} instead of |
| 306 | * {@link IOException}. |
| 307 | * |
| 308 | * @param path See delegate. |
| 309 | * @param options See delegate. |
| 310 | * @return See delegate. |
| 311 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 312 | */ |
| 313 | public static UserPrincipal getOwner(final Path path, final LinkOption... options) { |
| 314 | return Uncheck.apply(Files::getOwner, path, options); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Delegates to {@link Files#getPosixFilePermissions(Path, LinkOption...)} throwing {@link UncheckedIOException} instead |
| 319 | * of {@link IOException}. |
| 320 | * |
| 321 | * @param path See delegate. |
| 322 | * @param options See delegate. |
| 323 | * @return See delegate. |
| 324 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 325 | */ |
| 326 | public static Set<PosixFilePermission> getPosixFilePermissions(final Path path, final LinkOption... options) { |
| 327 | return Uncheck.apply(Files::getPosixFilePermissions, path, options); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Delegates to {@link Files#isHidden(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. |
| 332 | * |
| 333 | * @param path See delegate. |
| 334 | * @return See delegate. |
| 335 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 336 | */ |
| 337 | public static boolean isHidden(final Path path) { |
| 338 | return Uncheck.apply(Files::isHidden, path); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Delegates to {@link Files#isSameFile(Path, Path)} throwing {@link UncheckedIOException} instead of |
| 343 | * {@link IOException}. |
| 344 | * |
| 345 | * @param path See delegate. |
| 346 | * @param path2 See delegate. |
| 347 | * @return See delegate. |
| 348 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 349 | */ |
| 350 | public static boolean isSameFile(final Path path, final Path path2) { |
| 351 | return Uncheck.apply(Files::isSameFile, path, path2); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Delegates to {@link Files#lines(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. |
| 356 | * <p> |
| 357 | * The returned {@link Stream} wraps a {@link Reader}. When you require timely disposal of file system resources, use a {@code try}-with-resources block to |
| 358 | * ensure invocation of the stream's {@link Stream#close()} method after the stream operations are completed. |
| 359 | * </p> |
| 360 | * |
| 361 | * @param path See delegate. |
| 362 | * @return See delegate. |
| 363 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 364 | */ |
| 365 | public static Stream<String> lines(final Path path) { |
| 366 | return Uncheck.apply(Files::lines, path); |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Delegates to {@link Files#lines(Path, Charset)} throwing {@link UncheckedIOException} instead of {@link IOException}. |
| 371 | * <p> |
| 372 | * The returned {@link Stream} wraps a {@link Reader}. When you require timely disposal of file system resources, use a {@code try}-with-resources block to |
| 373 | * ensure invocation of the stream's {@link Stream#close()} method after the stream operations are completed. |
| 374 | * </p> |
| 375 | * |
| 376 | * @param path See delegate. |
| 377 | * @param cs See delegate. |
| 378 | * @return See delegate. |
| 379 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 380 | */ |
| 381 | public static Stream<String> lines(final Path path, final Charset cs) { |
| 382 | return Uncheck.apply(Files::lines, path, cs); |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Delegates to {@link Files#list(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. |
| 387 | * <p> |
| 388 | * The returned {@link Stream} wraps a {@link DirectoryStream}. When you require timely disposal of file system resources, use a {@code try}-with-resources |
| 389 | * block to ensure invocation of the stream's {@link Stream#close()} method after the stream operations are completed. |
| 390 | * </p> |
| 391 | * |
| 392 | * @param dir See delegate. |
| 393 | * @return See delegate. |
| 394 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 395 | */ |
| 396 | public static Stream<Path> list(final Path dir) { |
| 397 | return Uncheck.apply(Files::list, dir); |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Delegates to {@link Files#move(Path, Path, CopyOption...)} throwing {@link UncheckedIOException} instead of |
| 402 | * {@link IOException}. |
| 403 | * |
| 404 | * @param source See delegate. |
| 405 | * @param target See delegate. |
| 406 | * @param options See delegate. |
| 407 | * @return See delegate. |
| 408 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 409 | */ |
| 410 | public static Path move(final Path source, final Path target, final CopyOption... options) { |
| 411 | return Uncheck.apply(Files::move, source, target, options); |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Delegates to {@link Files#newBufferedReader(Path)} throwing {@link UncheckedIOException} instead of |
| 416 | * {@link IOException}. |
| 417 | * |
| 418 | * @param path See delegate. |
| 419 | * @return See delegate. |
| 420 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 421 | */ |
| 422 | public static BufferedReader newBufferedReader(final Path path) { |
| 423 | return Uncheck.apply(Files::newBufferedReader, path); |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Delegates to {@link Files#newBufferedReader(Path, Charset)} throwing {@link UncheckedIOException} instead of |
| 428 | * {@link IOException}. |
| 429 | * |
| 430 | * @param path See delegate. |
| 431 | * @param cs See delegate. |
| 432 | * @return See delegate. |
| 433 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 434 | */ |
| 435 | public static BufferedReader newBufferedReader(final Path path, final Charset cs) { |
| 436 | return Uncheck.apply(Files::newBufferedReader, path, cs); |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Delegates to {@link Files#newBufferedWriter(Path, Charset, OpenOption...)} throwing {@link UncheckedIOException} |
| 441 | * instead of {@link IOException}. |
| 442 | * |
| 443 | * @param path See delegate. |
| 444 | * @param cs See delegate. |
| 445 | * @param options See delegate. |
| 446 | * @return See delegate. |
| 447 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 448 | */ |
| 449 | public static BufferedWriter newBufferedWriter(final Path path, final Charset cs, final OpenOption... options) { |
| 450 | return Uncheck.apply(Files::newBufferedWriter, path, cs, options); |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Delegates to {@link Files#newBufferedWriter(Path, OpenOption...)} throwing {@link UncheckedIOException} instead of |
| 455 | * {@link IOException}. |
| 456 | * |
| 457 | * @param path See delegate. |
| 458 | * @param options See delegate. |
| 459 | * @return See delegate. |
| 460 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 461 | */ |
| 462 | public static BufferedWriter newBufferedWriter(final Path path, final OpenOption... options) { |
| 463 | return Uncheck.apply(Files::newBufferedWriter, path, options); |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Delegates to {@link Files#newByteChannel(Path, OpenOption...)} throwing {@link UncheckedIOException} instead of |
| 468 | * {@link IOException}. |
| 469 | * |
| 470 | * @param path See delegate. |
| 471 | * @param options See delegate. |
| 472 | * @return See delegate. |
| 473 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 474 | */ |
| 475 | public static SeekableByteChannel newByteChannel(final Path path, final OpenOption... options) { |
| 476 | return Uncheck.apply(Files::newByteChannel, path, options); |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Delegates to {@link Files#newByteChannel(Path, Set, FileAttribute...)} throwing {@link UncheckedIOException} instead |
| 481 | * of {@link IOException}. |
| 482 | * |
| 483 | * @param path See delegate. |
| 484 | * @param options See delegate. |
| 485 | * @param attrs See delegate. |
| 486 | * @return See delegate. |
| 487 | * @throws UncheckedIOException Wraps an {@link IOException}. |
| 488 | */ |
| 489 | public static SeekableByteChannel newByteChannel(final Path path, final Set<? extends OpenOption> options, final FileAttribute<?>... attrs) { |
| 490 | return Uncheck.apply(Files::newByteChannel, path, options, attrs); |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * Delegates to {@link Files#newDirectoryStream(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. |
| 495 | * <p> |
| 496 | * If you don't use the try-with-resources construct, then you must call the stream's {@link Stream#close()} method after iteration is complete to free any |
| 497 | * resources held for the open directory. |
| 498 | * </p> |
| 499 | * |
| 500 | * @param dir See delegate. |
| 501 | * @return See delegate. |
| 502 | */ |
| 503 | public static DirectoryStream<Path> newDirectoryStream(final Path dir) { |
| 504 | return Uncheck.apply(Files::newDirectoryStream, dir); |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Delegates to {@link Files#newDirectoryStream(Path, java.nio.file.DirectoryStream.Filter)} throwing {@link UncheckedIOException} instead of |
| 509 | * {@link IOException}. |
| 510 | * <p> |
| 511 | * If you don't use the try-with-resources construct, then you must call the stream's {@link Stream#close()} method after iteration is complete to free any |
| 512 | * resources held for the open directory. |
| 513 | * </p> |
| 514 | * |
| 515 | * @param dir See delegate. |
| 516 | * @param filter See delegate. |
| 517 | * @return See delegate. |
| 518 | */ |
| 519 | public static DirectoryStream<Path> newDirectoryStream(final Path dir, final DirectoryStream.Filter<? super Path> filter) { |
| 520 | return Uncheck.apply(Files::newDirectoryStream, dir, filter); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Delegates to {@link Files#newDirectoryStream(Path, String)} throwing {@link UncheckedIOException} instead of |
| 525 | * {@link IOException}. |
| 526 | * <p> |
| 527 | * The returned {@link Stream} wraps a {@link DirectoryStream}. When you require timely disposal of file system resources, use a {@code try}-with-resources |
| 528 | * block to ensure invocation of the stream's {@link Stream#close()} method after the stream operations are completed. |
| 529 | * </p> |
| 530 | * |
| 531 | * @param dir See delegate. |
| 532 | * @param glob See delegate. |
| 533 | * @return See delegate. |
| 534 | */ |
| 535 | public static DirectoryStream<Path> newDirectoryStream(final Path dir, final String glob) { |
| 536 | return Uncheck.apply(Files::newDirectoryStream, dir, glob); |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * Delegates to {@link Files#newInputStream(Path, OpenOption...)} throwing {@link UncheckedIOException} instead of |
| 541 | * {@link IOException}. |
| 542 | * |
| 543 | * @param path See delegate. |
| 544 | * @param options See delegate. |
| 545 | * @return See delegate. |
| 546 | */ |
| 547 | public static InputStream newInputStream(final Path path, final OpenOption... options) { |
| 548 | return Uncheck.apply(Files::newInputStream, path, options); |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * Delegates to {@link Files#newOutputStream(Path, OpenOption...)} throwing {@link UncheckedIOException} instead of |
| 553 | * {@link IOException}. |
| 554 | * |
| 555 | * @param path See delegate. |
| 556 | * @param options See delegate. |
| 557 | * @return See delegate. |
| 558 | */ |
| 559 | public static OutputStream newOutputStream(final Path path, final OpenOption... options) { |
| 560 | return Uncheck.apply(Files::newOutputStream, path, options); |
| 561 | } |
| 562 | |
| 563 | /** |
| 564 | * Delegates to {@link Files#probeContentType(Path)} throwing {@link UncheckedIOException} instead of |
| 565 | * {@link IOException}. |
| 566 | * |
| 567 | * @param path See delegate. |
| 568 | * @return See delegate. |
| 569 | */ |
| 570 | public static String probeContentType(final Path path) { |
| 571 | return Uncheck.apply(Files::probeContentType, path); |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * Delegates to {@link Files#readAllBytes(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. |
| 576 | * |
| 577 | * @param path See delegate. |
| 578 | * @return See delegate. |
| 579 | */ |
| 580 | public static byte[] readAllBytes(final Path path) { |
| 581 | return Uncheck.apply(Files::readAllBytes, path); |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * Delegates to {@link Files#readAllLines(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. |
| 586 | * |
| 587 | * @param path See delegate. |
| 588 | * @return See delegate. |
| 589 | */ |
| 590 | public static List<String> readAllLines(final Path path) { |
| 591 | return Uncheck.apply(Files::readAllLines, path); |
| 592 | } |
| 593 | |
| 594 | /** |
| 595 | * Delegates to {@link Files#readAllLines(Path, Charset)} throwing {@link UncheckedIOException} instead of |
| 596 | * {@link IOException}. |
| 597 | * |
| 598 | * @param path See delegate. |
| 599 | * @param cs See delegate. |
| 600 | * @return See delegate. |
| 601 | */ |
| 602 | public static List<String> readAllLines(final Path path, final Charset cs) { |
| 603 | return Uncheck.apply(Files::readAllLines, path, cs); |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * Delegates to {@link Files#readAttributes(Path, Class, LinkOption...)} throwing {@link UncheckedIOException} instead |
| 608 | * of {@link IOException}. |
| 609 | * |
| 610 | * @param <A> See delegate. |
| 611 | * @param path See delegate. |
| 612 | * @param type See delegate. |
| 613 | * @param options See delegate. |
| 614 | * @return See delegate. |
| 615 | */ |
| 616 | public static <A extends BasicFileAttributes> A readAttributes(final Path path, final Class<A> type, final LinkOption... options) { |
| 617 | return Uncheck.apply(Files::readAttributes, path, type, options); |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * Delegates to {@link Files#readAttributes(Path, String, LinkOption...)} throwing {@link UncheckedIOException} instead |
| 622 | * of {@link IOException}. |
| 623 | * |
| 624 | * @param path See delegate. |
| 625 | * @param attributes See delegate. |
| 626 | * @param options See delegate. |
| 627 | * @return See delegate. |
| 628 | */ |
| 629 | public static Map<String, Object> readAttributes(final Path path, final String attributes, final LinkOption... options) { |
| 630 | return Uncheck.apply(Files::readAttributes, path, attributes, options); |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Delegates to {@link Files#readSymbolicLink(Path)} throwing {@link UncheckedIOException} instead of |
| 635 | * {@link IOException}. |
| 636 | * |
| 637 | * @param link See delegate. |
| 638 | * @return See delegate. |
| 639 | */ |
| 640 | public static Path readSymbolicLink(final Path link) { |
| 641 | return Uncheck.apply(Files::readSymbolicLink, link); |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * Delegates to {@link Files#setAttribute(Path, String, Object, LinkOption...)} throwing {@link UncheckedIOException} |
| 646 | * instead of {@link IOException}. |
| 647 | * |
| 648 | * @param path See delegate. |
| 649 | * @param attribute See delegate. |
| 650 | * @param value See delegate. |
| 651 | * @param options See delegate. |
| 652 | * @return See delegate. |
| 653 | */ |
| 654 | public static Path setAttribute(final Path path, final String attribute, final Object value, final LinkOption... options) { |
| 655 | return Uncheck.apply(Files::setAttribute, path, attribute, value, options); |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * Delegates to {@link Files#setLastModifiedTime(Path, FileTime)} throwing {@link UncheckedIOException} instead of |
| 660 | * {@link IOException}. |
| 661 | * |
| 662 | * @param path See delegate. |
| 663 | * @param time See delegate. |
| 664 | * @return See delegate. |
| 665 | */ |
| 666 | public static Path setLastModifiedTime(final Path path, final FileTime time) { |
| 667 | return Uncheck.apply(Files::setLastModifiedTime, path, time); |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * Delegates to {@link Files#setOwner(Path, UserPrincipal)} throwing {@link UncheckedIOException} instead of |
| 672 | * {@link IOException}. |
| 673 | * |
| 674 | * @param path See delegate. |
| 675 | * @param owner See delegate. |
| 676 | * @return See delegate. |
| 677 | */ |
| 678 | public static Path setOwner(final Path path, final UserPrincipal owner) { |
| 679 | return Uncheck.apply(Files::setOwner, path, owner); |
| 680 | } |
| 681 | |
| 682 | /** |
| 683 | * Delegates to {@link Files#setPosixFilePermissions(Path, Set)} throwing {@link UncheckedIOException} instead of |
| 684 | * {@link IOException}. |
| 685 | * |
| 686 | * @param path See delegate. |
| 687 | * @param perms See delegate. |
| 688 | * @return See delegate. |
| 689 | */ |
| 690 | public static Path setPosixFilePermissions(final Path path, final Set<PosixFilePermission> perms) { |
| 691 | return Uncheck.apply(Files::setPosixFilePermissions, path, perms); |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * Delegates to {@link Files#size(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. |
| 696 | * |
| 697 | * @param path See delegate. |
| 698 | * @return See delegate. |
| 699 | */ |
| 700 | public static long size(final Path path) { |
| 701 | return Uncheck.apply(Files::size, path); |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * Delegates to {@link Files#walk(Path, FileVisitOption...)} throwing {@link UncheckedIOException} instead of {@link IOException}. |
| 706 | * <p> |
| 707 | * The returned {@link Stream} may wrap one or more {@link DirectoryStream}s. When you require timely disposal of file system resources, use a |
| 708 | * {@code try}-with-resources block to ensure invocation of the stream's {@link Stream#close()} method after the stream operations are completed. Calling a |
| 709 | * closed stream causes a {@link IllegalStateException}. |
| 710 | * </p> |
| 711 | * |
| 712 | * @param start See delegate. |
| 713 | * @param options See delegate. |
| 714 | * @return See delegate. |
| 715 | */ |
| 716 | public static Stream<Path> walk(final Path start, final FileVisitOption... options) { |
| 717 | return Uncheck.apply(Files::walk, start, options); |
| 718 | } |
| 719 | |
| 720 | /** |
| 721 | * Delegates to {@link Files#walk(Path, int, FileVisitOption...)} throwing {@link UncheckedIOException} instead of {@link IOException}. |
| 722 | * <p> |
| 723 | * The returned {@link Stream} may wrap one or more {@link DirectoryStream}s. When you require timely disposal of file system resources, use a |
| 724 | * {@code try}-with-resources block to ensure invocation of the stream's {@link Stream#close()} method after the stream operations are completed. Calling a |
| 725 | * closed stream causes a {@link IllegalStateException}. |
| 726 | * </p> |
| 727 | * |
| 728 | * @param start See delegate. |
| 729 | * @param maxDepth See delegate. |
| 730 | * @param options See delegate. |
| 731 | * @return See delegate. |
| 732 | */ |
| 733 | public static Stream<Path> walk(final Path start, final int maxDepth, final FileVisitOption... options) { |
| 734 | return Uncheck.apply(Files::walk, start, maxDepth, options); |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * Delegates to {@link Files#walkFileTree(Path, FileVisitor)} throwing {@link UncheckedIOException} instead of |
| 739 | * {@link IOException}. |
| 740 | * |
| 741 | * @param start See delegate. |
| 742 | * @param visitor See delegate. |
| 743 | * @return See delegate. |
| 744 | */ |
| 745 | public static Path walkFileTree(final Path start, final FileVisitor<? super Path> visitor) { |
| 746 | return Uncheck.apply(Files::walkFileTree, start, visitor); |
| 747 | } |
| 748 | |
| 749 | /** |
| 750 | * Delegates to {@link Files#walkFileTree(Path, Set, int, FileVisitor)} throwing {@link UncheckedIOException} instead of |
| 751 | * {@link IOException}. |
| 752 | * |
| 753 | * @param start See delegate. |
| 754 | * @param options See delegate. |
| 755 | * @param maxDepth See delegate. |
| 756 | * @param visitor See delegate. |
| 757 | * @return See delegate. |
| 758 | */ |
| 759 | public static Path walkFileTree(final Path start, final Set<FileVisitOption> options, final int maxDepth, final FileVisitor<? super Path> visitor) { |
| 760 | return Uncheck.apply(Files::walkFileTree, start, options, maxDepth, visitor); |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * Delegates to {@link Files#write(Path, byte[], OpenOption...)} throwing {@link UncheckedIOException} instead of |
| 765 | * {@link IOException}. |
| 766 | * |
| 767 | * @param path See delegate. |
| 768 | * @param bytes See delegate. |
| 769 | * @param options See delegate. |
| 770 | * @return See delegate. |
| 771 | */ |
| 772 | public static Path write(final Path path, final byte[] bytes, final OpenOption... options) { |
| 773 | return Uncheck.apply(Files::write, path, bytes, options); |
| 774 | } |
| 775 | |
| 776 | /** |
| 777 | * Delegates to {@link Files#write(Path, Iterable, Charset, OpenOption...)} throwing {@link UncheckedIOException} |
| 778 | * instead of {@link IOException}. |
| 779 | * |
| 780 | * @param path See delegate. |
| 781 | * @param lines See delegate. |
| 782 | * @param cs See delegate. |
| 783 | * @param options See delegate. |
| 784 | * @return See delegate. |
| 785 | */ |
| 786 | public static Path write(final Path path, final Iterable<? extends CharSequence> lines, final Charset cs, final OpenOption... options) { |
| 787 | return Uncheck.apply(Files::write, path, lines, cs, options); |
| 788 | } |
| 789 | |
| 790 | /** |
| 791 | * Delegates to {@link Files#write(Path, Iterable, OpenOption...)} throwing {@link UncheckedIOException} instead of |
| 792 | * {@link IOException}. |
| 793 | * |
| 794 | * @param path See delegate. |
| 795 | * @param lines See delegate. |
| 796 | * @param options See delegate. |
| 797 | * @return See delegate. |
| 798 | */ |
| 799 | public static Path write(final Path path, final Iterable<? extends CharSequence> lines, final OpenOption... options) { |
| 800 | return Uncheck.apply(Files::write, path, lines, options); |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * No instances. |
| 805 | */ |
| 806 | private FilesUncheck() { |
| 807 | // No instances |
| 808 | } |
| 809 | } |