FileKit.java

Download FileKit.java

 1: // FileKit is intended to be a collection of generally useful methods
 2: // for working with files.  All it does now is MD5 digests on files.
 3: // A test driver (main) is included to test the various methods.  Usage:
 4: //    java FileKit filename
 5: // (todo: add GUI File selection interface.)
 6: //
 7: // Written 10/2002 by Wayne Pollock, Tampa FL USA.  All Rights Reserved.
 8: 
 9: // package com.wpollock.utils;
10: 
11: import java.io.*;
12: import java.security.*;
13: 
14: public class FileKit
15: {
16:    // A lookup is more efficient than using Integer.toHexDigit():
17:    private static final char [] hexDigit =
18:       { '0', '1', '2', '3', '4', '5', '6', '7',
19:         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
20: 
21:    public static String md5Digest ( String filename ) throws IOException
22:    {  return md5Digest( new FileInputStream( filename ) );  }
23: 
24:    public static String md5Digest ( File file ) throws IOException
25:    {  return md5Digest( new FileInputStream( file ) );  }
26: 
27:    public static String md5Digest ( FileInputStream fis ) throws IOException
28:    {
29:       byte [] buffer = new byte[1024];
30:       MessageDigest md = null;
31: 
32:       try
33:       {   md = MessageDigest.getInstance( "MD5" );
34:       } catch ( NoSuchAlgorithmException nsae )
35:       {   System.out.println( nsae ); System.exit( 1 );  }
36: 
37:       for ( ; ; )  // Do forever
38:       {  int numOfBytesRead = fis.read( buffer );
39:          if ( numOfBytesRead < 1 )
40:             break;
41:          md.update( buffer, 0, numOfBytesRead );
42:       }
43:       fis.close();
44:       return hexString( md.digest() );
45:    }
46: 
47:    private static String hexString ( byte [] bytes )
48:    {
49:       // Converts an array of bytes to a String of hex digits,
50:       // with a space between groups of four digits.
51:       // Note that the MD5 digest is always 128 bits = 16 bytes,
52:       // so there will be 5 * 16 / 2 = 40 (32 hex digits plus
53:       // 7 spaces = 39 characters, which is close enough).
54: 
55:       StringBuffer s = new StringBuffer( 5 * bytes.length / 2 );
56: 
57:       for ( int i = 0; i < bytes.length; ++i )
58:       {  int d1 = ( bytes[i] & 0x0000000F );
59:          int d2 = ( bytes[i] & 0x000000F0 ) >>> 4;
60: 
61:          // Output a space seperator after every 4th hex digit:
62:          if ( i % 2 == 0 && i != 0 )
63:             s.append( ' ' );
64: 
65:          s.append( hexDigit[d2] );
66:          s.append( hexDigit[d1] );
67:       }
68: 
69:       return s.toString();
70:    }
71: 
72:    // Test driver for md5
73:    public static void main ( String [] args ) throws Exception
74:    {
75:       if ( args.length == 0 )
76:       {  System.out.println( "Usage: java FileKit filename" );
77:          System.out.println( "       will display the MD5 digest "
78:              + "(or \"fingerprint\") for the named file." );
79:          System.exit( 0 );
80:       }
81: 
82:       System.out.println( "File:       " + args[0] );
83:       System.out.println( "MD5 Digest: " + md5Digest( args[0] ) );
84:    }
85: }