Skip to content

Instantly share code, notes, and snippets.

@terrywbrady
Last active January 3, 2016 09:29
Show Gist options
  • Save terrywbrady/8442822 to your computer and use it in GitHub Desktop.
Save terrywbrady/8442822 to your computer and use it in GitHub Desktop.
File Analyzer File Test Example
public class NameChecksum extends DefaultFileTest {
public String toString() {
return "Sort By Checksum";
}
public String getShortName(){return "Checksum";}
public String getDescription() {
return "This test reports the checksum for a given filename.\n" +
"The summary report will identify files with the same checksum value.\n" +
"You may select from a number of standard checksum algorithms.";
}
}
public void initFilters() {
initAllFilters();
}
/* from DefaultFileTest.java*/
public void initAllFilters() {
filters.add(new DefaultFileTestFilter());
filters.add(new AVFileTestFilter());
filters.add(new ImageFileTestFilter());
filters.add(new TiffFileTestFilter());
filters.add(new JpegFileTestFilter());
}
public static final String ALGORITHM = "Algorithm";
static enum Algorithm {
MD5("MD5"),
SHA1("SHA-1"),
SHA256("SHA-256"),
SHA384("SHA-384"),
SHA512("SHA-512");
String algorithm;
Algorithm(String s) {algorithm = s;}
MessageDigest getInstance() throws NoSuchAlgorithmException {
return MessageDigest.getInstance(algorithm);
}
}
public NameChecksum(FTDriver dt) {
super(dt);
keymap = new HashMap<String, List<ChecksumStats>>();
this.ftprops.add(new FTPropEnum(dt, this.getClass().getName(), ALGORITHM, "algorithm",
"Checksum Algorithm", Algorithm.values(), Algorithm.MD5));
}
public Stats createStats(String key){
return ChecksumStats.Generator.INSTANCE.create(key);
}
public StatsItemConfig getStatsDetails() {
return ChecksumStats.details;
}
/*from ChecksumStats.java*/
public class ChecksumStats extends Stats {
public static enum DUP {Unique, FirstFound, Duplicate;}
public static enum ChecksumStatsItems implements StatsItemEnum {
Key(StatsItem.makeStringStatsItem("Key", 400)),
Data(StatsItem.makeStatsItem(Object.class, "Data", 300).setInitVal("")),
IsDuplicate(StatsItem.makeEnumStatsItem(YN.class, "Is Duplicate").setInitVal(YN.N)),
DuplicateStat(StatsItem.makeEnumStatsItem(DUP.class, "Duplicate Stat").setInitVal(DUP.Unique)),
MatchCount(StatsItem.makeIntStatsItem("Num of Matches").setInitVal(1));
StatsItem si;
ChecksumStatsItems(StatsItem si) {this.si=si;}
public StatsItem si() {return si;}
}
public static enum Generator implements StatsGenerator {
INSTANCE;
public ChecksumStats create(String key) {return new ChecksumStats(key);}
}
}
public String getKey(File f) {
return getRelPath(f);
}
/*from DefaultFileTest.java*/
public String getRelPath(File f) {
return f.getAbsolutePath().substring(getRoot().getAbsolutePath().length());
}
public boolean isTestable(File f) {
return true;
}
public boolean isTestDirectory() {
return false;
}
public boolean processRoot() {
return false;
}
public boolean isTestFiles() {
return true;
}
public String getChecksum(File f) {
Algorithm algorithm = (Algorithm)getProperty(ALGORITHM);
FileInputStream fis = null;
try {
MessageDigest md = algorithm.getInstance();
fis = new FileInputStream(f);
byte[] dataBytes = new byte[1204];
int nread = 0;
while((nread = fis.read(dataBytes)) != -1){
md.update(dataBytes, 0, nread);
}
byte[] mdbytes = md.digest();
StringBuffer sb = new StringBuffer();
for(int i=0; i<mdbytes.length; i++){
sb.append(Integer.toString((mdbytes[i] & 0xFF) + 0x100, 16).substring(1));
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis!=null)
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public Object fileTest(File f) {
return getChecksum(f);
}
@Override public void init() {
keymap.clear();
}
@Override public void refineResults() {
for(List<ChecksumStats> matches: keymap.values()) {
if (matches.size() == 1) continue;
int count = 0;
for(ChecksumStats match: matches) {
match.setVal(ChecksumStatsItems.IsDuplicate, YN.Y);
if (count == 0) {
match.setVal(ChecksumStatsItems.DuplicateStat, ChecksumStats.DUP.FirstFound);
} else {
match.setVal(ChecksumStatsItems.DuplicateStat, ChecksumStats.DUP.Duplicate);
}
count++;
match.setVal(ChecksumStatsItems.MatchCount, matches.size());
}
}
}
public class ActionRegistry extends Vector<FileTest> {
private static final long serialVersionUID = 1L;
boolean modifyAllowed = true;
public ActionRegistry(FTDriver dt, boolean modifyAllowed) {
this.modifyAllowed = modifyAllowed;
...
add(new NameChecksum(dt));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment