Skip to content

Commit

Permalink
🔖 Comments added
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Jan 5, 2022
1 parent 77f4938 commit ee8f914
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/main/java/org/rrajesh1979/demo/MyCheckSum.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@
import lombok.extern.slf4j.Slf4j;
import picocli.CommandLine;

@CommandLine.Command(name = "mychecksum", mixinStandardHelpOptions = true, version = "mychecksum 0.0.8", description = "Prints the checksum (MD5 by default) of a file to STDOUT.")
/**
* @author rrajesh1979
* @version 0.0.9
* @since 2021-Jan-01
*/
@CommandLine.Command(name = "mychecksum", mixinStandardHelpOptions = true, version = "mychecksum 0.0.9", description = "Prints the checksum (MD5 by default) of a file to STDOUT.")
@Slf4j
public class MyCheckSum implements Callable<Integer> {
@CommandLine.Parameters(index = "0", description = "The file whose checksum to calculate.")
Expand All @@ -35,6 +40,13 @@ public class MyCheckSum implements Callable<Integer> {
@CommandLine.Option(names = { "-a", "--algorithm" }, description = "MD5, SHA-1, SHA-256, ...")
private String algorithm = "MD5";

/**
* Main function to invoke the picocli framework.
* @param args the command line arguments.
* The first argument is the file whose checksum to calculate.
* The second argument is the algorithm to use.
* The default algorithm is MD5.
*/
public static void main(String[] args) {
int exitCode;
log.info("Hello from MyCheckSum");
Expand All @@ -44,13 +56,26 @@ public static void main(String[] args) {

}

/**
* Function invoked by picocli to calculate the checksum of a file.
* @return Integer 0 if successful.
* @throws Exception if the file is not found. or if the algorithm is not found.
*/
@Override
public Integer call() throws Exception {
String checkSum = getCheckSum(file);
log.info("Checksum of file {}, is : {}", file.getName(), checkSum);
return 0;
}

/**
* Function to calculate the checksum of a file.
*
* @param file the file whose checksum to calculate
* @return String
* @throws IOException if the file is not found.
* @throws NoSuchAlgorithmException if the algorithm is not found.
*/
public String getCheckSum(File file) throws IOException, NoSuchAlgorithmException {
byte[] fileContents = Files.readAllBytes(file.toPath());
byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);
Expand Down

0 comments on commit ee8f914

Please sign in to comment.