forked from adhavalboy/crc32
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrc32.java
More file actions
66 lines (49 loc) · 1.51 KB
/
Crc32.java
File metadata and controls
66 lines (49 loc) · 1.51 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
public class Crc32 {
private int value;
private int[] table = new int[256];
private static final int SEED = 0xEDB88320;
public Crc32() {
for(int i = 0; i < 256; ++i) {
int v = i;
for(int j = 0; j < 8; ++j) {
v = (v & 1) == 1 ? (SEED ^ (v >>> 1)) : (v >>> 1);
}
this.table[i] = v;
}
}
public void start() {
value = 0xFFFFFFFF;
}
public void update(byte[] bytes, int length) {
for(int i = 0; i < length; ++i) {
this.value = this.table[((this.value ^ bytes[i]) & 0xFF)] ^ (this.value >>> 8);
}
}
public int result() {
return (this.value ^ 0xFFFFFFFF);
}
public static void main(String[] args) throws IOException {
Crc32 crc = new Crc32();
byte[] buf = new byte[1024 * 1024];
int len;
for (String filename : args) {
FileInputStream fs = null;
try {
fs = new FileInputStream(filename);
crc.start();
while((len = fs.read(buf)) > 0) {
crc.update(buf, len);
}
System.out.format("%s: %08x\n", filename, crc.result());
} catch (FileNotFoundException ex) {
} finally {
if (fs != null) {
fs.close();
}
}
}
}
}