Skip to content

Commit ee8f914

Browse files
🔖 Comments added
1 parent 77f4938 commit ee8f914

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/main/java/org/rrajesh1979/demo/MyCheckSum.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@
2626
import lombok.extern.slf4j.Slf4j;
2727
import picocli.CommandLine;
2828

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

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

4557
}
4658

59+
/**
60+
* Function invoked by picocli to calculate the checksum of a file.
61+
* @return Integer 0 if successful.
62+
* @throws Exception if the file is not found. or if the algorithm is not found.
63+
*/
4764
@Override
4865
public Integer call() throws Exception {
4966
String checkSum = getCheckSum(file);
5067
log.info("Checksum of file {}, is : {}", file.getName(), checkSum);
5168
return 0;
5269
}
5370

71+
/**
72+
* Function to calculate the checksum of a file.
73+
*
74+
* @param file the file whose checksum to calculate
75+
* @return String
76+
* @throws IOException if the file is not found.
77+
* @throws NoSuchAlgorithmException if the algorithm is not found.
78+
*/
5479
public String getCheckSum(File file) throws IOException, NoSuchAlgorithmException {
5580
byte[] fileContents = Files.readAllBytes(file.toPath());
5681
byte[] digest = MessageDigest.getInstance(algorithm).digest(fileContents);

0 commit comments

Comments
 (0)