Skip to content

Commit cdef9c2

Browse files
author
Gary Gregory
committed
In-line some single use variables.
- Let requireNonNullChars() be used like Objects.requireNonNull(). - Format tweaks. - Refactor commons code. - No need to search some strings twice in separatorsTo* - Javadoc close HTML tags.
1 parent 4f966c5 commit cdef9c2

2 files changed

Lines changed: 32 additions & 28 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ private static boolean isOsNameMatch(final String osName, final String osNamePre
218218
private final String[] reservedFileNames;
219219
private final boolean supportsDriveLetter;
220220
private final char nameSeparator;
221+
private final char nameSeparatorOther;
221222

222223
/**
223224
* Constructs a new instance.
@@ -242,6 +243,7 @@ private static boolean isOsNameMatch(final String osName, final String osNamePre
242243
this.casePreserving = casePreserving;
243244
this.supportsDriveLetter = supportsDriveLetter;
244245
this.nameSeparator = nameSeparator;
246+
this.nameSeparatorOther = FilenameUtils.flipSeparator(nameSeparator);
245247
}
246248

247249
/**

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

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -121,24 +121,16 @@ public class FilenameUtils {
121121
/**
122122
* The separator character that is the opposite of the system separator.
123123
*/
124-
private static final char OTHER_SEPARATOR;
124+
private static final char OTHER_SEPARATOR = flipSeparator(SYSTEM_NAME_SEPARATOR);
125125

126-
static {
127-
if (isSystemWindows()) {
128-
OTHER_SEPARATOR = UNIX_NAME_SEPARATOR;
129-
} else {
130-
OTHER_SEPARATOR = WINDOWS_NAME_SEPARATOR;
131-
}
132-
}
133-
134-
private static final Pattern IPV4_PATTERN =
135-
Pattern.compile("^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$");
126+
private static final Pattern IPV4_PATTERN = Pattern.compile("^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$");
136127

137128
private static final int IPV4_MAX_OCTET_VALUE = 255;
138129

139130
private static final int IPV6_MAX_HEX_GROUPS = 8;
140131

141132
private static final int IPV6_MAX_HEX_DIGITS_PER_GROUP = 4;
133+
142134
private static final int MAX_UNSIGNED_SHORT = 0xffff;
143135

144136
private static final int BASE_16 = 16;
@@ -245,7 +237,7 @@ public static boolean directoryContains(final String canonicalParent, final Stri
245237
return false;
246238
}
247239

248-
final char separator = canonicalParent.charAt(0) == UNIX_NAME_SEPARATOR ? UNIX_NAME_SEPARATOR : WINDOWS_NAME_SEPARATOR;
240+
final char separator = toSeparator(canonicalParent.charAt(0) == UNIX_NAME_SEPARATOR);
249241
final String parentWithEndSeparator = canonicalParent.charAt(canonicalParent.length() - 1) == separator ? canonicalParent : canonicalParent + separator;
250242

251243
return IOCase.SYSTEM.checkStartsWith(canonicalChild, parentWithEndSeparator);
@@ -299,13 +291,11 @@ private static String doGetPath(final String fileName, final int separatorAdd) {
299291
return null;
300292
}
301293
final int index = indexOfLastSeparator(fileName);
302-
final int endIndex = index+separatorAdd;
294+
final int endIndex = index + separatorAdd;
303295
if (prefix >= fileName.length() || index < 0 || prefix >= endIndex) {
304296
return EMPTY_STRING;
305297
}
306-
final String path = fileName.substring(prefix, endIndex);
307-
requireNonNullChars(path);
308-
return path;
298+
return requireNonNullChars(fileName.substring(prefix, endIndex));
309299
}
310300

311301
/**
@@ -440,9 +430,7 @@ public static boolean equals(final String fileName1, final String fileName2) {
440430
* @return true if the fileNames are equal, null equals null
441431
* @since 1.3
442432
*/
443-
public static boolean equals(
444-
String fileName1, String fileName2,
445-
final boolean normalized, IOCase caseSensitivity) {
433+
public static boolean equals(String fileName1, String fileName2, final boolean normalized, IOCase caseSensitivity) {
446434

447435
if (fileName1 == null || fileName2 == null) {
448436
return fileName1 == null && fileName2 == null;
@@ -674,9 +662,7 @@ public static String getName(final String fileName) {
674662
if (fileName == null) {
675663
return null;
676664
}
677-
requireNonNullChars(fileName);
678-
final int index = indexOfLastSeparator(fileName);
679-
return fileName.substring(index + 1);
665+
return requireNonNullChars(fileName).substring(indexOfLastSeparator(fileName) + 1);
680666
}
681667

682668
/**
@@ -775,9 +761,7 @@ public static String getPrefix(final String fileName) {
775761
requireNonNullChars(fileName);
776762
return fileName + UNIX_NAME_SEPARATOR;
777763
}
778-
final String path = fileName.substring(0, len);
779-
requireNonNullChars(path);
780-
return path;
764+
return requireNonNullChars(fileName.substring(0, len));
781765
}
782766

783767
/**
@@ -994,8 +978,7 @@ public static boolean isExtension(final String fileName, final String extension)
994978
if (isEmpty(extension)) {
995979
return indexOfExtension(fileName) == NOT_FOUND;
996980
}
997-
final String fileExt = getExtension(fileName);
998-
return fileExt.equals(extension);
981+
return getExtension(fileName).equals(extension);
999982
}
1000983

1001984
/**
@@ -1157,6 +1140,22 @@ private static boolean isSeparator(final char ch) {
11571140
return ch == UNIX_NAME_SEPARATOR || ch == WINDOWS_NAME_SEPARATOR;
11581141
}
11591142

1143+
/**
1144+
* Flips the Windows name separator to Linux and vice-versa.
1145+
*
1146+
* @param ch The Windows or Linux name separator.
1147+
* @return The Windows or Linux name separator.
1148+
*/
1149+
static char flipSeparator(final char ch) {
1150+
if (ch == UNIX_NAME_SEPARATOR) {
1151+
return WINDOWS_NAME_SEPARATOR;
1152+
}
1153+
if (ch == WINDOWS_NAME_SEPARATOR) {
1154+
return UNIX_NAME_SEPARATOR;
1155+
}
1156+
throw new IllegalArgumentException(String.valueOf(ch));
1157+
}
1158+
11601159
/**
11611160
* Determines if Windows file system is in use.
11621161
*
@@ -1402,12 +1401,14 @@ public static String removeExtension(final String fileName) {
14021401
* This may be used for poison byte attacks.
14031402
*
14041403
* @param path the path to check
1404+
* @return The input
14051405
*/
1406-
private static void requireNonNullChars(final String path) {
1406+
private static String requireNonNullChars(final String path) {
14071407
if (path.indexOf(0) >= 0) {
14081408
throw new IllegalArgumentException(
14091409
"Null byte present in file/path name. There are no known legitimate use cases for such data, but several injection attacks may use it");
14101410
}
1411+
return path;
14111412
}
14121413
/**
14131414
* Converts all separators to the system separator.
@@ -1447,6 +1448,7 @@ public static String separatorsToWindows(final String path) {
14471448
}
14481449
return path.replace(UNIX_NAME_SEPARATOR, WINDOWS_NAME_SEPARATOR);
14491450
}
1451+
14501452
/**
14511453
* Splits a string into a number of tokens.
14521454
* The text is split by '?' and '*'.

0 commit comments

Comments
 (0)