5
5
import java .io .IOException ;
6
6
import java .security .MessageDigest ;
7
7
import java .security .NoSuchAlgorithmException ;
8
+ import java .util .ArrayList ;
8
9
import java .util .Arrays ;
9
10
import java .util .HashMap ;
11
+ import java .util .List ;
10
12
import java .util .Map ;
13
+ import java .util .Map .Entry ;
11
14
import java .util .Stack ;
12
15
13
16
import org .alixia .javalibrary .strings .StringTools ;
@@ -42,10 +45,15 @@ public boolean equals(Object obj) {
42
45
return false ;
43
46
return true ;
44
47
}
48
+
49
+ @ Override
50
+ public String toString () {
51
+ return StringTools .toHexString (bytes );
52
+ }
45
53
46
54
}
47
55
48
- private static final int BUFFER_SIZE = 65535 , STATUS_DELAY_GAP = 1000 ;
56
+ private static final int BUFFER_SIZE = 65535 , STATUS_DELAY_GAP = 10000 ;
49
57
private static final boolean PRINT_STATUS = true ;
50
58
51
59
public static void main (String [] args ) throws NoSuchAlgorithmException {
@@ -56,7 +64,7 @@ public static void main(String[] args) throws NoSuchAlgorithmException {
56
64
else if (!file .isDirectory ())
57
65
System .err .println ("You need to specify a directory to search through!" );
58
66
else {
59
- Map <Hash , File > hashtable = new HashMap <>();
67
+ Map <Hash , List < File > > hashtable = new HashMap <>();
60
68
MessageDigest hasher = MessageDigest .getInstance ("SHA-256" );
61
69
62
70
Stack <File > dirchildren = new Stack <>();
@@ -103,16 +111,26 @@ else if (!file.isDirectory())
103
111
byte [] hash = hasher .digest ();
104
112
Hash h = new Hash (hash );
105
113
if (hashtable .containsKey (h ))
106
- System .out .println ("Duplicate between files: \n \t " + hashtable .get (h ) + "\n \t " + f );
107
- else
108
- hashtable .put (h , f );
114
+ hashtable .get (h ).add (f );
115
+ else {
116
+ List <File > files = new ArrayList <>(1 );
117
+ hashtable .put (h , files );
118
+ files .add (f );
119
+ }
109
120
} catch (IOException e ) {
110
121
System .err .println ("An error occurred with OPENING the file: " + f + ".\n \t Error message: "
111
122
+ e .getMessage ());
112
123
}
113
124
}
114
125
}
115
126
127
+ for (Entry <Hash , List <File >> e : hashtable .entrySet ())
128
+ if (e .getValue ().size () != 1 ) {
129
+ System .out .println ("Files with hash " + e .getKey () + ':' );
130
+ for (File f : e .getValue ())
131
+ System .out .println ("\t " + f );
132
+ }
133
+
116
134
}
117
135
}
118
136
0 commit comments