s/candidate.length() == 0/candidate.isEmpty()

?



On Nov 16, 2017 05:44, <s...@apache.org> wrote:

> Repository: commons-io
> Updated Branches:
>   refs/heads/master dcf6e82f8 -> 84a0d9078
>
>
> += isLegalFile(CharSequence)
>
> Project: http://git-wip-us.apache.org/repos/asf/commons-io/repo
> Commit: http://git-wip-us.apache.org/repos/asf/commons-io/commit/84a0d907
> Tree: http://git-wip-us.apache.org/repos/asf/commons-io/tree/84a0d907
> Diff: http://git-wip-us.apache.org/repos/asf/commons-io/diff/84a0d907
>
> Branch: refs/heads/master
> Commit: 84a0d90783b095775b5d14b2a807497862d80db5
> Parents: dcf6e82
> Author: Sebb <s...@apache.org>
> Authored: Thu Nov 16 12:44:10 2017 +0000
> Committer: Sebb <s...@apache.org>
> Committed: Thu Nov 16 12:44:10 2017 +0000
>
> ----------------------------------------------------------------------
>  .../java/org/apache/commons/io/FileSystem.java  | 25 +++++++++++++++++++-
>  .../apache/commons/io/FileSystemTestCase.java   | 10 ++++++++
>  2 files changed, 34 insertions(+), 1 deletion(-)
> ----------------------------------------------------------------------
>
>
> http://git-wip-us.apache.org/repos/asf/commons-io/blob/
> 84a0d907/src/main/java/org/apache/commons/io/FileSystem.java
> ----------------------------------------------------------------------
> diff --git a/src/main/java/org/apache/commons/io/FileSystem.java
> b/src/main/java/org/apache/commons/io/FileSystem.java
> index 94a7e36..fed5bd6 100644
> --- a/src/main/java/org/apache/commons/io/FileSystem.java
> +++ b/src/main/java/org/apache/commons/io/FileSystem.java
> @@ -199,7 +199,7 @@ public enum FileSystem {
>      /**
>       * Converts a candidate file name (without a path) like {@code
> "filename.ext"} or {@code "filename"} to a legal file
>       * name. Illegal characters in the candidate name are replaced by the
> {@code replacement} character. If the file
> -     * name exceeds {@link #getMaxFileNameLength()}, then the name is
> truncated to {@link #getMaxFileNameLength()}.
> +     * name length exceeds {@link #getMaxFileNameLength()}, then the name
> is truncated to {@link #getMaxFileNameLength()}.
>       *
>       * @param candidate
>       *            a candidate file name (without a path) like {@code
> "filename.ext"} or {@code "filename"}
> @@ -225,4 +225,27 @@ public enum FileSystem {
>          }
>          return changed ? String.valueOf(charArray) : truncated;
>      }
> +
> +    /**
> +     * Checks if a candidate file name (without a path)
> +     * such as {@code "filename.ext"} or {@code "filename"}
> +     * is a potentially legal file name.
> +     * If the file name length exceeds {@link #getMaxFileNameLength()},
> +     * or if it contains an illegal character then the check fails.
> +     *
> +     * @param candidate
> +     *            a candidate file name (without a path) like {@code
> "filename.ext"} or {@code "filename"}
> +     * @return {@code true} if the candidate name is legal
> +     */
> +    public boolean isLegalFileName(final CharSequence candidate) {
> +        if (candidate == null || candidate.length() == 0 ||
> candidate.length() > maxFileNameLength) {
> +            return false;
> +        }
> +        for (int i = 0; i < candidate.length(); i++) {
> +            if (isIllegalFileNameChar(candidate.charAt(i))) {
> +                return false;
> +            }
> +        }
> +        return true;
> +    }
>  }
> \ No newline at end of file
>
> http://git-wip-us.apache.org/repos/asf/commons-io/blob/
> 84a0d907/src/test/java/org/apache/commons/io/FileSystemTestCase.java
> ----------------------------------------------------------------------
> diff --git a/src/test/java/org/apache/commons/io/FileSystemTestCase.java
> b/src/test/java/org/apache/commons/io/FileSystemTestCase.java
> index 84dc3c3..f5335c1 100644
> --- a/src/test/java/org/apache/commons/io/FileSystemTestCase.java
> +++ b/src/test/java/org/apache/commons/io/FileSystemTestCase.java
> @@ -52,5 +52,15 @@ public class FileSystemTestCase {
>          for (char i = '0'; i < '9'; i++) {
>              Assert.assertEquals(i, fs.toLegalFileName(String.valueOf(i),
> replacement).charAt(0));
>          }
> +    }
> +
> +    @Test
> +    public void testIsLegalName() {
> +        for (FileSystem fs : FileSystem.values()) {
> +            Assert.assertFalse(fs.name(), fs.isLegalFileName("")); //
> Empty is always illegal
> +            Assert.assertFalse(fs.name(), fs.isLegalFileName(null)); //
> null is always illegal
> +            Assert.assertFalse(fs.name(), fs.isLegalFileName("\0")); //
> Assume NUL is always illegal
> +            Assert.assertTrue(fs.name(), fs.isLegalFileName("0")); //
> Assume simple name always legal
> +        }
>      }
>  }
>
>

Reply via email to