You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2025/09/14 21:24:30 UTC

(commons-io) 03/04: Update new API FileSystem.toLegalFileName(CharSequence, char, Charset) to use a CharSequence instead of a String for input

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 5dd1dff2e9dd42274235ba502c81c026f31c6b48
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 14 17:24:17 2025 -0400

    Update new API FileSystem.toLegalFileName(CharSequence, char, Charset)
    to use a CharSequence instead of a String for input
    
    Add FileSystemTest.testXmlRoundtrip(FileSystem, Path)
---
 .../java/org/apache/commons/io/FileSystem.java     | 63 +++++++----------
 .../java/org/apache/commons/io/FileSystemTest.java | 82 ++++++++++++++++++++++
 2 files changed, 109 insertions(+), 36 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/FileSystem.java b/src/main/java/org/apache/commons/io/FileSystem.java
index c75eb9f49..4169e1a53 100644
--- a/src/main/java/org/apache/commons/io/FileSystem.java
+++ b/src/main/java/org/apache/commons/io/FileSystem.java
@@ -138,39 +138,30 @@ CharSequence truncate(final CharSequence value, final int limit, final Charset c
                 final CharsetEncoder encoder = charset.newEncoder()
                         .onMalformedInput(CodingErrorAction.REPORT)
                         .onUnmappableCharacter(CodingErrorAction.REPORT);
-
                 if (!encoder.canEncode(value)) {
-                    throw new IllegalArgumentException(
-                            "The value " + value + " cannot be encoded using " + charset.name());
+                    throw new IllegalArgumentException("The value " + value + " cannot be encoded using " + charset.name());
                 }
-
                 // Fast path: if even the worst-case expansion fits, we're done.
                 if (value.length() <= Math.floor(limit / encoder.maxBytesPerChar())) {
                     return value;
                 }
-
                 // Slow path: encode into a fixed-size byte buffer.
                 // 1. Compute length of extension in bytes (if any).
                 final CharSequence[] parts = splitExtension(value);
                 final int extensionLength = getLength(parts[1], charset);
                 if (extensionLength > 0 && extensionLength >= limit) {
                     // Extension itself does not fit
-                    throw new IllegalArgumentException(
-                            "The extension of " + value + " is too long to fit within " + limit + " bytes");
+                    throw new IllegalArgumentException("The extension of " + value + " is too long to fit within " + limit + " bytes");
                 }
-
                 // 2. Compute the character part that fits within the remaining byte budget.
                 final ByteBuffer byteBuffer = ByteBuffer.allocate(limit - extensionLength);
                 final CharBuffer charBuffer = CharBuffer.wrap(parts[0]);
-
                 // Encode until the first character that would exceed the byte budget.
                 final CoderResult cr = encoder.encode(charBuffer, byteBuffer, true);
-
                 if (cr.isUnderflow()) {
                     // Entire candidate fit within maxFileNameLength bytes.
                     return value;
                 }
-
                 final CharSequence truncated = safeTruncate(value, charBuffer.position());
                 return extensionLength == 0 ? truncated : truncated.toString() + parts[1];
             }
