Question
Need help with the following java code calculating a checksum through a terminal screen: How do I incorporate the following code to read an input
Need help with the following java code calculating a checksum through a terminal screen:
How do I incorporate the following code to read an input file with non-letter characters?
The following input file:
The highest forms of understanding we can achieve are laughter and human compassion. Richard Feynman
Output should be:
The highest forms of understanding we can achieve are laughter and human compass ion. Richard Feynman
8 bit checksum is 38 for all 106 chars
The highest forms of understanding we can achieve are laughter and human compass ion. Richard Feynman
16 bit checksum is 9aaf for all 106 chars
The highest forms of understanding we can achieve are laughter and human compass ion. Richard Feynman XX 32 bit checksum is 749e7e72 for all 108 chars
The following code I have so far:
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
public class checksum {
public static void main(String[] args) {
int checkSumSize = 0, characterCount = 0, checkSumResult = 0;
String input = null;
byte[] fileBytes;
if (args.length < 2 || !Character.isDigit(args[1].charAt(0)) || !validBitSize(Integer.parseInt(args[1]))) {
if (!Character.isDigit(args[1].charAt(0)) || !validBitSize(Integer.parseInt(args[1])))
System.err.print(" Valid checksum sizes are 8, 16, or 32 ");
else
System.err.println(" Please provide the proper parameters. " +
"First Parameter is the input file name, second is the size of the checksum. ");
System.exit(1);
}
try {
input = readFileAsString(args[0]);
} catch (Exception e) {
e.printStackTrace();
}
if (input != null){
switch (Integer.parseInt(args[1])) {
case 8:
checkSumSize = 8;
fileBytes = getAdjustedByteArray(input, checkSumSize);
characterCount = fileBytes.length;
checkSumResult = checksum8(fileBytes);
break;
case 16:
checkSumSize = 16;
fileBytes = getAdjustedByteArray(input, checkSumSize);
characterCount = fileBytes.length;
checkSumResult = checksum16(fileBytes);
break;
case 32:
checkSumSize = 32;
fileBytes = getAdjustedByteArray(input, checkSumSize);
characterCount = fileBytes.length;
checkSumResult = checksum32(fileBytes);
break;
default:
System.err.print("Valid checksum sizes are 8, 16, or 32 ");
System.exit(1);
break;
}
System.out.printf(" %s %2d bit checksum is %8x for all %4d chars ",
formattedStringOutput(getAdjustedString(input, checkSumSize)), checkSumSize, checkSumResult, characterCount);
}
}
// Checksum 8-Bit
private static int checksum8(byte[] data) {
int check = 0;
for (byte b : data)
check += b;
return check & 0xFF;
}
// Checksum 16-bit
private static int checksum16(byte[] data) {
int check = 0;
for (int i = 0; i <= data.length - 2; i += 2)
check += ((data[i] << 8) | (data[i + 1] & 0xFF));
return check & 0xFFFF;
}
// Checksum 32-bit
private static int checksum32(byte[] data) {
int check = 0;
for (int i = 0; i < data.length; i += 4)
check += ((data[i] << 24) | (data[i + 1] << 16) | (data[i + 2] << 8) | (data[i + 3])) & 0xffffffffL;
return check;
}
private static String readFileAsString(String fileName) throws Exception {
String data;
data = new String(Files.readAllBytes(Paths.get(fileName)));
return data;
}
private static String formattedStringOutput(String output) {
StringBuilder res = new StringBuilder();
for (int i = 0; i < output.length(); i++) {
if (i > 0 && i % 80 == 0)
res.append(" ");
res.append(output.charAt(i));
}
return res.toString();
}
private static byte[] getAdjustedByteArray(String in, int bit) {
int originalSize = in.getBytes().length, newSize;
newSize = originalSize + getPadding(originalSize, bit);
byte[] temp = new byte[newSize];
for (int i = 0; i < originalSize; i++) {
temp[i] = (byte) in.charAt(i);
}
if (getPadding(originalSize, bit) > 0) {
for (int j = originalSize; j < newSize; j++) {
temp[j] = 88;
}
}
return temp;
}
private static String getAdjustedString(String in, int bit) {
int originalSize = in.getBytes().length, newSize;
StringBuilder builder = new StringBuilder();
newSize = originalSize + getPadding(originalSize, bit);
for (int i = 0; i < originalSize; i++) {
builder.append(in.charAt(i));
}
if (getPadding(originalSize, bit) > 0) {
for (int j = originalSize; j < newSize; j++) {
builder.append("X");
}
}
return builder.toString();
}
private static boolean validBitSize(int bit) {
int[] validBits = {8, 16, 32};
return Arrays.stream(validBits).anyMatch(i -> i == bit);
}
private static int getPadding(int lengthOriginal, int bit) {
int a = lengthOriginal;
int b = bit == 32 ? 4 : 2;
int c = 0;
while (a % b != 0) {
a = a + 1;
c++;
}
return bit > 8 ? c : 0;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started