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
The following commit(s) were added to refs/heads/master by this push: new 1d14807 [IO-632] Add PathUtils for operations on NIO Path. 1d14807 is described below commit 1d1480716b147523440cbea61d43baeefe124f09 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Thu Oct 10 14:18:15 2019 -0400 [IO-632] Add PathUtils for operations on NIO Path. --- src/changes/changes.xml | 3 + src/main/java/org/apache/commons/io/PathUtils.java | 70 ++++++++++++++++++++++ .../java/org/apache/commons/io/PathUtilsTest.java | 70 ++++++++++++++++++++++ 3 files changed, 143 insertions(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index d08df13..cdb38e6 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -146,6 +146,9 @@ The <action> type attribute can be add,update,fix,remove. <action issue="IO-631" dev="ggregory" type="add" due-to="Gary Gregory"> Add a CountingFileVisitor (as the basis for a forthcoming DeletingFileVisitor). </action> + <action issue="IO-632" dev="ggregory" type="add" due-to="Gary Gregory"> + Add PathUtils for operations on NIO Path. + </action> </release> <release version="2.6" date="2017-10-15" description="Java 7 required, Java 9 supported."> diff --git a/src/main/java/org/apache/commons/io/PathUtils.java b/src/main/java/org/apache/commons/io/PathUtils.java new file mode 100644 index 0000000..ee5b821 --- /dev/null +++ b/src/main/java/org/apache/commons/io/PathUtils.java @@ -0,0 +1,70 @@ +/* + * 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; + +import java.io.IOException; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; +import java.nio.file.Path; + +/** + * NIO Path utilities. + * + * @since 2.7 + */ +public class PathUtils { + + /** + * Returns whether the given file or directory is empty. + * + * @param path the the given file or directory to query. + * @return whether the given file or directory is empty. + * @throws IOException if an I/O error occurs + */ + public static boolean isEmpty(final Path path) throws IOException { + return Files.isDirectory(path) ? isEmptyDirectory(path) : isEmptyFile(path); + } + + /** + * Returns whether the directory is empty. + * + * @param directory the the given directory to query. + * @return whether the given directory is empty. + * @throws IOException if an I/O error occurs + */ + public static boolean isEmptyDirectory(final Path directory) throws IOException { + try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory)) { + if (directoryStream.iterator().hasNext()) { + return false; + } + } + return true; + } + + /** + * Returns whether the given file is empty. + * + * @param file the the given file to query. + * @return whether the given file is empty. + * @throws IOException if an I/O error occurs + */ + public static boolean isEmptyFile(final Path file) throws IOException { + return Files.size(file) <= 0; + } + +} diff --git a/src/test/java/org/apache/commons/io/PathUtilsTest.java b/src/test/java/org/apache/commons/io/PathUtilsTest.java new file mode 100644 index 0000000..134a209 --- /dev/null +++ b/src/test/java/org/apache/commons/io/PathUtilsTest.java @@ -0,0 +1,70 @@ +/* + * 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; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * Tests {@link PathUtils}. + */ +public class PathUtilsTest { + + private static final Path DIR_SIZE_1 = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"); + + private static final Path FILE_SIZE_0 = Paths + .get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0/file-size-0.bin"); + + private static final Path FILE_SIZE_1 = Paths + .get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin"); + + @Test + public void testIsEmpty() throws IOException { + Assertions.assertTrue(PathUtils.isEmpty(FILE_SIZE_0)); + Assertions.assertFalse(PathUtils.isEmpty(FILE_SIZE_1)); + final Path tempDirectory = Files.createTempDirectory(getClass().getCanonicalName()); + try { + Assertions.assertTrue(PathUtils.isEmpty(tempDirectory)); + } finally { + Files.delete(tempDirectory); + } + Assertions.assertFalse(PathUtils.isEmpty(DIR_SIZE_1)); + } + + @Test + public void testisEmptyDirectory() throws IOException { + final Path tempDirectory = Files.createTempDirectory(getClass().getCanonicalName()); + try { + Assertions.assertTrue(PathUtils.isEmptyDirectory(tempDirectory)); + } finally { + Files.delete(tempDirectory); + } + Assertions.assertFalse(PathUtils.isEmptyDirectory(DIR_SIZE_1)); + } + + @Test + public void testisEmptyFile() throws IOException { + Assertions.assertTrue(PathUtils.isEmptyFile(FILE_SIZE_0)); + Assertions.assertFalse(PathUtils.isEmptyFile(FILE_SIZE_1)); + } +}