@@ -186,25 +177,20 @@ int getLength(final CharSequence value, final Charset charset) {
             @Override
             CharSequence truncate(final CharSequence value, final int limit, final Charset charset) {
                 if (!UTF_16.newEncoder().canEncode(value)) {
-                    throw new IllegalArgumentException(
-                            "The value " + value + " can not be encoded using " + UTF_16.name());
+                    throw new IllegalArgumentException("The value " + value + " can not be encoded using " + UTF_16.name());
                 }
-
                 // Fast path: no truncation needed.
                 if (value.length() <= limit) {
                     return value;
                 }
-
                 // Slow path: truncate to limit.
                 // 1. Compute length of extension in chars (if any).
                 final CharSequence[] parts = splitExtension(value);
                 final int extensionLength = parts[1].length();
                 if (extensionLength > 0 && extensionLength >= limit) {
                     // Extension itself does not fit
-                    throw new IllegalArgumentException(
-                            "The extension of " + value + " is too long to fit within " + limit + " characters");
+                    throw new IllegalArgumentException("The extension of " + value + " is too long to fit within " + limit + " characters");
                 }
-
                 // 2. Truncate the non-extension part and append the extension (if any).
                 final CharSequence truncated = safeTruncate(value, limit - extensionLength);
                 return extensionLength == 0 ? truncated : truncated.toString() + parts[1];
@@ -560,6 +546,10 @@ public int getMaxPathLength() {
         return maxPathLength;
     }
 
+    NameLengthStrategy getNameLengthStrategy() {
+        return nameLengthStrategy;
+    }
+
     /**
      * Gets the name separator, '\\' on Windows, '/' on Linux.
      *
@@ -703,12 +693,27 @@ public boolean supportsDriveLetter() {
      *            A candidate file name (without a path) like {@code "filename.ext"} or {@code "filename"}.
      * @param replacement
      *            Illegal characters in the candidate name are replaced by this character.
+     * @param charset
+     *            The charset to use when the file name length is measured in bytes.
      * @return a String without illegal characters.
+     * @since 2.21.0
      */
-    public String toLegalFileName(final String candidate, final char replacement) {
-        return toLegalFileName(candidate, replacement, Charset.defaultCharset());
+    public String toLegalFileName(final CharSequence candidate, final char replacement, final Charset charset) {
+        Objects.requireNonNull(candidate, "candidate");
+        if (candidate.length() == 0) {
+            throw new IllegalArgumentException("The candidate file name is empty");
+        }
+        if (isIllegalFileNameChar(replacement)) {
+            // %s does not work properly with NUL
+            throw new IllegalArgumentException(String.format("The replacement character '%s' cannot be one of the %s illegal characters: %s",
+                replacement == '\0' ? "\\0" : replacement, name(), Arrays.toString(illegalFileNameChars)));
+        }
+        final CharSequence truncated = nameLengthStrategy.truncate(candidate, getMaxFileNameLength(), charset);
+        final int[] array = truncated.chars().map(i -> isIllegalFileNameChar(i) ? replacement : i).toArray();
+        return new String(array, 0, array.length);
     }
 
+
     /**
      * Converts a candidate file name (without a path) to a legal file name.
      *
@@ -722,24 +727,10 @@ public String toLegalFileName(final String candidate, final char replacement) {
      *            A candidate file name (without a path) like {@code "filename.ext"} or {@code "filename"}.
      * @param replacement
      *            Illegal characters in the candidate name are replaced by this character.
-     * @param charset
-     *            The charset to use when the file name length is measured in bytes.
      * @return a String without illegal characters.
-     * @since 2.21.0
      */
-    public String toLegalFileName(final String candidate, final char replacement, final Charset charset) {
-        Objects.requireNonNull(candidate, "candidate");
-        if (candidate.isEmpty()) {
-            throw new IllegalArgumentException("The candidate file name is empty");
-        }
-        if (isIllegalFileNameChar(replacement)) {
-            // %s does not work properly with NUL
-            throw new IllegalArgumentException(String.format("The replacement character '%s' cannot be one of the %s illegal characters: %s",
-                replacement == '\0' ? "\\0" : replacement, name(), Arrays.toString(illegalFileNameChars)));
-        }
-        final CharSequence truncated = nameLengthStrategy.truncate(candidate, getMaxFileNameLength(), charset);
-        final int[] array = truncated.chars().map(i -> isIllegalFileNameChar(i) ? replacement : i).toArray();
-        return new String(array, 0, array.length);
+    public String toLegalFileName(final String candidate, final char replacement) {
+        return toLegalFileName(candidate, replacement, Charset.defaultCharset());
     }
 
 }
diff --git a/src/test/java/org/apache/commons/io/FileSystemTest.java b/src/test/java/org/apache/commons/io/FileSystemTest.java
index 167a8649d..f4c841696 100644
--- a/src/test/java/org/apache/commons/io/FileSystemTest.java
+++ b/src/test/java/org/apache/commons/io/FileSystemTest.java
@@ -27,19 +27,28 @@
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNotSame;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
+import java.io.BufferedReader;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.StringReader;
 import java.nio.charset.Charset;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.util.Objects;
 import java.util.stream.Stream;
 
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
 import org.apache.commons.io.FileSystem.NameLengthStrategy;
 import org.apache.commons.lang3.JavaVersion;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.SystemProperties;
 import org.apache.commons.lang3.SystemUtils;
 import org.junit.jupiter.api.Test;
@@ -48,6 +57,9 @@
 import org.junit.jupiter.params.provider.Arguments;
 import org.junit.jupiter.params.provider.EnumSource;
 import org.junit.jupiter.params.provider.MethodSource;
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
 
 /**
  * Tests {@link FileSystem}.
@@ -252,6 +264,20 @@ static Stream<Arguments> testNameLengthStrategyTruncate_Throws() {
                                 Arguments.of(UTF16_CODE_UNITS, 30, CHAR_UTF8_69B, UTF_8, "truncated to 30 characters")));
     }
 
+    private String parseXmlRootValue(final Path xmlPath, final Charset charset) throws SAXException, IOException, ParserConfigurationException {
+        final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+        try (BufferedReader reader = Files.newBufferedReader(xmlPath, charset)) {
+            final Document document = builder.parse(new InputSource(reader));
+            return document.getDocumentElement().getTextContent();
+        }
+    }
+
+    private String parseXmlRootValue(final String xmlString) throws SAXException, IOException, ParserConfigurationException {
+        final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+        final Document document = builder.parse(new InputSource(new StringReader(xmlString)));
+        return document.getDocumentElement().getTextContent();
+    }
+
     @Test
     void testGetBlockSize() {
         assertTrue(FileSystem.getCurrent().getBlockSize() >= 0);
@@ -487,4 +513,60 @@ void testToLegalFileNameWindows() {
         assertThrows(IllegalArgumentException.class, () -> fs.toLegalFileName("test", '\0'));
         assertThrows(IllegalArgumentException.class, () -> fs.toLegalFileName("test", ':'));
     }
+
+    @ParameterizedTest
+    @EnumSource(FileSystem.class)
+    void testXmlRoundtrip(final FileSystem fs, @TempDir final Path tempDir) throws Exception {
+        final Charset charset = Charset.defaultCharset();
+        assertEquals("a", fs.toLegalFileName("a", '_', charset));
+        assertEquals("abcdefghijklmno", fs.toLegalFileName("abcdefghijklmno", '_', charset));
+        assertEquals("\u4F60\u597D\u55CE", fs.toLegalFileName("\u4F60\u597D\u55CE", '_', charset));
+        assertEquals("\u2713\u2714", fs.toLegalFileName("\u2713\u2714", '_', charset));
+        assertEquals("\uD83D\uDE80\u2728\uD83C\uDF89", fs.toLegalFileName("\uD83D\uDE80\u2728\uD83C\uDF89", '_', charset));
+        assertEquals("\uD83D\uDE03", fs.toLegalFileName("\uD83D\uDE03", '_', charset));
+        assertEquals("\uD83D\uDE03\uD83D\uDE03\uD83D\uDE03\uD83D\uDE03\uD83D\uDE03",
+                fs.toLegalFileName("\uD83D\uDE03\uD83D\uDE03\uD83D\uDE03\uD83D\uDE03\uD83D\uDE03", '_', charset));
+        for (int i = 1; i <= 100; i++) {
+            final String name1 = fs.toLegalFileName(StringUtils.repeat("🦊", i), '_', charset);
+            assertNotNull(name1);
+            final byte[] name1Bytes = name1.getBytes();
+            final String xmlString1 = toXmlString(name1, charset);
+            final Path path = tempDir.resolve(name1);
+            Files.write(path, xmlString1.getBytes(charset));
+            final String xmlFromPath = parseXmlRootValue(path, charset);
+            assertEquals(name1, xmlFromPath, "i =  " + i);
+            final String name2 = new String(name1Bytes, charset);
+            assertEquals(name1, name2);
+            final String xmlString2 = toXmlString(name2, charset);
+            assertEquals(xmlString1, xmlString2);
+            final String parsedValue = Objects.toString(parseXmlRootValue(xmlString2), "");
+            assertEquals(name1, parsedValue, "i =  " + i);
+            assertEquals(name2, parsedValue, "i =  " + i);
+        }
+        for (int i = 1; i <= 100; i++) {
+            final String name1 = fs.toLegalFileName(fs.getNameLengthStrategy().truncate(
+                    "πŸ‘©πŸ»β€πŸ‘¨πŸ»β€πŸ‘¦πŸ»β€πŸ‘¦πŸ»πŸ‘©πŸΌβ€πŸ‘¨πŸΌβ€πŸ‘¦πŸΌβ€πŸ‘¦πŸΌπŸ‘©πŸ½β€πŸ‘¨πŸ½β€πŸ‘¦πŸ½β€πŸ‘¦πŸ½πŸ‘©πŸΎβ€πŸ‘¨πŸΎβ€πŸ‘¦πŸΎβ€πŸ‘¦πŸΎπŸ‘©πŸΏβ€πŸ‘¨πŸΏβ€πŸ‘¦πŸΏβ€πŸ‘¦πŸΏπŸ‘©πŸ»β€πŸ‘¨πŸ»β€πŸ‘¦πŸ»β€πŸ‘¦πŸ»πŸ‘©πŸΌβ€πŸ‘¨πŸΌβ€πŸ‘¦πŸΌβ€πŸ‘¦πŸΌπŸ‘©πŸ½β€πŸ‘¨πŸ½β€πŸ‘¦πŸ½β€πŸ‘¦πŸ½πŸ‘©πŸΎβ€πŸ‘¨πŸΎβ€πŸ‘¦πŸΎβ€πŸ‘¦πŸΎπŸ‘©πŸΏβ€πŸ‘¨πŸΏβ€πŸ‘¦πŸΏβ€πŸ‘¦πŸΏ",
+                    // TODO hack 100: truncate blows up when it can't.
+                    100 + i, charset), '_', charset);
+            assertNotNull(name1);
+            final byte[] name1Bytes = name1.getBytes();
+            final String xmlString1 = toXmlString(name1, charset);
+            final Path path = tempDir.resolve(name1);
+            Files.write(path, xmlString1.getBytes(charset));
+            final String xmlFromPath = parseXmlRootValue(path, charset);
+            assertEquals(name1, xmlFromPath, "i =  " + i);
+            final String name2 = new String(name1Bytes, charset);
+            assertEquals(name1, name2);
+            final String xmlString2 = toXmlString(name2, charset);
+            assertEquals(xmlString1, xmlString2);
+            final String parsedValue = Objects.toString(parseXmlRootValue(xmlString2), "");
+            assertEquals(name1, parsedValue, "i =  " + i);
+            assertEquals(name2, parsedValue, "i =  " + i);
+        }
+    }
+
+    private String toXmlString(final String s, final Charset charset) {
+        return String.format("<?xml version=\"1.0\" encoding=\"%s\"?><data>%s</data>", charset.name(), s);
+    }
+
 }