55import java .io .IOException ;
66import java .security .MessageDigest ;
77import java .security .NoSuchAlgorithmException ;
8+ import java .util .ArrayList ;
89import java .util .Arrays ;
910import java .util .HashMap ;
11+ import java .util .List ;
1012import java .util .Map ;
13+ import java .util .Map .Entry ;
1114import java .util .Stack ;
1215
1316import org .alixia .javalibrary .strings .StringTools ;
@@ -42,10 +45,15 @@ public boolean equals(Object obj) {
4245 return false ;
4346 return true ;
4447 }
48+
49+ @ Override
50+ public String toString () {
51+ return StringTools .toHexString (bytes );
52+ }
4553
4654 }
4755
48- private static final int BUFFER_SIZE = 65535 , STATUS_DELAY_GAP = 1000 ;
56+ private static final int BUFFER_SIZE = 65535 , STATUS_DELAY_GAP = 10000 ;
4957 private static final boolean PRINT_STATUS = true ;
5058
5159 public static void main (String [] args ) throws NoSuchAlgorithmException {
@@ -56,7 +64,7 @@ public static void main(String[] args) throws NoSuchAlgorithmException {
5664 else if (!file .isDirectory ())
5765 System .err .println ("You need to specify a directory to search through!" );
5866 else {
59- Map <Hash , File > hashtable = new HashMap <>();
67+ Map <Hash , List < File > > hashtable = new HashMap <>();
6068 MessageDigest hasher = MessageDigest .getInstance ("SHA-256" );
6169
6270 Stack <File > dirchildren = new Stack <>();
@@ -103,16 +111,26 @@ else if (!file.isDirectory())
103111 byte [] hash = hasher .digest ();
104112 Hash h = new Hash (hash );
105113 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+ }
109120 } catch (IOException e ) {
110121 System .err .println ("An error occurred with OPENING the file: " + f + ".\n \t Error message: "
111122 + e .getMessage ());
112123 }
113124 }
114125 }
115126
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+
116134 }
117135 }
118136
0 commit comments