1
+ import sun .plugin .dom .exception .InvalidStateException ;
2
+
3
+ import java .io .IOException ;
1
4
import java .nio .file .Files ;
5
+ import java .nio .file .Path ;
2
6
import java .nio .file .Paths ;
3
7
import java .util .Arrays ;
4
8
9
13
* Description: Program that calculates a checksum based on bytes.
10
14
* Every 1 byte for 8 bit, every two for 16 bit, and every 4 for 32 bit.
11
15
*/
16
+ public class Checksum {
12
17
13
- public class checksum {
18
+ private static final int [] VALID_BIT_SIZES = { 8 , 16 , 32 };
14
19
15
20
public static void main (String [] args ) {
16
- int checkSumSize = 0 , characterCount = 0 , checkSumResult = 0 ;
17
- String input = null ;
18
- byte [] fileBytes ;
19
-
20
- if (args .length < 2 || !Character .isDigit (args [1 ].charAt (0 )) || !validBitSize (Integer .parseInt (args [1 ]))) {
21
- if (!Character .isDigit (args [1 ].charAt (0 )) || !validBitSize (Integer .parseInt (args [1 ])))
22
- System .err .print ("\n Valid checksum sizes are 8, 16, or 32\n " );
23
- else
24
- System .err .println ("\n Please provide the proper parameters.\n " +
25
- "First Parameter is the input file name, second is the size of the checksum.\n " );
21
+ if (args .length != 2 ) {
22
+ System .err .println ("\n Please provide the proper parameters.\n " +
23
+ "First Parameter is the input file name, second is the size of the checksum.\n " );
24
+ System .exit (1 );
25
+ }
26
+
27
+ Path filePath = Paths .get (args [0 ]);
28
+
29
+ if (!Files .exists (filePath )) {
30
+ System .err .print ("\n File " + args [0 ] + " does not exist" );
31
+ System .exit (1 );
32
+ }
33
+
34
+ int checkSumSize ;
35
+ try {
36
+ checkSumSize = Integer .parseInt (args [1 ]);
37
+ } catch (NumberFormatException e ) {
38
+ System .err .print ("\n Valid checksum sizes are 8, 16, or 32\n " );
26
39
System .exit (1 );
40
+ return ;
27
41
}
28
42
43
+ if (Arrays .stream (VALID_BIT_SIZES ).noneMatch (validBitSize -> validBitSize == checkSumSize )) {
44
+ System .err .print ("\n Valid checksum sizes are 8, 16, or 32\n " );
45
+ System .exit (1 );
46
+ }
47
+
48
+ String input = null ;
29
49
try {
30
- input = readFileAsString ( args [ 0 ] );
31
- } catch (Exception e ) {
50
+ input = new String ( Files . readAllBytes ( filePath ) );
51
+ } catch (IOException e ) {
32
52
e .printStackTrace ();
53
+ System .exit (1 );
54
+ }
55
+
56
+ byte [] adjustedBytes = getAdjustedByteArray (input , checkSumSize );
57
+ int checksum = checksum (adjustedBytes , checkSumSize );
58
+ System .out .printf ("\n %s\n %2d bit checksum is %8x for all %4d chars\n " ,
59
+ formattedStringOutput (getAdjustedString (input , checkSumSize )), checkSumSize , checksum , adjustedBytes .length );
60
+
61
+ }
62
+
63
+ /**
64
+ * @param in - input text to grab characters from to convert to byte.
65
+ * @param bit - bit size we are using. bit > 8 ? sum every 2 chars : sum each chars
66
+ * @return byte[] with appropriate padding if applicable. Padding with X (88 ASCII)
67
+ */
68
+ private static byte [] getAdjustedByteArray (String in , int bit ) {
69
+ int originalSize = in .getBytes ().length , newSize ;
70
+
71
+ newSize = originalSize + getPadding (originalSize , bit );
72
+ byte [] temp = new byte [newSize ];
73
+
74
+ for (int i = 0 ; i < originalSize ; i ++) {
75
+ temp [i ] = (byte ) in .charAt (i );
33
76
}
34
77
35
- if (input != null ){
36
- switch (Integer .parseInt (args [1 ])) {
37
- case 8 :
38
- checkSumSize = 8 ;
39
- fileBytes = getAdjustedByteArray (input , checkSumSize );
40
- characterCount = fileBytes .length ;
41
- checkSumResult = checksum8 (fileBytes );
42
- break ;
43
- case 16 :
44
- checkSumSize = 16 ;
45
- fileBytes = getAdjustedByteArray (input , checkSumSize );
46
- characterCount = fileBytes .length ;
47
- checkSumResult = checksum16 (fileBytes );
48
- break ;
49
- case 32 :
50
- checkSumSize = 32 ;
51
- fileBytes = getAdjustedByteArray (input , checkSumSize );
52
- characterCount = fileBytes .length ;
53
- checkSumResult = checksum32 (fileBytes );
54
- break ;
55
- default :
56
- System .err .print ("Valid checksum sizes are 8, 16, or 32\n " );
57
- System .exit (1 );
58
- break ;
78
+ if (getPadding (originalSize , bit ) > 0 ) {
79
+ for (int j = originalSize ; j < newSize ; j ++) {
80
+ temp [j ] = 88 ;
59
81
}
60
- System .out .printf ("\n %s\n %2d bit checksum is %8x for all %4d chars\n " ,
61
- formattedStringOutput (getAdjustedString (input , checkSumSize )), checkSumSize , checkSumResult , characterCount );
82
+ }
83
+ return temp ;
84
+ }
85
+
86
+ private static int checksum (byte [] bytes , int numBits ) {
87
+ switch (numBits ) {
88
+ case 8 :
89
+ return checksum8 (bytes );
90
+ case 16 :
91
+ return checksum16 (bytes );
92
+ case 32 :
93
+ return checksum32 (bytes );
94
+ default :
95
+ throw new InvalidStateException ("Valid checksum sizes are 8, 16, or 32\n " );
62
96
}
63
97
}
64
98
@@ -71,8 +105,9 @@ public static void main(String[] args) {
71
105
private static int checksum8 (byte [] data ) {
72
106
int check = 0 ;
73
107
74
- for (byte b : data )
108
+ for (byte b : data ) {
75
109
check += b ;
110
+ }
76
111
77
112
return check & 0xFF ;
78
113
}
@@ -86,8 +121,9 @@ private static int checksum8(byte[] data) {
86
121
private static int checksum16 (byte [] data ) {
87
122
int check = 0 ;
88
123
89
- for (int i = 0 ; i <= data .length - 2 ; i += 2 )
124
+ for (int i = 0 ; i <= data .length - 2 ; i += 2 ) {
90
125
check += ((data [i ] << 8 ) | (data [i + 1 ] & 0xFF ));
126
+ }
91
127
92
128
return check & 0xFFFF ;
93
129
}
@@ -101,23 +137,13 @@ private static int checksum16(byte[] data) {
101
137
private static int checksum32 (byte [] data ) {
102
138
int check = 0 ;
103
139
104
- for (int i = 0 ; i < data .length ; i += 4 )
140
+ for (int i = 0 ; i < data .length ; i += 4 ) {
105
141
check += ((data [i ] << 24 ) | (data [i + 1 ] << 16 ) | (data [i + 2 ] << 8 ) | (data [i + 3 ])) & 0xffffffffL ;
142
+ }
106
143
107
144
return check ;
108
145
}
109
146
110
- /**
111
- * @param fileName - file to search
112
- * @return reads everything from the file and returns data in String format.
113
- * @throws Exception Citation: https://www.geeksforgeeks.org/different-ways-reading-text-file-java/
114
- */
115
- private static String readFileAsString (String fileName ) throws Exception {
116
- String data ;
117
- data = new String (Files .readAllBytes (Paths .get (fileName )));
118
- return data ;
119
- }
120
-
121
147
/**
122
148
* @param output - message to format and output
123
149
* @return formatted message output by adding a new line every 80 characters.
@@ -126,51 +152,29 @@ private static String readFileAsString(String fileName) throws Exception {
126
152
private static String formattedStringOutput (String output ) {
127
153
StringBuilder res = new StringBuilder ();
128
154
for (int i = 0 ; i < output .length (); i ++) {
129
- if (i > 0 && i % 80 == 0 )
155
+ if (i > 0 && i % 80 == 0 ) {
130
156
res .append ("\n " );
157
+ }
131
158
res .append (output .charAt (i ));
132
159
}
133
160
return res .toString ();
134
161
}
135
162
136
- /**
137
- * @param in - input text to grab characters from to convert to byte.
138
- * @param bit - bit size we are using. bit > 8 ? sum every 2 chars : sum each chars
139
- * @return byte[] with appropriate padding if applicable. Padding with X (88 ASCII)
140
- */
141
- private static byte [] getAdjustedByteArray (String in , int bit ) {
142
- int originalSize = in .getBytes ().length , newSize ;
143
-
144
- newSize = originalSize + getPadding (originalSize , bit );
145
- byte [] temp = new byte [newSize ];
146
-
147
- for (int i = 0 ; i < originalSize ; i ++) {
148
- temp [i ] = (byte ) in .charAt (i );
149
- }
150
-
151
- if (getPadding (originalSize , bit ) > 0 ) {
152
- for (int j = originalSize ; j < newSize ; j ++) {
153
- temp [j ] = 88 ;
154
- }
155
- }
156
- return temp ;
157
- }
158
-
159
163
/**
160
164
* @param in - input string to be adjusted
161
165
* @param bit - bit size parameter to adjust text with padding character 'X'
162
166
* @return formatted output
163
167
*/
164
168
private static String getAdjustedString (String in , int bit ) {
165
- int originalSize = in .getBytes ().length , newSize ;
169
+ int originalSize = in .getBytes ().length , padding = getPadding ( originalSize , bit ), newSize ;
166
170
StringBuilder builder = new StringBuilder ();
167
- newSize = originalSize + getPadding ( originalSize , bit ) ;
171
+ newSize = originalSize + padding ;
168
172
169
173
for (int i = 0 ; i < originalSize ; i ++) {
170
174
builder .append (in .charAt (i ));
171
175
}
172
176
173
- if (getPadding ( originalSize , bit ) > 0 ) {
177
+ if (padding > 0 ) {
174
178
for (int j = originalSize ; j < newSize ; j ++) {
175
179
builder .append ("X" );
176
180
}
@@ -179,33 +183,20 @@ private static String getAdjustedString(String in, int bit) {
179
183
return builder .toString ();
180
184
}
181
185
182
- /**
183
- * @param bit - bit size parameter to check against the specified bit sizes that are compatible
184
- * @return boolean result if any match preset sizes
185
- */
186
- private static boolean validBitSize (int bit ) {
187
- int [] validBits = {8 , 16 , 32 };
188
- return Arrays .stream (validBits ).anyMatch (i -> i == bit );
189
- }
190
-
191
186
192
187
/**
193
188
* @param lengthOriginal - length of the input file non-padded
194
189
* @param bit - bit size == 32 then we mod 4 else mod 2. (4 for every 4 bytes : 2 for every 2 bytes)
195
- * a - length
196
- * b - modulus
197
- * c - padding result
198
190
* @return amount of padding we need to add to the original length (input length) if bit > 8
199
191
*/
200
192
private static int getPadding (int lengthOriginal , int bit ) {
201
- int a = lengthOriginal ;
202
- int b = bit == 32 ? 4 : 2 ;
203
- int c = 0 ;
204
- while (a % b != 0 ) {
205
- a = a + 1 ;
206
- c ++;
193
+ int length = lengthOriginal ;
194
+ int modulus = bit == 32 ? 4 : 2 ;
195
+ int result = 0 ;
196
+ while (length % modulus != 0 ) {
197
+ length = length + 1 ;
198
+ result ++;
207
199
}
208
- return bit > 8 ? c : 0 ;
200
+ return bit > 8 ? result : 0 ;
209
201
}
210
-
211
202
}
0 commit comments