-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdaptiveArithmeticCompress.java
More file actions
50 lines (42 loc) · 1.3 KB
/
Copy pathAdaptiveArithmeticCompress.java
File metadata and controls
50 lines (42 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
49
50
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.InputStream;
public class AdaptiveArithmeticCompress {
public static void main(String[] args) throws IOException {
// Show what command line arguments to use
if (args.length == 0) {
System.err.println("Usage: java ArithmeticCompress InputFile OutputFile");
System.exit(1);
return;
}
// Otherwise, compress
File inputFile = new File(args[0]);
File outputFile = new File(args[1]);
InputStream in = new BufferedInputStream(new FileInputStream(inputFile));
BitOutputStream out = new BitOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
try {
compress(in, out);
} finally {
out.close();
in.close();
}
}
static void compress(InputStream in, BitOutputStream out) throws IOException {
FrequencyTable freq = new SimpleFrequencyTable(new FlatFrequencyTable(257)); // Initialize with all symbol frequencies at 1
ArithmeticEncoder enc = new ArithmeticEncoder(out);
while (true) {
int b = in.read();
if (b == -1)
break;
enc.write(freq, b);
freq.increment(b);
}
enc.write(freq, 256); // EOF
enc.finish();
}
}