This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
commit 3ec3315571f70f1d371b958fe1308a0b316961e6 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Sun Jul 24 12:08:12 2022 -0400 Add FilesUncheck --- src/changes/changes.xml | 2 +- .../org/apache/commons/io/file/FilesUncheck.java | 756 +++++++++++++++++++++ .../apache/commons/io/file/FilesUncheckTest.java | 445 ++++++++++++ 3 files changed, 1202 insertions(+), 1 deletion(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index cb0b734f..21414100 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -410,7 +410,7 @@ The <action> type attribute can be add,update,fix,remove. Add PathUtils.getLastModifiedFileTime(*). </action> <action dev="ggregory" type="add" due-to="Gary Gregory"> - Add IOBiFunction, IOTriFunction, IOQuadFunction, IOPredicate. + Add IOBiFunction, IOTriFunction, IOQuadFunction, IOPredicate, FilesUncheck. </action> <action dev="ggregory" type="add" due-to="Gary Gregory"> Add IOUtils.consume(Reader). diff --git a/src/main/java/org/apache/commons/io/file/FilesUncheck.java b/src/main/java/org/apache/commons/io/file/FilesUncheck.java new file mode 100644 index 00000000..06de080b --- /dev/null +++ b/src/main/java/org/apache/commons/io/file/FilesUncheck.java @@ -0,0 +1,756 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.io.file; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.UncheckedIOException; +import java.nio.channels.SeekableByteChannel; +import java.nio.charset.Charset; +import java.nio.file.CopyOption; +import java.nio.file.DirectoryStream; +import java.nio.file.FileStore; +import java.nio.file.FileVisitOption; +import java.nio.file.FileVisitor; +import java.nio.file.Files; +import java.nio.file.LinkOption; +import java.nio.file.OpenOption; +import java.nio.file.Path; +import java.nio.file.attribute.BasicFileAttributes; +import java.nio.file.attribute.FileAttribute; +import java.nio.file.attribute.FileTime; +import java.nio.file.attribute.PosixFilePermission; +import java.nio.file.attribute.UserPrincipal; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Stream; + +import org.apache.commons.io.UncheckedIO; + +/** + * Delegates to {@link Files} to uncheck calls by throwing {@link UncheckedIOException} instead of {@link IOException}. + * + * @see Files + * @see IOException + * @see UncheckedIOException + * @since 2.12.0 + */ +public class FilesUncheck { + + /** + * Delegates to {@link Files#copy(InputStream, Path,CopyOption...)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param in See delegate. See delegate. + * @param target See delegate. See delegate. + * @param options See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + * @see Files#copy(InputStream, Path,CopyOption...) + */ + public static long copy(final InputStream in, final Path target, final CopyOption... options) { + return UncheckedIO.apply(Files::copy, in, target, options); + } + + /** + * Delegates to {@link Files#copy(Path, OutputStream)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param source See delegate. See delegate. See delegate. + * @param out See delegate. See delegate. See delegate. + * @return See delegate. See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static long copy(final Path source, final OutputStream out) { + return UncheckedIO.apply(Files::copy, source, out); + } + + /** + * Delegates to {@link Files#copy(Path, Path, CopyOption...)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param source See delegate. See delegate. + * @param target See delegate. See delegate. + * @param options See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static Path copy(final Path source, final Path target, final CopyOption... options) { + return UncheckedIO.apply(Files::copy, source, target, options); + } + + /** + * Delegates to {@link Files#createDirectories(Path, FileAttribute...)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param dir See delegate. See delegate. + * @param attrs See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static Path createDirectories(final Path dir, final FileAttribute<?>... attrs) { + return UncheckedIO.apply(Files::createDirectories, dir, attrs); + } + + /** + * Delegates to {@link Files#createDirectory(Path, FileAttribute...)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param dir See delegate. See delegate. + * @param attrs See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static Path createDirectory(final Path dir, final FileAttribute<?>... attrs) { + return UncheckedIO.apply(Files::createDirectory, dir, attrs); + } + + /** + * Delegates to {@link Files#createFile(Path, FileAttribute...)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param path See delegate. See delegate. + * @param attrs See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static Path createFile(final Path path, final FileAttribute<?>... attrs) { + return UncheckedIO.apply(Files::createFile, path, attrs); + } + + /** + * Delegates to {@link Files#createLink(Path, Path)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param link See delegate. See delegate. + * @param existing See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static Path createLink(final Path link, final Path existing) { + return UncheckedIO.apply(Files::createLink, link, existing); + } + + /** + * Delegates to {@link Files#createSymbolicLink(Path, Path, FileAttribute...)} throwing {@link UncheckedIOException} + * instead of {@link IOException}. + * + * @param link See delegate. See delegate. + * @param target See delegate. See delegate. + * @param attrs See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static Path createSymbolicLink(final Path link, final Path target, final FileAttribute<?>... attrs) { + return UncheckedIO.apply(Files::createSymbolicLink, link, target, attrs); + } + + /** + * Delegates to {@link Files#createTempDirectory(Path, String, FileAttribute...)} throwing {@link UncheckedIOException} + * instead of {@link IOException}. + * + * @param dir See delegate. See delegate. + * @param prefix See delegate. See delegate. + * @param attrs See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static Path createTempDirectory(final Path dir, final String prefix, final FileAttribute<?>... attrs) { + return UncheckedIO.apply(Files::createTempDirectory, dir, prefix, attrs); + } + + /** + * Delegates to {@link Files#createTempDirectory(String, FileAttribute...)} throwing {@link UncheckedIOException} + * instead of {@link IOException}. + * + * @param prefix See delegate. See delegate. + * @param attrs See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static Path createTempDirectory(final String prefix, final FileAttribute<?>... attrs) { + return UncheckedIO.apply(Files::createTempDirectory, prefix, attrs); + } + + /** + * Delegates to {@link Files#createTempFile(Path, String, String, FileAttribute...)} throwing + * {@link UncheckedIOException} instead of {@link IOException}. + * + * @param dir See delegate. See delegate. + * @param prefix See delegate. See delegate. + * @param suffix See delegate. See delegate. + * @param attrs See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static Path createTempFile(final Path dir, final String prefix, final String suffix, final FileAttribute<?>... attrs) { + return UncheckedIO.apply(Files::createTempFile, dir, prefix, suffix, attrs); + } + + /** + * Delegates to {@link Files#createTempFile(String, String, FileAttribute...)} throwing {@link UncheckedIOException} + * instead of {@link IOException}. + * + * @param prefix See delegate. See delegate. + * @param suffix See delegate. See delegate. + * @param attrs See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static Path createTempFile(final String prefix, final String suffix, final FileAttribute<?>... attrs) { + return UncheckedIO.apply(Files::createTempFile, prefix, suffix, attrs); + } + + /** + * Delegates to {@link Files#delete(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. + * + * @param path See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static void delete(final Path path) { + UncheckedIO.accept(Files::delete, path); + } + + /** + * Delegates to {@link Files#deleteIfExists(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. + * + * @param path See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static boolean deleteIfExists(final Path path) { + return UncheckedIO.apply(Files::deleteIfExists, path); + } + + /** + * Delegates to {@link Files#getAttribute(Path, String, LinkOption...)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param path See delegate. See delegate. + * @param attribute See delegate. See delegate. + * @param options See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static Object getAttribute(final Path path, final String attribute, final LinkOption... options) { + return UncheckedIO.apply(Files::getAttribute, path, attribute, options); + } + + /** + * Delegates to {@link Files#getFileStore(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. + * + * @param path See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static FileStore getFileStore(final Path path) { + return UncheckedIO.apply(Files::getFileStore, path); + } + + /** + * Delegates to {@link Files#getLastModifiedTime(Path, LinkOption...)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param path See delegate. See delegate. + * @param options See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static FileTime getLastModifiedTime(final Path path, final LinkOption... options) { + return UncheckedIO.apply(Files::getLastModifiedTime, path, options); + } + + /** + * Delegates to {@link Files#getOwner(Path, LinkOption...)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param path See delegate. See delegate. + * @param options See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static UserPrincipal getOwner(final Path path, final LinkOption... options) { + return UncheckedIO.apply(Files::getOwner, path, options); + } + + /** + * Delegates to {@link Files#getPosixFilePermissions(Path, LinkOption...)} throwing {@link UncheckedIOException} instead + * of {@link IOException}. + * + * @param path See delegate. See delegate. + * @param options See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static Set<PosixFilePermission> getPosixFilePermissions(final Path path, final LinkOption... options) { + return UncheckedIO.apply(Files::getPosixFilePermissions, path, options); + } + + /** + * Delegates to {@link Files#isHidden(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. + * + * @param path See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static boolean isHidden(final Path path) { + return UncheckedIO.apply(Files::isHidden, path); + } + + /** + * Delegates to {@link Files#isSameFile(Path, Path)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param path See delegate. See delegate. + * @param path2 See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static boolean isSameFile(final Path path, final Path path2) { + return UncheckedIO.apply(Files::isSameFile, path, path2); + } + + /** + * Delegates to {@link Files#lines(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. + * + * @param path See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static Stream<String> lines(final Path path) { + return UncheckedIO.apply(Files::lines, path); + } + + /** + * Delegates to {@link Files#lines(Path, Charset)} throwing {@link UncheckedIOException} instead of {@link IOException}. + * + * @param path See delegate. See delegate. + * @param cs See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static Stream<String> lines(final Path path, final Charset cs) { + return UncheckedIO.apply(Files::lines, path, cs); + } + + /** + * Delegates to {@link Files#list(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. + * + * @param dir See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static Stream<Path> list(final Path dir) { + return UncheckedIO.apply(Files::list, dir); + } + + /** + * Delegates to {@link Files#move(Path, Path, CopyOption...)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param source See delegate. See delegate. + * @param target See delegate. See delegate. + * @param options See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static Path move(final Path source, final Path target, final CopyOption... options) { + return UncheckedIO.apply(Files::move, source, target, options); + } + + /** + * Delegates to {@link Files#newBufferedReader(Path)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param path See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static BufferedReader newBufferedReader(final Path path) { + return UncheckedIO.apply(Files::newBufferedReader, path); + } + + /** + * Delegates to {@link Files#newBufferedReader(Path, Charset)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param path See delegate. See delegate. + * @param cs See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static BufferedReader newBufferedReader(final Path path, final Charset cs) { + return UncheckedIO.apply(Files::newBufferedReader, path, cs); + } + + /** + * Delegates to {@link Files#newBufferedWriter(Path, Charset, OpenOption...)} throwing {@link UncheckedIOException} + * instead of {@link IOException}. + * + * @param path See delegate. See delegate. + * @param cs See delegate. See delegate. + * @param options See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static BufferedWriter newBufferedWriter(final Path path, final Charset cs, final OpenOption... options) { + return UncheckedIO.apply(Files::newBufferedWriter, path, cs, options); + } + + /** + * Delegates to {@link Files#newBufferedWriter(Path, OpenOption...)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param path See delegate. See delegate. + * @param options See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static BufferedWriter newBufferedWriter(final Path path, final OpenOption... options) { + return UncheckedIO.apply(Files::newBufferedWriter, path, options); + } + + /** + * Delegates to {@link Files#newByteChannel(Path, OpenOption...)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param path See delegate. See delegate. + * @param options See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static SeekableByteChannel newByteChannel(final Path path, final OpenOption... options) { + return UncheckedIO.apply(Files::newByteChannel, path, options); + } + + /** + * Delegates to {@link Files#newByteChannel(Path, Set, FileAttribute...)} throwing {@link UncheckedIOException} instead + * of {@link IOException}. + * + * @param path See delegate. See delegate. + * @param options See delegate. See delegate. + * @param attrs See delegate. See delegate. + * @return See delegate. See delegate. + * @throws UncheckedIOException Wraps an {@link IOException}. + */ + public static SeekableByteChannel newByteChannel(final Path path, final Set<? extends OpenOption> options, final FileAttribute<?>... attrs) { + return UncheckedIO.apply(Files::newByteChannel, path, options, attrs); + } + + /** + * Delegates to {@link Files#newDirectoryStream(Path)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param dir See delegate. See delegate. + * @return See delegate. See delegate. + */ + public static DirectoryStream<Path> newDirectoryStream(final Path dir) { + return UncheckedIO.apply(Files::newDirectoryStream, dir); + } + + /** + * Delegates to {@link Files#newDirectoryStream(Path, String)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param dir See delegate. + * @param glob See delegate. + * @return See delegate. + */ + public static DirectoryStream<Path> newDirectoryStream(final Path dir, final String glob) { + return UncheckedIO.apply(Files::newDirectoryStream, dir, glob); + } + + /** + * Delegates to {@link Files#newDirectoryStream(Path, java.nio.file.DirectoryStream.Filter)} throwing + * {@link UncheckedIOException} instead of {@link IOException}. + * + * @param dir See delegate. + * @param filter See delegate. + * @return See delegate. + */ + public static DirectoryStream<Path> newDirectoryStream(final Path dir, final DirectoryStream.Filter<? super Path> filter) { + return UncheckedIO.apply(Files::newDirectoryStream, dir, filter); + } + + /** + * Delegates to {@link Files#newInputStream(Path, OpenOption...)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param path See delegate. + * @param options See delegate. + * @return See delegate. + */ + public static InputStream newInputStream(final Path path, final OpenOption... options) { + return UncheckedIO.apply(Files::newInputStream, path, options); + } + + /** + * Delegates to {@link Files#newOutputStream(Path, OpenOption...)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param path See delegate. + * @param options See delegate. + * @return See delegate. + */ + public static OutputStream newOutputStream(final Path path, final OpenOption... options) { + return UncheckedIO.apply(Files::newOutputStream, path, options); + } + + /** + * Delegates to {@link Files#probeContentType(Path)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param path See delegate. + * @return See delegate. + */ + public static String probeContentType(final Path path) { + return UncheckedIO.apply(Files::probeContentType, path); + } + + /** + * Delegates to {@link Files#readAllBytes(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. + * + * @param path See delegate. + * @return See delegate. + */ + public static byte[] readAllBytes(final Path path) { + return UncheckedIO.apply(Files::readAllBytes, path); + } + + /** + * Delegates to {@link Files#readAllLines(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. + * + * @param path See delegate. + * @return See delegate. + */ + public static List<String> readAllLines(final Path path) { + return UncheckedIO.apply(Files::readAllLines, path); + } + + /** + * Delegates to {@link Files#readAllLines(Path, Charset)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param path See delegate. + * @param cs See delegate. + * @return See delegate. + */ + public static List<String> readAllLines(final Path path, final Charset cs) { + return UncheckedIO.apply(Files::readAllLines, path, cs); + } + + /** + * Delegates to {@link Files#readAttributes(Path, Class, LinkOption...)} throwing {@link UncheckedIOException} instead + * of {@link IOException}. + * + * @param <A> See delegate. + * @param path See delegate. + * @param type See delegate. + * @param options See delegate. + * @return See delegate. + */ + public static <A extends BasicFileAttributes> A readAttributes(final Path path, final Class<A> type, final LinkOption... options) { + return UncheckedIO.apply(Files::readAttributes, path, type, options); + } + + /** + * Delegates to {@link Files#readAttributes(Path, String, LinkOption...)} throwing {@link UncheckedIOException} instead + * of {@link IOException}. + * + * @param path See delegate. + * @param attributes See delegate. + * @param options See delegate. + * @return See delegate. + */ + public static Map<String, Object> readAttributes(final Path path, final String attributes, final LinkOption... options) { + return UncheckedIO.apply(Files::readAttributes, path, attributes, options); + } + + /** + * Delegates to {@link Files#readSymbolicLink(Path)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param link See delegate. + * @return See delegate. + */ + public static Path readSymbolicLink(final Path link) { + return UncheckedIO.apply(Files::readSymbolicLink, link); + } + + /** + * Delegates to {@link Files#setAttribute(Path, String, Object, LinkOption...)} throwing {@link UncheckedIOException} + * instead of {@link IOException}. + * + * @param path See delegate. + * @param attribute See delegate. + * @param value See delegate. + * @param options See delegate. + * @return See delegate. + */ + public static Path setAttribute(final Path path, final String attribute, final Object value, final LinkOption... options) { + return UncheckedIO.apply(Files::setAttribute, path, attribute, value, options); + } + + /** + * Delegates to {@link Files#setLastModifiedTime(Path, FileTime)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param path See delegate. + * @param time See delegate. + * @return See delegate. + */ + public static Path setLastModifiedTime(final Path path, final FileTime time) { + return UncheckedIO.apply(Files::setLastModifiedTime, path, time); + } + + /** + * Delegates to {@link Files#setOwner(Path, UserPrincipal)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param path See delegate. + * @param owner See delegate. + * @return See delegate. + */ + public static Path setOwner(final Path path, final UserPrincipal owner) { + return UncheckedIO.apply(Files::setOwner, path, owner); + } + + /** + * Delegates to {@link Files#setPosixFilePermissions(Path, Set)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param path See delegate. + * @param perms See delegate. + * @return See delegate. + */ + public static Path setPosixFilePermissions(final Path path, final Set<PosixFilePermission> perms) { + return UncheckedIO.apply(Files::setPosixFilePermissions, path, perms); + } + + /** + * Delegates to {@link Files#size(Path)} throwing {@link UncheckedIOException} instead of {@link IOException}. + * + * @param path See delegate. + * @return See delegate. + */ + public static long size(final Path path) { + return UncheckedIO.apply(Files::size, path); + } + + /** + * Delegates to {@link Files#walk(Path, int, FileVisitOption...)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param start See delegate. + * @param maxDepth See delegate. + * @param options See delegate. + * @return See delegate. + */ + public static Stream<Path> walk(final Path start, final int maxDepth, final FileVisitOption... options) { + return UncheckedIO.apply(Files::walk, start, maxDepth, options); + } + + /** + * Delegates to {@link Files#walk(Path, FileVisitOption...)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param start See delegate. + * @param options See delegate. + * @return See delegate. + */ + public static Stream<Path> walk(final Path start, final FileVisitOption... options) { + return UncheckedIO.apply(Files::walk, start, options); + } + + /** + * Delegates to {@link Files#walkFileTree(Path, FileVisitor)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param start See delegate. + * @param visitor See delegate. + * @return See delegate. + */ + public static Path walkFileTree(final Path start, final FileVisitor<? super Path> visitor) { + return UncheckedIO.apply(Files::walkFileTree, start, visitor); + } + + /** + * Delegates to {@link Files#walkFileTree(Path, Set, int, FileVisitor)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param start See delegate. + * @param options See delegate. + * @param maxDepth See delegate. + * @param visitor See delegate. + * @return See delegate. + */ + public static Path walkFileTree(final Path start, final Set<FileVisitOption> options, final int maxDepth, final FileVisitor<? super Path> visitor) { + return UncheckedIO.apply(Files::walkFileTree, start, options, maxDepth, visitor); + } + + /** + * Delegates to {@link Files#write(Path, byte[], OpenOption...)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param path See delegate. + * @param bytes See delegate. + * @param options See delegate. + * @return See delegate. + */ + public static Path write(final Path path, final byte[] bytes, final OpenOption... options) { + return UncheckedIO.apply(Files::write, path, bytes, options); + } + + /** + * Delegates to {@link Files#write(Path, Iterable, Charset, OpenOption...)} throwing {@link UncheckedIOException} + * instead of {@link IOException}. + * + * @param path See delegate. + * @param lines See delegate. + * @param cs See delegate. + * @param options See delegate. + * @return See delegate. + */ + public static Path write(final Path path, final Iterable<? extends CharSequence> lines, final Charset cs, final OpenOption... options) { + return UncheckedIO.apply(Files::write, path, lines, cs, options); + } + + /** + * Delegates to {@link Files#write(Path, Iterable, OpenOption...)} throwing {@link UncheckedIOException} instead of + * {@link IOException}. + * + * @param path See delegate. + * @param lines See delegate. + * @param options See delegate. + * @return See delegate. + */ + public static Path write(final Path path, final Iterable<? extends CharSequence> lines, final OpenOption... options) { + return UncheckedIO.apply(Files::write, path, lines, options); + } + + /** + * No instances. + */ + private FilesUncheck() { + // No instances + } +} diff --git a/src/test/java/org/apache/commons/io/file/FilesUncheckTest.java b/src/test/java/org/apache/commons/io/file/FilesUncheckTest.java new file mode 100644 index 00000000..fd858203 --- /dev/null +++ b/src/test/java/org/apache/commons/io/file/FilesUncheckTest.java @@ -0,0 +1,445 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.io.file; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.UncheckedIOException; +import java.nio.channels.SeekableByteChannel; +import java.nio.charset.StandardCharsets; +import java.nio.file.DirectoryStream; +import java.nio.file.FileVisitOption; +import java.nio.file.Files; +import java.nio.file.LinkOption; +import java.nio.file.OpenOption; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.nio.file.StandardOpenOption; +import java.nio.file.attribute.BasicFileAttributes; +import java.nio.file.attribute.FileAttribute; +import java.nio.file.attribute.FileTime; +import java.nio.file.attribute.PosixFilePermission; +import java.nio.file.attribute.UserPrincipal; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.io.UncheckedIO; +import org.apache.commons.io.input.NullInputStream; +import org.apache.commons.io.output.NullOutputStream; +import org.apache.commons.lang3.ArrayUtils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Tests {@link FilesUncheck}. + * + * These tests are simple and just makes sure we do can make the call without catching IOException. + */ +public class FilesUncheckTest { + + private static final FileAttribute<?>[] EMPTY_FILE_ATTRIBUTES_ARRAY = {}; + + private static final Path FILE_PATH_EMPTY = Paths.get("src/test/resources/org/apache/commons/io/test-file-empty.bin"); + + private static final Path FILE_PATH_A = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin"); + + private static final Path NEW_DIR_PATH = Paths.get("target/newdir"); + + private static final Path NEW_FILE_PATH = Paths.get("target/file.txt"); + + private static final Path NEW_FILE_PATH_LINK = Paths.get("target/to_another_file.txt"); + + private static final String PREFIX = "prefix"; + + private static final String SUFFIX = "suffix"; + + private static final Path TARGET_PATH = Paths.get("target"); + + @BeforeEach + @AfterEach + public void deleteFixtures() throws IOException { + Files.deleteIfExists(NEW_FILE_PATH); + Files.deleteIfExists(NEW_DIR_PATH); + Files.deleteIfExists(NEW_FILE_PATH_LINK); + } + + @Test + public void testCopyInputStreamPathCopyOptionArray() { + assertEquals(0, FilesUncheck.copy(NullInputStream.INSTANCE, NEW_FILE_PATH, PathUtils.EMPTY_COPY_OPTIONS)); + } + + @Test + public void testCopyPathOutputStream() { + assertEquals(0, FilesUncheck.copy(FILE_PATH_EMPTY, NullOutputStream.INSTANCE)); + } + + @Test + public void testCopyPathPathCopyOptionArray() { + assertEquals(NEW_FILE_PATH, FilesUncheck.copy(FILE_PATH_EMPTY, NEW_FILE_PATH, PathUtils.EMPTY_COPY_OPTIONS)); + } + + @Test + public void testCreateDirectories() { + assertEquals(TARGET_PATH, FilesUncheck.createDirectories(TARGET_PATH, EMPTY_FILE_ATTRIBUTES_ARRAY)); + } + + @Test + public void testCreateDirectory() { + assertEquals(NEW_DIR_PATH, FilesUncheck.createDirectory(NEW_DIR_PATH, EMPTY_FILE_ATTRIBUTES_ARRAY)); + } + + @Test + public void testCreateFile() { + assertEquals(NEW_FILE_PATH, FilesUncheck.createFile(NEW_FILE_PATH, EMPTY_FILE_ATTRIBUTES_ARRAY)); + } + + @Test + public void testCreateLink() { + assertEquals(NEW_FILE_PATH_LINK, FilesUncheck.createLink(NEW_FILE_PATH_LINK, FILE_PATH_EMPTY)); + } + + @Test + public void testCreateSymbolicLink() { + // May cause: Caused by: java.nio.file.FileSystemException: A required privilege is not held by the client. + assertEquals(NEW_FILE_PATH_LINK, FilesUncheck.createSymbolicLink(NEW_FILE_PATH_LINK, FILE_PATH_EMPTY)); + } + + @Test + public void testCreateTempDirectoryPathStringFileAttributeOfQArray() { + assertEquals(TARGET_PATH, FilesUncheck.createTempDirectory(TARGET_PATH, PREFIX, EMPTY_FILE_ATTRIBUTES_ARRAY).getParent()); + } + + @Test + public void testCreateTempDirectoryStringFileAttributeOfQArray() { + assertEquals(PathUtils.getTempDirectory(), FilesUncheck.createTempDirectory(PREFIX, EMPTY_FILE_ATTRIBUTES_ARRAY).getParent()); + } + + @Test + public void testCreateTempFilePathStringStringFileAttributeOfQArray() { + assertEquals(TARGET_PATH, FilesUncheck.createTempFile(TARGET_PATH, PREFIX, SUFFIX, EMPTY_FILE_ATTRIBUTES_ARRAY).getParent()); + } + + @Test + public void testCreateTempFileStringStringFileAttributeOfQArray() { + assertEquals(PathUtils.getTempDirectory(), FilesUncheck.createTempFile(PREFIX, SUFFIX, EMPTY_FILE_ATTRIBUTES_ARRAY).getParent()); + } + + @Test + public void testDelete() { + assertThrows(UncheckedIOException.class, () -> FilesUncheck.delete(NEW_FILE_PATH)); + } + + @Test + public void testDeleteIfExists() { + assertFalse(FilesUncheck.deleteIfExists(NEW_FILE_PATH)); + } + + @Test + public void testGetAttribute() { + assertEquals(0L, FilesUncheck.getAttribute(FILE_PATH_EMPTY, "basic:size", LinkOption.NOFOLLOW_LINKS)); + } + + @Test + public void testGetFileStore() { + assertNotNull(FilesUncheck.getFileStore(FILE_PATH_EMPTY)); + } + + @Test + public void testGetLastModifiedTime() { + assertTrue(0 < FilesUncheck.getLastModifiedTime(FILE_PATH_EMPTY, LinkOption.NOFOLLOW_LINKS).toMillis()); + } + + @Test + public void testGetOwner() { + assertNotNull(FilesUncheck.getOwner(FILE_PATH_EMPTY, LinkOption.NOFOLLOW_LINKS)); + } + + @Test + public void testGetPosixFilePermissions() { + assumeTrue(PathUtils.isPosix(FILE_PATH_EMPTY, LinkOption.NOFOLLOW_LINKS)); + assertNotNull(FilesUncheck.getPosixFilePermissions(FILE_PATH_EMPTY, LinkOption.NOFOLLOW_LINKS)); + } + + @Test + public void testIsHidden() { + assertFalse(FilesUncheck.isHidden(FILE_PATH_EMPTY)); + } + + @Test + public void testIsSameFile() { + assertTrue(FilesUncheck.isSameFile(FILE_PATH_EMPTY, FILE_PATH_EMPTY)); + } + + @Test + public void testLinesPath() { + assertEquals(0, FilesUncheck.lines(FILE_PATH_EMPTY).count()); + } + + @Test + public void testLinesPathCharset() { + assertEquals(0, FilesUncheck.lines(FILE_PATH_EMPTY, StandardCharsets.UTF_8).count()); + } + + @Test + public void testList() { + assertEquals(1, FilesUncheck.list(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0")).count()); + } + + @Test + public void testMove() { + final Path tempFile1 = FilesUncheck.createTempFile(PREFIX, SUFFIX); + final Path tempFile2 = FilesUncheck.createTempFile(PREFIX, SUFFIX); + assertEquals(tempFile2, FilesUncheck.move(tempFile1, tempFile2, StandardCopyOption.REPLACE_EXISTING)); + FilesUncheck.delete(tempFile2); + } + + @Test + public void testNewBufferedReaderPath() { + UncheckedIO.run(() -> { + try (BufferedReader reader = FilesUncheck.newBufferedReader(FILE_PATH_EMPTY)) { + IOUtils.consume(reader); + } + }); + } + + @Test + public void testNewBufferedReaderPathCharset() { + UncheckedIO.run(() -> { + try (BufferedReader reader = FilesUncheck.newBufferedReader(FILE_PATH_EMPTY, StandardCharsets.UTF_8)) { + IOUtils.consume(reader); + } + }); + } + + @Test + public void testNewBufferedWriterPathCharsetOpenOptionArray() { + final Path tempPath = FilesUncheck.createTempFile(PREFIX, SUFFIX); + UncheckedIO.run(() -> { + try (BufferedWriter writer = FilesUncheck.newBufferedWriter(tempPath, StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING)) { + writer.append("test"); + } + }); + assertEquals("test", FilesUncheck.readAllLines(tempPath, StandardCharsets.UTF_8).get(0)); + } + + @Test + public void testNewBufferedWriterPathOpenOptionArray() { + final Path tempPath = FilesUncheck.createTempFile(PREFIX, SUFFIX); + UncheckedIO.run(() -> { + try (BufferedWriter writer = FilesUncheck.newBufferedWriter(tempPath, StandardOpenOption.TRUNCATE_EXISTING)) { + writer.append("test"); + } + }); + assertEquals("test", FilesUncheck.readAllLines(tempPath).get(0)); + } + + @Test + public void testNewByteChannelPathOpenOptionArray() { + assertEquals(0, UncheckedIO.get(() -> { + try (SeekableByteChannel c = FilesUncheck.newByteChannel(FILE_PATH_EMPTY, StandardOpenOption.READ)) { + return c.size(); + } + })); + } + + @Test + public void testNewByteChannelPathSetOfQextendsOpenOptionFileAttributeOfQArray() { + final Set<OpenOption> options = new HashSet<>(); + options.add(StandardOpenOption.READ); + assertEquals(0, UncheckedIO.get(() -> { + try (SeekableByteChannel c = FilesUncheck.newByteChannel(FILE_PATH_EMPTY, options, EMPTY_FILE_ATTRIBUTES_ARRAY)) { + return c.size(); + } + })); + } + + @Test + public void testNewDirectoryStreamPath() { + UncheckedIO.run(() -> { + try (final DirectoryStream<Path> directoryStream = FilesUncheck.newDirectoryStream(TARGET_PATH)) { + directoryStream.forEach(e -> assertEquals(TARGET_PATH, e.getParent())); + } + }); + } + + @Test + public void testNewDirectoryStreamPathString() { + UncheckedIO.run(() -> { + try (final DirectoryStream<Path> directoryStream = FilesUncheck.newDirectoryStream(TARGET_PATH, "*.xml")) { + directoryStream.forEach(e -> assertEquals(TARGET_PATH, e.getParent())); + } + }); + } + + @Test + public void testNewDirectoryStreamPathFilterOfQsuperPath() { + UncheckedIO.run(() -> { + try (final DirectoryStream<Path> directoryStream = FilesUncheck.newDirectoryStream(TARGET_PATH, e -> true)) { + directoryStream.forEach(e -> assertEquals(TARGET_PATH, e.getParent())); + } + }); + } + + @Test + public void testNewInputStream() { + assertEquals(0, UncheckedIO.get(() -> { + try (InputStream in = FilesUncheck.newInputStream(FILE_PATH_EMPTY, StandardOpenOption.READ)) { + return in.available(); + } + })); + } + + @Test + public void testNewOutputStream() { + final Path tempPath = FilesUncheck.createTempFile(PREFIX, SUFFIX); + UncheckedIO.run(() -> { + try (OutputStream stream = FilesUncheck.newOutputStream(tempPath, StandardOpenOption.TRUNCATE_EXISTING)) { + stream.write("test".getBytes()); + } + }); + assertEquals("test", FilesUncheck.readAllLines(tempPath).get(0)); + } + + @Test + public void testProbeContentType() { + assertNull(FilesUncheck.probeContentType(FILE_PATH_EMPTY)); + assertEquals("text/xml", FilesUncheck.probeContentType(Paths.get("src/test/resources/org/apache/commons/io/testfileBOM.xml"))); + } + + @Test + public void testReadAllBytes() { + assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, FilesUncheck.readAllBytes(FILE_PATH_EMPTY)); + assertArrayEquals(new byte[] {'a'}, FilesUncheck.readAllBytes(FILE_PATH_A)); + } + + @Test + public void testReadAllLinesPath() { + assertEquals(Collections.emptyList(), FilesUncheck.readAllLines(FILE_PATH_EMPTY)); + assertEquals(Arrays.asList("a"), FilesUncheck.readAllLines(FILE_PATH_A)); + } + + @Test + public void testReadAllLinesPathCharset() { + assertEquals(Collections.emptyList(), FilesUncheck.readAllLines(FILE_PATH_EMPTY, StandardCharsets.UTF_8)); + assertEquals(Arrays.asList("a"), FilesUncheck.readAllLines(FILE_PATH_A, StandardCharsets.UTF_8)); + } + + @Test + public void testReadAttributesPathClassOfALinkOptionArray() { + assertNotNull(FilesUncheck.readAttributes(FILE_PATH_EMPTY, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS)); + } + + @Test + public void testReadAttributesPathStringLinkOptionArray() { + assertNotNull(FilesUncheck.readAttributes(FILE_PATH_EMPTY, "basic:lastModifiedTime", LinkOption.NOFOLLOW_LINKS)); + } + + @Test + public void testReadSymbolicLink() { + assertThrows(UncheckedIOException.class, () -> FilesUncheck.readSymbolicLink(NEW_FILE_PATH_LINK)); + } + + @Test + public void testSetAttribute() { + final FileTime ft = FilesUncheck.getLastModifiedTime(FILE_PATH_EMPTY); + assertEquals(FILE_PATH_EMPTY, FilesUncheck.setAttribute(FILE_PATH_EMPTY, "basic:lastModifiedTime", ft, LinkOption.NOFOLLOW_LINKS)); + } + + @Test + public void testSetLastModifiedTime() { + final FileTime ft = FilesUncheck.getLastModifiedTime(FILE_PATH_EMPTY); + assertEquals(FILE_PATH_EMPTY, FilesUncheck.setLastModifiedTime(FILE_PATH_EMPTY, ft)); + } + + @Test + public void testSetOwner() { + final UserPrincipal owner = FilesUncheck.getOwner(FILE_PATH_EMPTY, LinkOption.NOFOLLOW_LINKS); + assertEquals(FILE_PATH_EMPTY, FilesUncheck.setOwner(FILE_PATH_EMPTY, owner)); + } + + @Test + public void testSetPosixFilePermissions() { + assumeTrue(PathUtils.isPosix(FILE_PATH_EMPTY, LinkOption.NOFOLLOW_LINKS)); + final Set<PosixFilePermission> posixFilePermissions = FilesUncheck.getPosixFilePermissions(FILE_PATH_EMPTY, LinkOption.NOFOLLOW_LINKS); + assertEquals(FILE_PATH_EMPTY, FilesUncheck.setPosixFilePermissions(FILE_PATH_EMPTY, posixFilePermissions)); + } + + @Test + public void testSize() { + assertEquals(0, FilesUncheck.size(FILE_PATH_EMPTY)); + assertEquals(1, FilesUncheck.size(FILE_PATH_A)); + } + + @Test + public void testWalkPathIntFileVisitOptionArray() { + assertEquals(1, FilesUncheck.walk(TARGET_PATH, 0, FileVisitOption.FOLLOW_LINKS).count()); + } + + @Test + public void testWalkPathFileVisitOptionArray() { + assertTrue(0 < FilesUncheck.walk(TARGET_PATH, FileVisitOption.FOLLOW_LINKS).count()); + } + + @Test + public void testWalkFileTreePathFileVisitorOfQsuperPath() { + assertEquals(TARGET_PATH, FilesUncheck.walkFileTree(TARGET_PATH, NoopPathVisitor.INSTANCE)); + } + + @Test + public void testWalkFileTreePathSetOfFileVisitOptionIntFileVisitorOfQsuperPath() { + assertEquals(TARGET_PATH, FilesUncheck.walkFileTree(TARGET_PATH, new HashSet<>(), 1, NoopPathVisitor.INSTANCE)); + } + + @Test + public void testWritePathByteArrayOpenOptionArray() { + final Path tempFile = FilesUncheck.createTempFile(PREFIX, SUFFIX); + assertEquals(tempFile, FilesUncheck.write(tempFile, "test".getBytes(), StandardOpenOption.TRUNCATE_EXISTING)); + FilesUncheck.delete(tempFile); + } + + @Test + public void testWritePathIterableOfQextendsCharSequenceCharsetOpenOptionArray() { + final Path tempFile = FilesUncheck.createTempFile(PREFIX, SUFFIX); + assertEquals(tempFile, FilesUncheck.write(tempFile, Arrays.asList("test"), StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING)); + FilesUncheck.delete(tempFile); + } + + @Test + public void testWritePathIterableOfQextendsCharSequenceOpenOptionArray() { + final Path tempFile = FilesUncheck.createTempFile(PREFIX, SUFFIX); + assertEquals(tempFile, FilesUncheck.write(tempFile, Arrays.asList("test"), StandardOpenOption.TRUNCATE_EXISTING)); + FilesUncheck.delete(tempFile); + } + +}