Skip to content

Commit 79c97ab

Browse files
author
Niall Pemberton
committed
Fix some issues identified by FindBugs
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1004077 13f79535-47bb-0310-9956-ffa450edef68
1 parent 218a9e6 commit 79c97ab

8 files changed

Lines changed: 22 additions & 8 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ public static boolean equals(
10811081
boolean normalized, IOCase caseSensitivity) {
10821082

10831083
if (filename1 == null || filename2 == null) {
1084-
return filename1 == filename2;
1084+
return (filename1 == null && filename2 == null);
10851085
}
10861086
if (normalized) {
10871087
filename1 = normalize(filename1);

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ public MagicNumberFileFilter(byte[] magicNumber, long offset) {
209209
throw new IllegalArgumentException("The offset cannot be negative");
210210
}
211211

212-
this.magicNumbers = magicNumber;
212+
this.magicNumbers = new byte[magicNumber.length];
213+
System.arraycopy(magicNumber, 0, this.magicNumbers, 0, magicNumber.length);
213214
this.byteOffset = offset;
214215
}
215216

@@ -237,7 +238,10 @@ public boolean accept(File file) {
237238
byte[] fileBytes = new byte[this.magicNumbers.length];
238239
randomAccessFile = new RandomAccessFile(file, "r");
239240
randomAccessFile.seek(byteOffset);
240-
randomAccessFile.read(fileBytes);
241+
int read = randomAccessFile.read(fileBytes);
242+
if (read != magicNumbers.length) {
243+
return false;
244+
}
241245
return Arrays.equals(this.magicNumbers, fileBytes);
242246
} catch (IOException ioe) {
243247
// Do nothing, fall through and do not accept file

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ public NameFileFilter(String[] names, IOCase caseSensitivity) {
103103
if (names == null) {
104104
throw new IllegalArgumentException("The array of names must not be null");
105105
}
106-
this.names = names;
106+
this.names = new String[names.length];
107+
System.arraycopy(names, 0, this.names, 0, names.length);
107108
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
108109
}
109110

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ public PrefixFileFilter(String[] prefixes, IOCase caseSensitivity) {
108108
if (prefixes == null) {
109109
throw new IllegalArgumentException("The array of prefixes must not be null");
110110
}
111-
this.prefixes = prefixes;
111+
this.prefixes = new String[prefixes.length];
112+
System.arraycopy(prefixes, 0, this.prefixes, 0, prefixes.length);
112113
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
113114
}
114115

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ public SuffixFileFilter(String[] suffixes, IOCase caseSensitivity) {
109109
if (suffixes == null) {
110110
throw new IllegalArgumentException("The array of suffixes must not be null");
111111
}
112-
this.suffixes = suffixes;
112+
this.suffixes = new String[suffixes.length];
113+
System.arraycopy(suffixes, 0, this.suffixes, 0, suffixes.length);
113114
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
114115
}
115116

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ public WildcardFileFilter(String[] wildcards, IOCase caseSensitivity) {
108108
if (wildcards == null) {
109109
throw new IllegalArgumentException("The wildcard array must not be null");
110110
}
111-
this.wildcards = wildcards;
111+
this.wildcards = new String[wildcards.length];
112+
System.arraycopy(wildcards, 0, this.wildcards, 0, wildcards.length);
112113
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
113114
}
114115

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ public WildcardFilter(String[] wildcards) {
7979
if (wildcards == null) {
8080
throw new IllegalArgumentException("The wildcard array must not be null");
8181
}
82-
this.wildcards = wildcards;
82+
this.wildcards = new String[wildcards.length];
83+
System.arraycopy(wildcards, 0, this.wildcards, 0, wildcards.length);
8384
}
8485

8586
/**

src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,7 @@ public void testMagicNumberFileFilterBytes() throws Exception {
942942

943943
File classFileA = new File(getTestDirectory(), "A.class");
944944
File xmlFileB = new File(getTestDirectory(), "B.xml");
945+
File emptyFile = new File(getTestDirectory(), "C.xml");
945946
File dir = new File(getTestDirectory(), "D");
946947
dir.mkdirs();
947948

@@ -951,17 +952,21 @@ public void testMagicNumberFileFilterBytes() throws Exception {
951952
classFileAStream.close();
952953

953954
FileUtils.write(xmlFileB, xmlFileContent);
955+
FileUtils.touch(emptyFile);
954956

955957
IOFileFilter filter = new MagicNumberFileFilter(classFileMagicNumber);
956958

957959
assertFiltering(filter, classFileA, true);
958960
assertFiltering(filter, xmlFileB, false);
961+
assertFiltering(filter, emptyFile, false);
959962
assertFiltering(filter, dir, false);
960963

964+
961965
filter = FileFilterUtils.magicNumberFileFilter(classFileMagicNumber);
962966

963967
assertFiltering(filter, classFileA, true);
964968
assertFiltering(filter, xmlFileB, false);
969+
assertFiltering(filter, emptyFile, false);
965970
assertFiltering(filter, dir, false);
966971
}
967972

0 commit comments

Comments
 (0)