-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChecksum.java
More file actions
47 lines (40 loc) · 1.3 KB
/
Checksum.java
File metadata and controls
47 lines (40 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.CRC32;
/**
* CS2105 Assignment 0
* Question 4
* @author Andhieka Putra
*/
public class Checksum {
private static int BUFFER_SIZE = 1 << 25; //in bytes. 32 MB.
private BufferedInputStream inputStream;
public void setInputFile(String inputFile) throws Exception {
try {
inputStream = new BufferedInputStream(new FileInputStream(inputFile));
} catch (Exception e) {
System.out.println("There is an error opening " + inputFile);
throw e;
}
}
public long getChecksum() throws IOException {
CRC32 crc = new CRC32();
byte[] buffer = new byte[BUFFER_SIZE];
while(inputStream.available() != 0) {
int numBytes = inputStream.read(buffer);
crc.update(buffer, 0, numBytes);
}
inputStream.close();
return crc.getValue();
}
public static void main(String[] args) {
try {
Checksum checksum = new Checksum();
checksum.setInputFile(args[0]);
System.out.println(checksum.getChecksum());
} catch (Exception e) {
System.out.println("Failed to calculate checksum. Now exiting.");
}
}
}