001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.io.file; 019 020import java.io.BufferedReader; 021import java.io.BufferedWriter; 022import java.io.IOException; 023import java.io.InputStream; 024import java.io.OutputStream; 025import java.io.UncheckedIOException; 026import java.nio.channels.SeekableByteChannel; 027import java.nio.charset.Charset; 028import java.nio.file.CopyOption; 029import java.nio.file.DirectoryStream; 030import java.nio.file.FileStore; 031import java.nio.file.FileVisitOption; 032import java.nio.file.FileVisitor; 033import java.nio.file.Files; 034import java.nio.file.LinkOption; 035import java.nio.file.OpenOption; 036import java.nio.file.Path; 037import java.nio.file.attribute.BasicFileAttributes; 038import java.nio.file.attribute.FileAttribute; 039import java.nio.file.attribute.FileTime; 040import java.nio.file.attribute.PosixFilePermission; 041import java.nio.file.attribute.UserPrincipal; 042import java.util.List; 043import java.util.Map; 044import java.util.Set; 045import java.util.function.BiPredicate; 046import java.util.stream.Stream; 047 048import org.apache.commons.io.function.Uncheck; 049 050/** 051 * Delegates to {@link Files} to uncheck calls by throwing {@link UncheckedIOException} instead of {@link IOException}. 052 * 053 * @see Files 054 * @see IOException 055 * @see UncheckedIOException 056 * @since 2.12.0 057 */ 058public final class FilesUncheck { 059 060 /** 061 * Delegates to {@link Files#copy(InputStream, Path, CopyOption...)} throwing {@link UncheckedIOException} instead of 062 * {@link IOException}. 063 * 064 * @param in See delegate. 065 * @param target See delegate. 066 * @param options See delegate. 067 * @return See delegate. 068 * @throws UncheckedIOException Wraps an {@link IOException}. 069 * @see Files#copy(InputStream, Path,CopyOption...) 070 */ 071 public static long copy(final InputStream in, final Path target, final CopyOption... options) { 072 return Uncheck.apply(Files::copy, in, target, options); 073 } 074 075 /** 076 * Delegates to {@link Files#copy(Path, OutputStream)} throwing {@link UncheckedIOException} instead of 077 * {@link IOException}. 078 * 079 * @param source See delegate. See delegate. 080 * @param out See delegate. See delegate. 081 * @return See delegate. See delegate. 082 * @throws UncheckedIOException Wraps an {@link IOException}. 083 */ 084 public static long copy(final Path source, final OutputStream out) { 085 return Uncheck.apply(Files::copy, source, out); 086 } 087 088 /** 089 * Delegates to {@link Files#copy(Path, Path, CopyOption...)} throwing {@link UncheckedIOException} instead of 090 * {@link IOException}. 091 * 092 * @param source See delegate. 093 * @param target See delegate. 094 * @param options See delegate. 095 * @return See delegate. 096 * @throws UncheckedIOException Wraps an {@link IOException}. 097 */ 098 public static Path copy(final Path source, final Path target, final CopyOption... options) { 099 return Uncheck.apply(Files::copy, source, target, options); 100 } 101 102 /** 103 * Delegates to {@link Files#createDirectories(Path, FileAttribute...)} throwing {@link UncheckedIOException} instead of 104 * {@link IOException}. 105 * 106 * @param dir See delegate. 107 * @param attrs See delegate. 108 * @return See delegate. 109 * @throws UncheckedIOException Wraps an {@link IOException}. 110 */ 111 public static Path createDirectories(final Path dir, final FileAttribute<?>... attrs) { 112 return Uncheck.apply(Files::createDirectories, dir, attrs); 113 } 114 115 /** 116 * Delegates to {@link Files#createDirectory(Path, FileAttribute...)} throwing {@link UncheckedIOException} instead of 117 * {@link IOException}. 118 * 119 * @param dir See delegate. 120 * @param attrs See delegate. 121 * @return See delegate. 122 * @throws UncheckedIOException Wraps an {@link IOException}. 123 */ 124 public static Path createDirectory(final Path dir, final FileAttribute<?>... attrs) { 125 return Uncheck.apply(Files::createDirectory, dir, attrs); 126 } 127 128 /** 129 * Delegates to {@link Files#createFile(Path, FileAttribute...)} throwing {@link UncheckedIOException} instead of 130 * {@link IOException}. 131 * 132 * @param path See delegate. 133 * @param attrs See delegate. 134 * @return See delegate. 135 * @throws UncheckedIOException Wraps an {@link IOException}. 136 */ 137 public static Path createFile(final Path path, final FileAttribute<?>... attrs) { 138 return Uncheck.apply(Files::createFile, path, attrs); 139 } 140 141 /** 142 * Delegates to {@link Files#createLink(Path, Path)} throwing {@link UncheckedIOException} instead of 143 * {@link IOException}. 144 * 145 * @param link See delegate. 146 * @param existing See delegate. 147 * @return See delegate. 148 * @throws UncheckedIOException Wraps an {@link IOException}. 149 */ 150 public static Path createLink(final Path link, final Path existing) { 151 return Uncheck.apply(Files::createLink, link, existing); 152 } 153 154 /** 155 * Delegates to {@link Files#createSymbolicLink(Path, Path, FileAttribute...)} throwing {@link UncheckedIOException} 156 * instead of {@link IOException}. 157 * 158 * @param link See delegate. 159 * @param target See delegate. 160 * @param attrs See delegate. 161 * @return See delegate. 162 * @throws UncheckedIOException Wraps an {@link IOException}. 163 */ 164 public static Path createSymbolicLink(final Path link, final Path target, final FileAttribute<?>... attrs) { 165 return Uncheck.apply(Files::createSymbolicLink, link, target, attrs); 166 } 167 168 /** 169 * Delegates to {@link Files#createTempDirectory(Path, String, FileAttribute...)} throwing {@link UncheckedIOException} 170 * instead of {@link IOException}. 171 * 172 * @param dir See delegate. 173 * @param prefix See delegate. 174 * @param attrs See delegate. 175 * @return See delegate. 176 * @throws UncheckedIOException Wraps an {@link IOException}. 177 */ 178 public static Path createTempDirectory(final Path dir, final String prefix, final FileAttribute<?>... attrs) { 179 return Uncheck.apply(Files::createTempDirectory, dir, prefix, attrs); 180 } 181 182 /** 183 * Delegates to {@link Files#createTempDirectory(String, FileAttribute...)} throwing {@link UncheckedIOException} 184 * instead of {@link IOException}. 185 * 186 * @param prefix See delegate. 187 * @param attrs See delegate. 188 * @return See delegate. 189 * @throws UncheckedIOException Wraps an {@link IOException}. 190 */ 191 public static Path createTempDirectory(final String prefix, final FileAttribute<?>... attrs) { 192 return Uncheck.apply(Files::createTempDirectory, prefix, attrs); 193 } 194 195 /** 196 * Delegates to {@link Files#createTempFile(Path, String, String, FileAttribute...)} throwing 197 * {@link UncheckedIOException} instead of {@link IOException}. 198 * 199 * @param dir See delegate. 200 * @param prefix See delegate. 201 * @param suffix See delegate. 202 * @param attrs See delegate. 203 * @return See delegate. 204 * @throws UncheckedIOException Wraps an {@link IOException}. 205 */ 206 public static Path createTempFile(final Path dir, final String prefix, final String suffix, final FileAttribute<?>... attrs) { 207 return Uncheck.apply(Files::createTempFile, dir, prefix, suffix, attrs); 208 } 209 210 /** 211 * Delegates to {@link Files#createTempFile(String, String, FileAttribute...)} throwing {@link UncheckedIOException} 212 * instead of {@link IOException}. 213 * 214 * @param prefix See delegate. 215 * @param suffix See delegate. 216 * @param attrs See delegate. 217 * @return See delegate. 218 * @throws UncheckedIOException Wraps an {@link IOException}. 219 */ 220 public static Path createTempFile(final String prefix, final String suffix, final FileAttribute<?>... attrs) { 221 return Uncheck.apply(Files::createTempFile, prefix, suffix, attrs); 222 } 223 224 /** 225 * Delegates to {@link Files#delete(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. 226 * 227 * @param path See delegate. 228 * @throws UncheckedIOException Wraps an {@link IOException}. 229 */ 230 public static void delete(final Path path) { 231 Uncheck.accept(Files::delete, path); 232 } 233 234 /** 235 * Delegates to {@link Files#deleteIfExists(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. 236 * 237 * @param path See delegate. 238 * @return See delegate. 239 * @throws UncheckedIOException Wraps an {@link IOException}. 240 */ 241 public static boolean deleteIfExists(final Path path) { 242 return Uncheck.apply(Files::deleteIfExists, path); 243 } 244 245 /** 246 * Delegates to {@link Files#find(Path, int, BiPredicate, FileVisitOption...)} throwing {@link UncheckedIOException} instead of {@link IOException}. 247 * 248 * @param start See delegate. 249 * @param maxDepth See delegate. 250 * @param matcher See delegate. 251 * @param options See delegate. 252 * @return See delegate. 253 * @throws UncheckedIOException Wraps an {@link IOException}. 254 * @since 2.14.0 255 */ 256 public static Stream<Path> find(final Path start, final int maxDepth, final BiPredicate<Path, BasicFileAttributes> matcher, 257 final FileVisitOption... options) { 258 return Uncheck.apply(Files::find, start, maxDepth, matcher, options); 259 } 260 261 /** 262 * Delegates to {@link Files#getAttribute(Path, String, LinkOption...)} throwing {@link UncheckedIOException} instead of 263 * {@link IOException}. 264 * 265 * @param path See delegate. 266 * @param attribute See delegate. 267 * @param options See delegate. 268 * @return See delegate. 269 * @throws UncheckedIOException Wraps an {@link IOException}. 270 */ 271 public static Object getAttribute(final Path path, final String attribute, final LinkOption... options) { 272 return Uncheck.apply(Files::getAttribute, path, attribute, options); 273 } 274 275 /** 276 * Delegates to {@link Files#getFileStore(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. 277 * 278 * @param path See delegate. 279 * @return See delegate. 280 * @throws UncheckedIOException Wraps an {@link IOException}. 281 */ 282 public static FileStore getFileStore(final Path path) { 283 return Uncheck.apply(Files::getFileStore, path); 284 } 285 286 /** 287 * Delegates to {@link Files#getLastModifiedTime(Path, LinkOption...)} throwing {@link UncheckedIOException} instead of 288 * {@link IOException}. 289 * 290 * @param path See delegate. 291 * @param options See delegate. 292 * @return See delegate. 293 * @throws UncheckedIOException Wraps an {@link IOException}. 294 */ 295 public static FileTime getLastModifiedTime(final Path path, final LinkOption... options) { 296 return Uncheck.apply(Files::getLastModifiedTime, path, options); 297 } 298 299 /** 300 * Delegates to {@link Files#getOwner(Path, LinkOption...)} throwing {@link UncheckedIOException} instead of 301 * {@link IOException}. 302 * 303 * @param path See delegate. 304 * @param options See delegate. 305 * @return See delegate. 306 * @throws UncheckedIOException Wraps an {@link IOException}. 307 */ 308 public static UserPrincipal getOwner(final Path path, final LinkOption... options) { 309 return Uncheck.apply(Files::getOwner, path, options); 310 } 311 312 /** 313 * Delegates to {@link Files#getPosixFilePermissions(Path, LinkOption...)} throwing {@link UncheckedIOException} instead 314 * of {@link IOException}. 315 * 316 * @param path See delegate. 317 * @param options See delegate. 318 * @return See delegate. 319 * @throws UncheckedIOException Wraps an {@link IOException}. 320 */ 321 public static Set<PosixFilePermission> getPosixFilePermissions(final Path path, final LinkOption... options) { 322 return Uncheck.apply(Files::getPosixFilePermissions, path, options); 323 } 324 325 /** 326 * Delegates to {@link Files#isHidden(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. 327 * 328 * @param path See delegate. 329 * @return See delegate. 330 * @throws UncheckedIOException Wraps an {@link IOException}. 331 */ 332 public static boolean isHidden(final Path path) { 333 return Uncheck.apply(Files::isHidden, path); 334 } 335 336 /** 337 * Delegates to {@link Files#isSameFile(Path, Path)} throwing {@link UncheckedIOException} instead of 338 * {@link IOException}. 339 * 340 * @param path See delegate. 341 * @param path2 See delegate. 342 * @return See delegate. 343 * @throws UncheckedIOException Wraps an {@link IOException}. 344 */ 345 public static boolean isSameFile(final Path path, final Path path2) { 346 return Uncheck.apply(Files::isSameFile, path, path2); 347 } 348 349 /** 350 * Delegates to {@link Files#lines(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. 351 * 352 * @param path See delegate. 353 * @return See delegate. 354 * @throws UncheckedIOException Wraps an {@link IOException}. 355 */ 356 public static Stream<String> lines(final Path path) { 357 return Uncheck.apply(Files::lines, path); 358 } 359 360 /** 361 * Delegates to {@link Files#lines(Path, Charset)} throwing {@link UncheckedIOException} instead of {@link IOException}. 362 * 363 * @param path See delegate. 364 * @param cs See delegate. 365 * @return See delegate. 366 * @throws UncheckedIOException Wraps an {@link IOException}. 367 */ 368 public static Stream<String> lines(final Path path, final Charset cs) { 369 return Uncheck.apply(Files::lines, path, cs); 370 } 371 372 /** 373 * Delegates to {@link Files#list(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. 374 * 375 * @param dir See delegate. 376 * @return See delegate. 377 * @throws UncheckedIOException Wraps an {@link IOException}. 378 */ 379 public static Stream<Path> list(final Path dir) { 380 return Uncheck.apply(Files::list, dir); 381 } 382 383 /** 384 * Delegates to {@link Files#move(Path, Path, CopyOption...)} throwing {@link UncheckedIOException} instead of 385 * {@link IOException}. 386 * 387 * @param source See delegate. 388 * @param target See delegate. 389 * @param options See delegate. 390 * @return See delegate. 391 * @throws UncheckedIOException Wraps an {@link IOException}. 392 */ 393 public static Path move(final Path source, final Path target, final CopyOption... options) { 394 return Uncheck.apply(Files::move, source, target, options); 395 } 396 397 /** 398 * Delegates to {@link Files#newBufferedReader(Path)} throwing {@link UncheckedIOException} instead of 399 * {@link IOException}. 400 * 401 * @param path See delegate. 402 * @return See delegate. 403 * @throws UncheckedIOException Wraps an {@link IOException}. 404 */ 405 public static BufferedReader newBufferedReader(final Path path) { 406 return Uncheck.apply(Files::newBufferedReader, path); 407 } 408 409 /** 410 * Delegates to {@link Files#newBufferedReader(Path, Charset)} throwing {@link UncheckedIOException} instead of 411 * {@link IOException}. 412 * 413 * @param path See delegate. 414 * @param cs See delegate. 415 * @return See delegate. 416 * @throws UncheckedIOException Wraps an {@link IOException}. 417 */ 418 public static BufferedReader newBufferedReader(final Path path, final Charset cs) { 419 return Uncheck.apply(Files::newBufferedReader, path, cs); 420 } 421 422 /** 423 * Delegates to {@link Files#newBufferedWriter(Path, Charset, OpenOption...)} throwing {@link UncheckedIOException} 424 * instead of {@link IOException}. 425 * 426 * @param path See delegate. 427 * @param cs See delegate. 428 * @param options See delegate. 429 * @return See delegate. 430 * @throws UncheckedIOException Wraps an {@link IOException}. 431 */ 432 public static BufferedWriter newBufferedWriter(final Path path, final Charset cs, final OpenOption... options) { 433 return Uncheck.apply(Files::newBufferedWriter, path, cs, options); 434 } 435 436 /** 437 * Delegates to {@link Files#newBufferedWriter(Path, OpenOption...)} throwing {@link UncheckedIOException} instead of 438 * {@link IOException}. 439 * 440 * @param path See delegate. 441 * @param options See delegate. 442 * @return See delegate. 443 * @throws UncheckedIOException Wraps an {@link IOException}. 444 */ 445 public static BufferedWriter newBufferedWriter(final Path path, final OpenOption... options) { 446 return Uncheck.apply(Files::newBufferedWriter, path, options); 447 } 448 449 /** 450 * Delegates to {@link Files#newByteChannel(Path, OpenOption...)} throwing {@link UncheckedIOException} instead of 451 * {@link IOException}. 452 * 453 * @param path See delegate. 454 * @param options See delegate. 455 * @return See delegate. 456 * @throws UncheckedIOException Wraps an {@link IOException}. 457 */ 458 public static SeekableByteChannel newByteChannel(final Path path, final OpenOption... options) { 459 return Uncheck.apply(Files::newByteChannel, path, options); 460 } 461 462 /** 463 * Delegates to {@link Files#newByteChannel(Path, Set, FileAttribute...)} throwing {@link UncheckedIOException} instead 464 * of {@link IOException}. 465 * 466 * @param path See delegate. 467 * @param options See delegate. 468 * @param attrs See delegate. 469 * @return See delegate. 470 * @throws UncheckedIOException Wraps an {@link IOException}. 471 */ 472 public static SeekableByteChannel newByteChannel(final Path path, final Set<? extends OpenOption> options, final FileAttribute<?>... attrs) { 473 return Uncheck.apply(Files::newByteChannel, path, options, attrs); 474 } 475 476 /** 477 * Delegates to {@link Files#newDirectoryStream(Path)} throwing {@link UncheckedIOException} instead of 478 * {@link IOException}. 479 * 480 * @param dir See delegate. 481 * @return See delegate. 482 */ 483 public static DirectoryStream<Path> newDirectoryStream(final Path dir) { 484 return Uncheck.apply(Files::newDirectoryStream, dir); 485 } 486 487 /** 488 * Delegates to {@link Files#newDirectoryStream(Path, java.nio.file.DirectoryStream.Filter)} throwing 489 * {@link UncheckedIOException} instead of {@link IOException}. 490 * 491 * @param dir See delegate. 492 * @param filter See delegate. 493 * @return See delegate. 494 */ 495 public static DirectoryStream<Path> newDirectoryStream(final Path dir, final DirectoryStream.Filter<? super Path> filter) { 496 return Uncheck.apply(Files::newDirectoryStream, dir, filter); 497 } 498 499 /** 500 * Delegates to {@link Files#newDirectoryStream(Path, String)} throwing {@link UncheckedIOException} instead of 501 * {@link IOException}. 502 * 503 * @param dir See delegate. 504 * @param glob See delegate. 505 * @return See delegate. 506 */ 507 public static DirectoryStream<Path> newDirectoryStream(final Path dir, final String glob) { 508 return Uncheck.apply(Files::newDirectoryStream, dir, glob); 509 } 510 511 /** 512 * Delegates to {@link Files#newInputStream(Path, OpenOption...)} throwing {@link UncheckedIOException} instead of 513 * {@link IOException}. 514 * 515 * @param path See delegate. 516 * @param options See delegate. 517 * @return See delegate. 518 */ 519 public static InputStream newInputStream(final Path path, final OpenOption... options) { 520 return Uncheck.apply(Files::newInputStream, path, options); 521 } 522 523 /** 524 * Delegates to {@link Files#newOutputStream(Path, OpenOption...)} throwing {@link UncheckedIOException} instead of 525 * {@link IOException}. 526 * 527 * @param path See delegate. 528 * @param options See delegate. 529 * @return See delegate. 530 */ 531 public static OutputStream newOutputStream(final Path path, final OpenOption... options) { 532 return Uncheck.apply(Files::newOutputStream, path, options); 533 } 534 535 /** 536 * Delegates to {@link Files#probeContentType(Path)} throwing {@link UncheckedIOException} instead of 537 * {@link IOException}. 538 * 539 * @param path See delegate. 540 * @return See delegate. 541 */ 542 public static String probeContentType(final Path path) { 543 return Uncheck.apply(Files::probeContentType, path); 544 } 545 546 /** 547 * Delegates to {@link Files#readAllBytes(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. 548 * 549 * @param path See delegate. 550 * @return See delegate. 551 */ 552 public static byte[] readAllBytes(final Path path) { 553 return Uncheck.apply(Files::readAllBytes, path); 554 } 555 556 /** 557 * Delegates to {@link Files#readAllLines(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. 558 * 559 * @param path See delegate. 560 * @return See delegate. 561 */ 562 public static List<String> readAllLines(final Path path) { 563 return Uncheck.apply(Files::readAllLines, path); 564 } 565 566 /** 567 * Delegates to {@link Files#readAllLines(Path, Charset)} throwing {@link UncheckedIOException} instead of 568 * {@link IOException}. 569 * 570 * @param path See delegate. 571 * @param cs See delegate. 572 * @return See delegate. 573 */ 574 public static List<String> readAllLines(final Path path, final Charset cs) { 575 return Uncheck.apply(Files::readAllLines, path, cs); 576 } 577 578 /** 579 * Delegates to {@link Files#readAttributes(Path, Class, LinkOption...)} throwing {@link UncheckedIOException} instead 580 * of {@link IOException}. 581 * 582 * @param <A> See delegate. 583 * @param path See delegate. 584 * @param type See delegate. 585 * @param options See delegate. 586 * @return See delegate. 587 */ 588 public static <A extends BasicFileAttributes> A readAttributes(final Path path, final Class<A> type, final LinkOption... options) { 589 return Uncheck.apply(Files::readAttributes, path, type, options); 590 } 591 592 /** 593 * Delegates to {@link Files#readAttributes(Path, String, LinkOption...)} throwing {@link UncheckedIOException} instead 594 * of {@link IOException}. 595 * 596 * @param path See delegate. 597 * @param attributes See delegate. 598 * @param options See delegate. 599 * @return See delegate. 600 */ 601 public static Map<String, Object> readAttributes(final Path path, final String attributes, final LinkOption... options) { 602 return Uncheck.apply(Files::readAttributes, path, attributes, options); 603 } 604 605 /** 606 * Delegates to {@link Files#readSymbolicLink(Path)} throwing {@link UncheckedIOException} instead of 607 * {@link IOException}. 608 * 609 * @param link See delegate. 610 * @return See delegate. 611 */ 612 public static Path readSymbolicLink(final Path link) { 613 return Uncheck.apply(Files::readSymbolicLink, link); 614 } 615 616 /** 617 * Delegates to {@link Files#setAttribute(Path, String, Object, LinkOption...)} throwing {@link UncheckedIOException} 618 * instead of {@link IOException}. 619 * 620 * @param path See delegate. 621 * @param attribute See delegate. 622 * @param value See delegate. 623 * @param options See delegate. 624 * @return See delegate. 625 */ 626 public static Path setAttribute(final Path path, final String attribute, final Object value, final LinkOption... options) { 627 return Uncheck.apply(Files::setAttribute, path, attribute, value, options); 628 } 629 630 /** 631 * Delegates to {@link Files#setLastModifiedTime(Path, FileTime)} throwing {@link UncheckedIOException} instead of 632 * {@link IOException}. 633 * 634 * @param path See delegate. 635 * @param time See delegate. 636 * @return See delegate. 637 */ 638 public static Path setLastModifiedTime(final Path path, final FileTime time) { 639 return Uncheck.apply(Files::setLastModifiedTime, path, time); 640 } 641 642 /** 643 * Delegates to {@link Files#setOwner(Path, UserPrincipal)} throwing {@link UncheckedIOException} instead of 644 * {@link IOException}. 645 * 646 * @param path See delegate. 647 * @param owner See delegate. 648 * @return See delegate. 649 */ 650 public static Path setOwner(final Path path, final UserPrincipal owner) { 651 return Uncheck.apply(Files::setOwner, path, owner); 652 } 653 654 /** 655 * Delegates to {@link Files#setPosixFilePermissions(Path, Set)} throwing {@link UncheckedIOException} instead of 656 * {@link IOException}. 657 * 658 * @param path See delegate. 659 * @param perms See delegate. 660 * @return See delegate. 661 */ 662 public static Path setPosixFilePermissions(final Path path, final Set<PosixFilePermission> perms) { 663 return Uncheck.apply(Files::setPosixFilePermissions, path, perms); 664 } 665 666 /** 667 * Delegates to {@link Files#size(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. 668 * 669 * @param path See delegate. 670 * @return See delegate. 671 */ 672 public static long size(final Path path) { 673 return Uncheck.apply(Files::size, path); 674 } 675 676 /** 677 * Delegates to {@link Files#walk(Path, FileVisitOption...)} throwing {@link UncheckedIOException} instead of 678 * {@link IOException}. 679 * 680 * @param start See delegate. 681 * @param options See delegate. 682 * @return See delegate. 683 */ 684 public static Stream<Path> walk(final Path start, final FileVisitOption... options) { 685 return Uncheck.apply(Files::walk, start, options); 686 } 687 688 /** 689 * Delegates to {@link Files#walk(Path, int, FileVisitOption...)} throwing {@link UncheckedIOException} instead of 690 * {@link IOException}. 691 * 692 * @param start See delegate. 693 * @param maxDepth See delegate. 694 * @param options See delegate. 695 * @return See delegate. 696 */ 697 public static Stream<Path> walk(final Path start, final int maxDepth, final FileVisitOption... options) { 698 return Uncheck.apply(Files::walk, start, maxDepth, options); 699 } 700 701 /** 702 * Delegates to {@link Files#walkFileTree(Path, FileVisitor)} throwing {@link UncheckedIOException} instead of 703 * {@link IOException}. 704 * 705 * @param start See delegate. 706 * @param visitor See delegate. 707 * @return See delegate. 708 */ 709 public static Path walkFileTree(final Path start, final FileVisitor<? super Path> visitor) { 710 return Uncheck.apply(Files::walkFileTree, start, visitor); 711 } 712 713 /** 714 * Delegates to {@link Files#walkFileTree(Path, Set, int, FileVisitor)} throwing {@link UncheckedIOException} instead of 715 * {@link IOException}. 716 * 717 * @param start See delegate. 718 * @param options See delegate. 719 * @param maxDepth See delegate. 720 * @param visitor See delegate. 721 * @return See delegate. 722 */ 723 public static Path walkFileTree(final Path start, final Set<FileVisitOption> options, final int maxDepth, final FileVisitor<? super Path> visitor) { 724 return Uncheck.apply(Files::walkFileTree, start, options, maxDepth, visitor); 725 } 726 727 /** 728 * Delegates to {@link Files#write(Path, byte[], OpenOption...)} throwing {@link UncheckedIOException} instead of 729 * {@link IOException}. 730 * 731 * @param path See delegate. 732 * @param bytes See delegate. 733 * @param options See delegate. 734 * @return See delegate. 735 */ 736 public static Path write(final Path path, final byte[] bytes, final OpenOption... options) { 737 return Uncheck.apply(Files::write, path, bytes, options); 738 } 739 740 /** 741 * Delegates to {@link Files#write(Path, Iterable, Charset, OpenOption...)} throwing {@link UncheckedIOException} 742 * instead of {@link IOException}. 743 * 744 * @param path See delegate. 745 * @param lines See delegate. 746 * @param cs See delegate. 747 * @param options See delegate. 748 * @return See delegate. 749 */ 750 public static Path write(final Path path, final Iterable<? extends CharSequence> lines, final Charset cs, final OpenOption... options) { 751 return Uncheck.apply(Files::write, path, lines, cs, options); 752 } 753 754 /** 755 * Delegates to {@link Files#write(Path, Iterable, OpenOption...)} throwing {@link UncheckedIOException} instead of 756 * {@link IOException}. 757 * 758 * @param path See delegate. 759 * @param lines See delegate. 760 * @param options See delegate. 761 * @return See delegate. 762 */ 763 public static Path write(final Path path, final Iterable<? extends CharSequence> lines, final OpenOption... options) { 764 return Uncheck.apply(Files::write, path, lines, options); 765 } 766 767 /** 768 * No instances. 769 */ 770 private FilesUncheck() { 771 // No instances 772 } 773}