@Test public void normalizeWithReservedChar() throws Exception { final String[] TEST_NAMES = { "test?.txt", "?test.txt", "test.txt?", "?test?txt?" }; final String[] EXPECTED_NAMES = { "test%3F.txt", "%3Ftest.txt", "test.txt%3F", "%3Ftest%3Ftxt%3F" }; for (int i=0; i<TEST_NAMES.length; ++i) { assertEquals(EXPECTED_NAMES[i], FilenameUtils.normalize(TEST_NAMES[i])); } }
public static String normalize(final String name) { if (name == null) { throw new IllegalArgumentException("name cannot be null"); } StringBuilder sb = new StringBuilder(); for (char c: name.toCharArray()) { if (RESERVED.contains(c)) { sb.append('%').append((c<16) ? "0" : "").append(Integer.toHexString(c).toUpperCase(Locale.ROOT)); } else { sb.append(c); } } return sb.toString(); }
FilenameUtils { public static String normalize(final String name) { if (name == null) { throw new IllegalArgumentException("name cannot be null"); } StringBuilder sb = new StringBuilder(); for (char c: name.toCharArray()) { if (RESERVED.contains(c)) { sb.append('%').append((c<16) ? "0" : "").append(Integer.toHexString(c).toUpperCase(Locale.ROOT)); } else { sb.append(c); } } return sb.toString(); } }
FilenameUtils { public static String normalize(final String name) { if (name == null) { throw new IllegalArgumentException("name cannot be null"); } StringBuilder sb = new StringBuilder(); for (char c: name.toCharArray()) { if (RESERVED.contains(c)) { sb.append('%').append((c<16) ? "0" : "").append(Integer.toHexString(c).toUpperCase(Locale.ROOT)); } else { sb.append(c); } } return sb.toString(); }  }
FilenameUtils { public static String normalize(final String name) { if (name == null) { throw new IllegalArgumentException("name cannot be null"); } StringBuilder sb = new StringBuilder(); for (char c: name.toCharArray()) { if (RESERVED.contains(c)) { sb.append('%').append((c<16) ? "0" : "").append(Integer.toHexString(c).toUpperCase(Locale.ROOT)); } else { sb.append(c); } } return sb.toString(); }  static String normalize(final String name); static String getName(final String path); }
FilenameUtils { public static String normalize(final String name) { if (name == null) { throw new IllegalArgumentException("name cannot be null"); } StringBuilder sb = new StringBuilder(); for (char c: name.toCharArray()) { if (RESERVED.contains(c)) { sb.append('%').append((c<16) ? "0" : "").append(Integer.toHexString(c).toUpperCase(Locale.ROOT)); } else { sb.append(c); } } return sb.toString(); }  static String normalize(final String name); static String getName(final String path); final static char[] RESERVED_FILENAME_CHARACTERS; }