Write a program that calculates the CRC-16 value for a given file and also verif
ID: 3545552 • Letter: W
Question
Write a program that calculates the CRC-16 value for a given file and also verifies the correctness of a given file that already has CRC-16 value appended at the end, as more fully described below.
Use the CRC polynomial x^(16) + x^(11) + x^(8) + x^(7) + x^(3) + x^(2) + 1.
Program operation:
1. The program must compile from the command line.
2. The program executable file name must be
Explanation / Answer
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import java.util.zip.CRC32;
public class CalculateCRC {
public static long generateCRC(String filepath) throws IOException {
InputStream inputStreamn = new FileInputStream(filepath);
CRC32 crc = new CRC32();
int cnt;
while ((cnt = inputStreamn.read()) != -1) {
crc.update(cnt);
}
return crc.getValue();
}
public static void main(String[] args) throws IOException {
System.out.println(args.length);
if(args.length == 2){
if(args[0].equalsIgnoreCase("c")){
System.out.println("The CRC is : "+generateCRC(args[1]));
}else{
System.out.print("Enter the CRC of the file : "+args[1]);
Scanner input = new Scanner(System.in);
long verify = input.nextLong();
if(verify == generateCRC(args[1])){
System.out.println("CRC Verified");
}else{
System.out.println("CRC Verification Failed!");
}
}
}
else{
System.out.println("java CalculateCRC <'c' for calculating and 'v' for verifying> <file path>");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.