matchedFiles.add(singleFileToProcess);
} else {
throw new CommandExecutionException(String.format("The specified input %s was not found", targetDirectoryOrFile));
}
BinarySignatureIdentifier binarySignatureIdentifier = new BinarySignatureIdentifier();
File fileSignaturesFile = new File(fileSignaturesFileName);
if (!fileSignaturesFile.exists()) {
throw new CommandExecutionException("Signature file not found");
}
binarySignatureIdentifier.setSignatureFile(fileSignaturesFileName);
try {
binarySignatureIdentifier.init();
} catch (SignatureParseException e) {
throw new CommandExecutionException("Can't parse signature file");
}
binarySignatureIdentifier.setMaxBytesToScan(maxBytesToScan);
String path = fileSignaturesFile.getAbsolutePath();
String slash = path.contains(FORWARD_SLASH) ? FORWARD_SLASH : BACKWARD_SLASH;
String slash1 = slash;
ContainerSignatureDefinitions containerSignatureDefinitions = null;
if (containerSignaturesFileName != null) {
File containerSignaturesFile = new File(containerSignaturesFileName);
InputStream in = null;
if (!containerSignaturesFile.exists()) {
throw new CommandExecutionException("Container signature file not found");
}
try {
in = new FileInputStream(containerSignaturesFileName);
final ContainerSignatureSaxParser parser = new ContainerSignatureSaxParser();
containerSignatureDefinitions = parser.parse(in);
} catch (SignatureParseException e) {
throw new CommandExecutionException("Can't parse container signature file");
} catch (IOException ioe) {
throw new CommandExecutionException(ioe);
} catch (JAXBException jaxbe) {
throw new CommandExecutionException(jaxbe);
}finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
throw new CommandExecutionException("Error closing InputStream on signature file");
}
}
}
}
path = "";
ResultPrinter resultPrinter =
new ResultPrinter(binarySignatureIdentifier, containerSignatureDefinitions,
path, slash, slash1, archives);
String fileName = null;
for (File file : matchedFiles) {
try {
fileName = file.getCanonicalPath();
} catch (IOException e) {
throw new CommandExecutionException(e);
}
URI uri = file.toURI();
RequestMetaData metaData =
new RequestMetaData(file.length(), file.lastModified(), fileName);
RequestIdentifier identifier = new RequestIdentifier(uri);
identifier.setParentId(1L);
InputStream in = null;
IdentificationRequest request = new FileSystemIdentificationRequest(metaData, identifier);
try {
in = new FileInputStream(file);
request.open(in);
IdentificationResultCollection results =
binarySignatureIdentifier.matchBinarySignatures(request);
resultPrinter.print(results, request);
} catch (FileNotFoundException fnfe) {
log.error("error processing files", fnfe);
throw new CommandExecutionException(fnfe);