Skip to content

Commit d4c5044

Browse files
author
Niall Pemberton
committed
IO-191 - Possible improvements using static analysis - patch from Peter Lawrey
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@736890 13f79535-47bb-0310-9956-ffa450edef68
1 parent e702b9f commit d4c5044

25 files changed

Lines changed: 127 additions & 123 deletions

src/java/org/apache/commons/io/DirectoryWalker.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,7 @@ private void walk(File directory, int depth, Collection<?> results) throws IOExc
359359
if (childFiles == null) {
360360
handleRestricted(directory, childDepth, results);
361361
} else {
362-
for (int i = 0; i < childFiles.length; i++) {
363-
File childFile = childFiles[i];
362+
for (File childFile : childFiles) {
364363
if (childFile.isDirectory()) {
365364
walk(childFile, childDepth, results);
366365
} else {
@@ -585,9 +584,9 @@ public static class CancelException extends IOException {
585584
private static final long serialVersionUID = 1347339620135041008L;
586585

587586
/** The file being processed when the exception was thrown. */
588-
private File file;
587+
private final File file;
589588
/** The file depth when the exception was thrown. */
590-
private int depth = -1;
589+
private final int depth;
591590

592591
/**
593592
* Constructs a <code>CancelException</code> with

src/java/org/apache/commons/io/FileUtils.java

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,11 @@ private static void innerListFiles(Collection<File> files, File directory,
258258
IOFileFilter filter) {
259259
File[] found = directory.listFiles((FileFilter) filter);
260260
if (found != null) {
261-
for (int i = 0; i < found.length; i++) {
262-
if (found[i].isDirectory()) {
263-
innerListFiles(files, found[i], filter);
261+
for (File file : found) {
262+
if (file.isDirectory()) {
263+
innerListFiles(files, file, filter);
264264
} else {
265-
files.add(found[i]);
265+
files.add(file);
266266
}
267267
}
268268
}
@@ -899,8 +899,8 @@ public static void copyDirectory(File srcDir, File destDir,
899899
File[] srcFiles = filter == null ? srcDir.listFiles() : srcDir.listFiles(filter);
900900
if (srcFiles != null && srcFiles.length > 0) {
901901
exclusionList = new ArrayList<String>(srcFiles.length);
902-
for (int i = 0; i < srcFiles.length; i++) {
903-
File copiedFile = new File(destDir, srcFiles[i].getName());
902+
for (File srcFile : srcFiles) {
903+
File copiedFile = new File(destDir, srcFile.getName());
904904
exclusionList.add(copiedFile.getCanonicalPath());
905905
}
906906
}
@@ -941,13 +941,13 @@ private static void doCopyDirectory(File srcDir, File destDir, FileFilter filter
941941
if (files == null) { // null if security restricted
942942
throw new IOException("Failed to list contents of " + srcDir);
943943
}
944-
for (int i = 0; i < files.length; i++) {
945-
File copiedFile = new File(destDir, files[i].getName());
946-
if (exclusionList == null || !exclusionList.contains(files[i].getCanonicalPath())) {
947-
if (files[i].isDirectory()) {
948-
doCopyDirectory(files[i], copiedFile, filter, preserveFileDate, exclusionList);
944+
for (File file : files) {
945+
File copiedFile = new File(destDir, file.getName());
946+
if (exclusionList == null || !exclusionList.contains(file.getCanonicalPath())) {
947+
if (file.isDirectory()) {
948+
doCopyDirectory(file, copiedFile, filter, preserveFileDate, exclusionList);
949949
} else {
950-
doCopyFile(files[i], copiedFile, preserveFileDate);
950+
doCopyFile(file, copiedFile, preserveFileDate);
951951
}
952952
}
953953
}
@@ -1029,12 +1029,12 @@ public static boolean deleteQuietly(File file) {
10291029
if (file.isDirectory()) {
10301030
cleanDirectory(file);
10311031
}
1032-
} catch (Exception e) {
1032+
} catch (Exception ignored) {
10331033
}
10341034

10351035
try {
10361036
return file.delete();
1037-
} catch (Exception e) {
1037+
} catch (Exception ignored) {
10381038
return false;
10391039
}
10401040
}
@@ -1062,8 +1062,7 @@ public static void cleanDirectory(File directory) throws IOException {
10621062
}
10631063

10641064
IOException exception = null;
1065-
for (int i = 0; i < files.length; i++) {
1066-
File file = files[i];
1065+
for (File file : files) {
10671066
try {
10681067
forceDelete(file);
10691068
} catch (IOException ioe) {
@@ -1503,8 +1502,7 @@ private static void cleanDirectoryOnExit(File directory) throws IOException {
15031502
}
15041503

15051504
IOException exception = null;
1506-
for (int i = 0; i < files.length; i++) {
1507-
File file = files[i];
1505+
for (File file : files) {
15081506
try {
15091507
forceDeleteOnExit(file);
15101508
} catch (IOException ioe) {
@@ -1570,9 +1568,7 @@ public static long sizeOfDirectory(File directory) {
15701568
if (files == null) { // null if security restricted
15711569
return 0L;
15721570
}
1573-
for (int i = 0; i < files.length; i++) {
1574-
File file = files[i];
1575-
1571+
for (File file : files) {
15761572
if (file.isDirectory()) {
15771573
size += sizeOfDirectory(file);
15781574
} else {

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.io.File;
2020
import java.util.ArrayList;
2121
import java.util.Collection;
22-
import java.util.Iterator;
2322
import java.util.Stack;
2423

2524
/**
@@ -102,7 +101,7 @@ public class FilenameUtils {
102101
* The extension separator String.
103102
* @since Commons IO 1.4
104103
*/
105-
public static final String EXTENSION_SEPARATOR_STR = (new Character(EXTENSION_SEPARATOR)).toString();
104+
public static final String EXTENSION_SEPARATOR_STR = Character.toString(EXTENSION_SEPARATOR);
106105

107106
/**
108107
* The Unix separator character.
@@ -1135,8 +1134,8 @@ public static boolean isExtension(String filename, String[] extensions) {
11351134
return (indexOfExtension(filename) == -1);
11361135
}
11371136
String fileExt = getExtension(filename);
1138-
for (int i = 0; i < extensions.length; i++) {
1139-
if (fileExt.equals(extensions[i])) {
1137+
for (String extension : extensions) {
1138+
if (fileExt.equals(extension)) {
11401139
return true;
11411140
}
11421141
}
@@ -1162,8 +1161,8 @@ public static boolean isExtension(String filename, Collection<String> extensions
11621161
return (indexOfExtension(filename) == -1);
11631162
}
11641163
String fileExt = getExtension(filename);
1165-
for (Iterator<String> it = extensions.iterator(); it.hasNext();) {
1166-
if (fileExt.equals(it.next())) {
1164+
for (String extension : extensions) {
1165+
if (fileExt.equals(extension)) {
11671166
return true;
11681167
}
11691168
}
@@ -1323,7 +1322,7 @@ static String[] splitOnTokens(String text) {
13231322
// used by wildcardMatch
13241323
// package level so a unit test may run on this
13251324

1326-
if (text.indexOf("?") == -1 && text.indexOf("*") == -1) {
1325+
if (text.indexOf('?') == -1 && text.indexOf('*') == -1) {
13271326
return new String[] { text };
13281327
}
13291328

src/java/org/apache/commons/io/IOUtils.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.nio.channels.Channel;
3333
import java.util.ArrayList;
3434
import java.util.Collection;
35-
import java.util.Iterator;
3635
import java.util.List;
3736

3837
import org.apache.commons.io.output.ByteArrayOutputStream;
@@ -1042,8 +1041,7 @@ public static void writeLines(Collection<?> lines, String lineEnding,
10421041
if (lineEnding == null) {
10431042
lineEnding = LINE_SEPARATOR;
10441043
}
1045-
for (Iterator<?> it = lines.iterator(); it.hasNext(); ) {
1046-
Object line = it.next();
1044+
for (Object line : lines) {
10471045
if (line != null) {
10481046
output.write(line.toString().getBytes());
10491047
}
@@ -1078,8 +1076,7 @@ public static void writeLines(Collection<?> lines, String lineEnding,
10781076
if (lineEnding == null) {
10791077
lineEnding = LINE_SEPARATOR;
10801078
}
1081-
for (Iterator<?> it = lines.iterator(); it.hasNext(); ) {
1082-
Object line = it.next();
1079+
for (Object line : lines) {
10831080
if (line != null) {
10841081
output.write(line.toString().getBytes(encoding));
10851082
}
@@ -1107,8 +1104,7 @@ public static void writeLines(Collection<?> lines, String lineEnding,
11071104
if (lineEnding == null) {
11081105
lineEnding = LINE_SEPARATOR;
11091106
}
1110-
for (Iterator<?> it = lines.iterator(); it.hasNext(); ) {
1111-
Object line = it.next();
1107+
for (Object line : lines) {
11121108
if (line != null) {
11131109
writer.write(line.toString());
11141110
}

src/java/org/apache/commons/io/comparator/AbstractFileComparator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ abstract class AbstractFileComparator implements Comparator<File> {
3333
/**
3434
* Sort an array of files.
3535
* <p>
36-
* This method uses {@link Arrays#sort(File[], Comparator)}
36+
* This method uses {@link Arrays#sort(Object[], Comparator)}
3737
* and returns the original array.
3838
*
3939
* @param files The files to sort, may be null

src/java/org/apache/commons/io/comparator/CompositeFileComparator.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
*/
4545
public class CompositeFileComparator extends AbstractFileComparator implements Serializable {
4646

47+
private static final Comparator[] NO_COMPARATORS = {};
4748
private final Comparator<File>[] delegates;
4849

4950
/**
@@ -52,10 +53,11 @@ public class CompositeFileComparator extends AbstractFileComparator implements S
5253
* @param delegates The delegate file comparators
5354
*/
5455
public CompositeFileComparator(Comparator<File>... delegates) {
55-
int size = (delegates == null ? 0 : delegates.length);
56-
this.delegates = new Comparator[size];
57-
for (int i = 0; i < size; i++) {
58-
this.delegates[i] = delegates[i];
56+
if (delegates == null) {
57+
this.delegates = NO_COMPARATORS;
58+
} else {
59+
this.delegates = new Comparator[delegates.length];
60+
System.arraycopy(delegates, 0, this.delegates, 0, delegates.length);
5961
}
6062
}
6163

@@ -65,13 +67,15 @@ public CompositeFileComparator(Comparator<File>... delegates) {
6567
* @param delegates The delegate file comparators
6668
*/
6769
public CompositeFileComparator(Iterable<Comparator<File>> delegates) {
68-
List<Comparator<File>> list = new ArrayList<Comparator<File>>();
69-
if (delegates != null) {
70+
if (delegates == null) {
71+
this.delegates = NO_COMPARATORS;
72+
} else {
73+
List<Comparator<File>> list = new ArrayList<Comparator<File>>();
7074
for (Comparator<File> comparator : delegates) {
7175
list.add(comparator);
7276
}
77+
this.delegates = list.toArray(new Comparator[list.size()]);
7378
}
74-
this.delegates = list.toArray(new Comparator[list.size()]);
7579
}
7680

7781
/**
@@ -84,8 +88,8 @@ public CompositeFileComparator(Iterable<Comparator<File>> delegates) {
8488
*/
8589
public int compare(File file1, File file2) {
8690
int result = 0;
87-
for (int i = 0; i < delegates.length; i++) {
88-
result = delegates[i].compare(file1, file2);
91+
for (Comparator<File> delegate : delegates) {
92+
result = delegate.compare(file1, file2);
8993
if (result != 0) {
9094
break;
9195
}

src/java/org/apache/commons/io/filefilter/AndFileFilter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class AndFileFilter
3939
implements ConditionalFileFilter, Serializable {
4040

4141
/** The list of file filters. */
42-
private List<IOFileFilter> fileFilters;
42+
private final List<IOFileFilter> fileFilters;
4343

4444
/**
4545
* Constructs a new instance of <code>AndFileFilter</code>.
@@ -76,7 +76,7 @@ public AndFileFilter(IOFileFilter filter1, IOFileFilter filter2) {
7676
if (filter1 == null || filter2 == null) {
7777
throw new IllegalArgumentException("The filters must not be null");
7878
}
79-
this.fileFilters = new ArrayList<IOFileFilter>();
79+
this.fileFilters = new ArrayList<IOFileFilter>(2);
8080
addFileFilter(filter1);
8181
addFileFilter(filter2);
8282
}
@@ -106,7 +106,8 @@ public boolean removeFileFilter(final IOFileFilter ioFileFilter) {
106106
* {@inheritDoc}
107107
*/
108108
public void setFileFilters(final List<IOFileFilter> fileFilters) {
109-
this.fileFilters = new ArrayList<IOFileFilter>(fileFilters);
109+
this.fileFilters.clear();
110+
this.fileFilters.addAll(fileFilters);
110111
}
111112

112113
/**

src/java/org/apache/commons/io/filefilter/NameFileFilter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ public NameFileFilter(List<String> names, IOCase caseSensitivity) {
144144
@Override
145145
public boolean accept(File file) {
146146
String name = file.getName();
147-
for (int i = 0; i < this.names.length; i++) {
148-
if (caseSensitivity.checkEquals(name, names[i])) {
147+
for (String name2 : this.names) {
148+
if (caseSensitivity.checkEquals(name, name2)) {
149149
return true;
150150
}
151151
}
@@ -161,8 +161,8 @@ public boolean accept(File file) {
161161
*/
162162
@Override
163163
public boolean accept(File file, String name) {
164-
for (int i = 0; i < names.length; i++) {
165-
if (caseSensitivity.checkEquals(name, names[i])) {
164+
for (String name2 : names) {
165+
if (caseSensitivity.checkEquals(name, name2)) {
166166
return true;
167167
}
168168
}

src/java/org/apache/commons/io/filefilter/OrFileFilter.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class OrFileFilter
3939
implements ConditionalFileFilter, Serializable {
4040

4141
/** The list of file filters. */
42-
private List<IOFileFilter> fileFilters;
42+
private final List<IOFileFilter> fileFilters;
4343

4444
/**
4545
* Constructs a new instance of <code>OrFileFilter</code>.
@@ -76,7 +76,7 @@ public OrFileFilter(IOFileFilter filter1, IOFileFilter filter2) {
7676
if (filter1 == null || filter2 == null) {
7777
throw new IllegalArgumentException("The filters must not be null");
7878
}
79-
this.fileFilters = new ArrayList<IOFileFilter>();
79+
this.fileFilters = new ArrayList<IOFileFilter>(2);
8080
addFileFilter(filter1);
8181
addFileFilter(filter2);
8282
}
@@ -106,7 +106,8 @@ public boolean removeFileFilter(IOFileFilter ioFileFilter) {
106106
* {@inheritDoc}
107107
*/
108108
public void setFileFilters(final List<IOFileFilter> fileFilters) {
109-
this.fileFilters = fileFilters;
109+
this.fileFilters.clear();
110+
this.fileFilters.addAll(fileFilters);
110111
}
111112

112113
/**

src/java/org/apache/commons/io/filefilter/PrefixFileFilter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ public PrefixFileFilter(List<String> prefixes, IOCase caseSensitivity) {
150150
@Override
151151
public boolean accept(File file) {
152152
String name = file.getName();
153-
for (int i = 0; i < this.prefixes.length; i++) {
154-
if (caseSensitivity.checkStartsWith(name, prefixes[i])) {
153+
for (String prefix : this.prefixes) {
154+
if (caseSensitivity.checkStartsWith(name, prefix)) {
155155
return true;
156156
}
157157
}
@@ -167,8 +167,8 @@ public boolean accept(File file) {
167167
*/
168168
@Override
169169
public boolean accept(File file, String name) {
170-
for (int i = 0; i < prefixes.length; i++) {
171-
if (caseSensitivity.checkStartsWith(name, prefixes[i])) {
170+
for (String prefix : prefixes) {
171+
if (caseSensitivity.checkStartsWith(name, prefix)) {
172172
return true;
173173
}
174174
}

0 commit comments

Comments
 (0)