Created
December 5, 2014 18:07
-
-
Save edwardbeckett/daedd61bc34a59e2799f to your computer and use it in GitHub Desktop.
WatcherApp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.concurrent.locks.ReentrantLock; | |
import org.apache.commons.io.monitor.FileAlterationListener; | |
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor; | |
import org.apache.commons.io.monitor.FileAlterationMonitor; | |
import org.apache.commons.io.monitor.FileAlterationObserver; | |
/** | |
* @author Edward Beckett :: <Edward@EdwardBeckett.com> | |
* @since :: 12/5/2014 | |
*/ | |
public class Watcher { | |
private final static String WATCHDIR = "D:\\Edward\\Desktop\\watch"; | |
private final static File dir = new File( WATCHDIR ); | |
private final static ReentrantLock lock = new ReentrantLock(); | |
public static void main( String[] args ) throws Exception { | |
FileAlterationObserver fao = new FileAlterationObserver( dir ); | |
final long interval = 500; | |
FileAlterationMonitor monitor = new FileAlterationMonitor( interval ); | |
FileAlterationListener listener = new FileAlterationListenerAdaptor() { | |
@Override | |
public void onFileCreate( File file ) { | |
try { | |
System.out.println( "File created: " + file.getCanonicalPath() + " checksum: " + calcSum( file ) ); | |
} catch( IOException e ) { | |
e.printStackTrace( System.err ); | |
} | |
} | |
@Override | |
public void onFileDelete( File file ) { | |
try { | |
System.out.println( "File removed: " + file.getCanonicalPath() ); | |
} catch( IOException e ) { | |
e.printStackTrace( System.err ); | |
} | |
} | |
@Override | |
public void onFileChange( File file ) { | |
try { | |
lock.lock(); | |
System.out.println( file.getName() + " changed: " + " checksum: " + calcSum( file ) ); | |
} catch( Exception e ) { | |
e.printStackTrace(); | |
} finally { | |
lock.unlock(); | |
} | |
} | |
}; | |
// Add listeners... | |
fao.addListener( listener ); | |
monitor.addObserver( fao ); | |
monitor.start(); | |
} | |
public static String calcSum( File file ) { | |
byte[] hashBytes = null; | |
try( FileInputStream fis = new FileInputStream( file ) ) { | |
MessageDigest digest = MessageDigest.getInstance( "MD5" ); | |
byte[] buffer = new byte[1024]; | |
int bytesRead = -1; | |
while( ( bytesRead = fis.read( buffer ) ) != -1 ) { | |
digest.update( buffer, 0, bytesRead ); | |
} | |
hashBytes = digest.digest(); | |
} catch( IOException | NoSuchAlgorithmException ex ) { | |
ex.printStackTrace(); | |
} | |
return bytesToHex( hashBytes ); | |
} | |
public static String bytesToHex( byte[] bytes ) { | |
final char[] hexArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; | |
char[] hexChars = new char[bytes.length * 2]; | |
int v; | |
for(int j = 0; j < bytes.length; j++) { | |
v = bytes[j] & 0xFF; | |
hexChars[j * 2] = hexArray[v >>> 4]; | |
hexChars[j * 2 + 1] = hexArray[v & 0x0F]; | |
} | |
return new String( hexChars ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment