-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdaptiveArithmeticDecompress.java
More file actions
48 lines (40 loc) · 1.3 KB
/
Copy pathAdaptiveArithmeticDecompress.java
File metadata and controls
48 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
48
package nayuki.arithcode;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class AdaptiveArithmeticDecompress {
public static void main(String[] args) throws IOException {
// Show what command line arguments to use
if (args.length == 0) {
System.err.println("Usage: java ArithmeticDecompress InputFile OutputFile");
System.exit(1);
return;
}
// Otherwise, decompress
File inputFile = new File(args[0]);
File outputFile = new File(args[1]);
BitInputStream in = new BitInputStream(new BufferedInputStream(new FileInputStream(inputFile)));
OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile));
try {
decompress(in, out);
} finally {
out.close();
in.close();
}
}
static void decompress(BitInputStream in, OutputStream out) throws IOException {
FrequencyTable freq = new SimpleFrequencyTable(new FlatFrequencyTable(257)); // Initialize with all symbol frequencies at 1
ArithmeticDecoder dec = new ArithmeticDecoder(in);
while (true) {
int symbol = dec.read(freq);
if (symbol == 256) // EOF symbol
break;
out.write(symbol);
freq.increment(symbol);
}
}
}