Skip to content

Commit 511e77e

Browse files
committed
(doc) [IO-484] Fix incorrect FilenameUtils documentation for null bytes
1 parent 73bd880 commit 511e77e

1 file changed

Lines changed: 37 additions & 19 deletions

File tree

src/main/java/org/apache/commons/io/FilenameUtils.java

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ public class FilenameUtils {
194194
*
195195
* @param basePath the base path to attach to, always treated as a path
196196
* @param fullFileNameToAdd the fileName (or path) to attach to the base
197-
* @return the concatenated path, or null if invalid. Null bytes inside string will be removed
197+
* @return the concatenated path, or null if invalid
198+
* @throws IllegalArgumentException if the result path contains null bytes
198199
*/
199200
public static String concat(final String basePath, final String fullFileNameToAdd) {
200201
final int prefix = getPrefixLength(fullFileNameToAdd);
@@ -260,8 +261,10 @@ public static boolean directoryContains(final String canonicalParent, final Stri
260261
* @param fileName the fileName
261262
* @param includeSeparator true to include the end separator
262263
* @return the path
264+
* @throws IllegalArgumentException if the result path contains null bytes
263265
*/
264266
private static String doGetFullPath(final String fileName, final boolean includeSeparator) {
267+
// TODO: Does not (indirectly) call requireNonNullChars in all cases for result
265268
if (fileName == null) {
266269
return null;
267270
}
@@ -291,7 +294,8 @@ private static String doGetFullPath(final String fileName, final boolean include
291294
*
292295
* @param fileName the fileName
293296
* @param separatorAdd 0 to omit the end separator, 1 to return it
294-
* @return the path. Null bytes inside string will be removed
297+
* @return the path
298+
* @throws IllegalArgumentException if the result path contains null bytes
295299
*/
296300
private static String doGetPath(final String fileName, final int separatorAdd) {
297301
if (fileName == null) {
@@ -315,7 +319,8 @@ private static String doGetPath(final String fileName, final int separatorAdd) {
315319
* @param fileName the fileName
316320
* @param separator The separator character to use
317321
* @param keepSeparator true to keep the final separator
318-
* @return the normalized fileName. Null bytes inside string will be removed.
322+
* @return the normalized fileName
323+
* @throws IllegalArgumentException if the fileName contains null bytes
319324
*/
320325
private static String doNormalize(final String fileName, final char separator, final boolean keepSeparator) {
321326
if (fileName == null) {
@@ -447,6 +452,9 @@ public static boolean equals(String fileName1, String fileName2, final boolean n
447452
if (fileName1 == null || fileName2 == null) {
448453
return fileName1 == null && fileName2 == null;
449454
}
455+
// TODO: Should IllegalArgumentException thrown by `normalize` for null bytes be
456+
// handled? (and for example `false` be returned instead); If not, have to mention it
457+
// in javadoc of this method and callers
450458
if (normalized) {
451459
fileName1 = normalize(fileName1);
452460
if (fileName1 == null) {
@@ -565,8 +573,8 @@ private static int getAdsCriticalOffset(final String fileName) {
565573
* </p>
566574
*
567575
* @param fileName the fileName to query, null returns null
568-
* @return the name of the file without the path, or an empty string if none exists. Null bytes inside string
569-
* will be removed
576+
* @return the name of the file without the path, or an empty string if none exists
577+
* @throws IllegalArgumentException if the fileName contains null bytes
570578
*/
571579
public static String getBaseName(final String fileName) {
572580
return removeExtension(getName(fileName));
@@ -610,6 +618,7 @@ public static String getExtension(final String fileName) throws IllegalArgumentE
610618
if (index == NOT_FOUND) {
611619
return EMPTY_STRING;
612620
}
621+
// TODO: Should check for null bytes?
613622
return fileName.substring(index + 1);
614623
}
615624

@@ -639,6 +648,7 @@ public static String getExtension(final String fileName) throws IllegalArgumentE
639648
*
640649
* @param fileName the fileName to query, null returns null
641650
* @return the path of the file, an empty string if none exists, null if invalid
651+
* @throws IllegalArgumentException if the result path contains null bytes
642652
*/
643653
public static String getFullPath(final String fileName) {
644654
return doGetFullPath(fileName, true);
@@ -671,6 +681,7 @@ public static String getFullPath(final String fileName) {
671681
*
672682
* @param fileName the fileName to query, null returns null
673683
* @return the path of the file, an empty string if none exists, null if invalid
684+
* @throws IllegalArgumentException if the result path contains null bytes
674685
*/
675686
public static String getFullPathNoEndSeparator(final String fileName) {
676687
return doGetFullPath(fileName, false);
@@ -693,8 +704,8 @@ public static String getFullPathNoEndSeparator(final String fileName) {
693704
* </p>
694705
*
695706
* @param fileName the fileName to query, null returns null
696-
* @return the name of the file without the path, or an empty string if none exists.
697-
* Null bytes inside string will be removed
707+
* @return the name of the file without the path, or an empty string if none exists
708+
* @throws IllegalArgumentException if the fileName contains null bytes
698709
*/
699710
public static String getName(final String fileName) {
700711
if (fileName == null) {
@@ -726,8 +737,8 @@ public static String getName(final String fileName) {
726737
* </p>
727738
*
728739
* @param fileName the fileName to query, null returns null
729-
* @return the path of the file, an empty string if none exists, null if invalid.
730-
* Null bytes inside string will be removed
740+
* @return the path of the file, an empty string if none exists, null if invalid
741+
* @throws IllegalArgumentException if the result path contains null bytes
731742
*/
732743
public static String getPath(final String fileName) {
733744
return doGetPath(fileName, 1);
@@ -757,8 +768,8 @@ public static String getPath(final String fileName) {
757768
* </p>
758769
*
759770
* @param fileName the fileName to query, null returns null
760-
* @return the path of the file, an empty string if none exists, null if invalid.
761-
* Null bytes inside string will be removed
771+
* @return the path of the file, an empty string if none exists, null if invalid
772+
* @throws IllegalArgumentException if the result path contains null bytes
762773
*/
763774
public static String getPathNoEndSeparator(final String fileName) {
764775
return doGetPath(fileName, 0);
@@ -793,7 +804,8 @@ public static String getPathNoEndSeparator(final String fileName) {
793804
* </p>
794805
*
795806
* @param fileName the fileName to query, null returns null
796-
* @return the prefix of the file, null if invalid. Null bytes inside string will be removed
807+
* @return the prefix of the file, null if invalid
808+
* @throws IllegalArgumentException if the result contains null bytes
797809
*/
798810
public static String getPrefix(final String fileName) {
799811
if (fileName == null) {
@@ -987,7 +999,7 @@ private static boolean isEmpty(final String string) {
987999
* @param fileName the fileName to query, null returns false
9881000
* @param extensions the extensions to check for, null checks for no extension
9891001
* @return true if the fileName is one of the extensions
990-
* @throws java.lang.IllegalArgumentException if the supplied fileName contains null bytes
1002+
* @throws IllegalArgumentException if the fileName contains null bytes
9911003
*/
9921004
public static boolean isExtension(final String fileName, final Collection<String> extensions) {
9931005
if (fileName == null) {
@@ -1017,7 +1029,7 @@ public static boolean isExtension(final String fileName, final Collection<String
10171029
* @param fileName the fileName to query, null returns false
10181030
* @param extension the extension to check for, null or empty checks for no extension
10191031
* @return true if the fileName has the specified extension
1020-
* @throws java.lang.IllegalArgumentException if the supplied fileName contains null bytes
1032+
* @throws IllegalArgumentException if the fileName contains null bytes
10211033
*/
10221034
public static boolean isExtension(final String fileName, final String extension) {
10231035
if (fileName == null) {
@@ -1041,7 +1053,7 @@ public static boolean isExtension(final String fileName, final String extension)
10411053
* @param fileName the fileName to query, null returns false
10421054
* @param extensions the extensions to check for, null checks for no extension
10431055
* @return true if the fileName is one of the extensions
1044-
* @throws java.lang.IllegalArgumentException if the supplied fileName contains null bytes
1056+
* @throws IllegalArgumentException if the fileName contains null bytes
10451057
*/
10461058
public static boolean isExtension(final String fileName, final String... extensions) {
10471059
if (fileName == null) {
@@ -1253,7 +1265,8 @@ private static boolean isValidHostName(final String name) {
12531265
* (Note the file separator returned will be correct for Windows/Unix)
12541266
*
12551267
* @param fileName the fileName to normalize, null returns null
1256-
* @return the normalized fileName, or null if invalid. Null bytes inside string will be removed
1268+
* @return the normalized fileName, or null if invalid
1269+
* @throws IllegalArgumentException if the fileName contains null bytes
12571270
*/
12581271
public static String normalize(final String fileName) {
12591272
return doNormalize(fileName, SYSTEM_NAME_SEPARATOR, true);
@@ -1300,7 +1313,8 @@ public static String normalize(final String fileName) {
13001313
* @param fileName the fileName to normalize, null returns null
13011314
* @param unixSeparator {@code true} if a unix separator should
13021315
* be used or {@code false} if a windows separator should be used.
1303-
* @return the normalized fileName, or null if invalid. Null bytes inside string will be removed
1316+
* @return the normalized fileName, or null if invalid
1317+
* @throws IllegalArgumentException if the fileName contains null bytes
13041318
* @since 2.0
13051319
*/
13061320
public static String normalize(final String fileName, final boolean unixSeparator) {
@@ -1346,7 +1360,8 @@ public static String normalize(final String fileName, final boolean unixSeparato
13461360
* (Note the file separator returned will be correct for Windows/Unix)
13471361
*
13481362
* @param fileName the fileName to normalize, null returns null
1349-
* @return the normalized fileName, or null if invalid. Null bytes inside string will be removed
1363+
* @return the normalized fileName, or null if invalid
1364+
* @throws IllegalArgumentException if the fileName contains null bytes
13501365
*/
13511366
public static String normalizeNoEndSeparator(final String fileName) {
13521367
return doNormalize(fileName, SYSTEM_NAME_SEPARATOR, false);
@@ -1392,7 +1407,8 @@ public static String normalizeNoEndSeparator(final String fileName) {
13921407
* @param fileName the fileName to normalize, null returns null
13931408
* @param unixSeparator {@code true} if a unix separator should
13941409
* be used or {@code false} if a windows separator should be used.
1395-
* @return the normalized fileName, or null if invalid. Null bytes inside string will be removed
1410+
* @return the normalized fileName, or null if invalid
1411+
* @throws IllegalArgumentException if the fileName contains null bytes
13961412
* @since 2.0
13971413
*/
13981414
public static String normalizeNoEndSeparator(final String fileName, final boolean unixSeparator) {
@@ -1415,6 +1431,7 @@ public static String normalizeNoEndSeparator(final String fileName, final boolea
14151431
*
14161432
* @param fileName the fileName to query, null returns null
14171433
* @return the fileName minus the extension
1434+
* @throws IllegalArgumentException if the fileName contains null bytes
14181435
*/
14191436
public static String removeExtension(final String fileName) {
14201437
if (fileName == null) {
@@ -1436,6 +1453,7 @@ public static String removeExtension(final String fileName) {
14361453
*
14371454
* @param path the path to check
14381455
* @return The input
1456+
* @throws IllegalArgumentException if path contains null bytes
14391457
*/
14401458
private static String requireNonNullChars(final String path) {
14411459
if (path.indexOf(0) >= 0) {

0 commit comments

Comments
 (0)