-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
91 lines (75 loc) · 2.92 KB
/
Main.java
File metadata and controls
91 lines (75 loc) · 2.92 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
long duration = 0;
FileReader inputStream = null;
String fileName = "Rand, Ayn - Atlas Shrugged.txt";
FileOutputStream outputStream = new FileOutputStream(new File("compressed.txt"));
FileOutputStream codeStream = new FileOutputStream(new File("codes.txt"));
try {
inputStream = new FileReader(fileName);
int c;
StringBuffer str = new StringBuffer();
// read characters into a list of characters
while ((c = inputStream.read()) != -1) {
str.append((char)c);
}
inputStream.close();
// pass list of characters to constructor
// constructor carries out all operations
// necessary for creating the encoding
CodingTree ct = new CodingTree(new String(str));
// write the code file
// codeStr is a String member of CodingTree
// consisting of one (char, binary-codeword) pair
codeStream.write(ct.codeStr.getBytes());
codeStream.close();
// System.out.println(ct);
List<String> codes = new ArrayList<String>();
MyHashTable<String, String> ht = ct.codes;
ht.stats();
StringBuffer codeBuffer = new StringBuffer();
StringBuffer wordBuffer = new StringBuffer();
long asciiCost = str.length()*8;
long compressedCost = 0;
for(int i = 0; i < str.length(); i++){
Character ch = str.charAt(i);
if((ch.compareTo('A') >= 0 && ch.compareTo('Z') <= 0) || (ch.compareTo('a') >= 0 && ch.compareTo('z') <= 0) || ch.equals('\'')){
wordBuffer.append(ch);
}
else { // separator
String codeStr = new String(wordBuffer);
// add the word's codeword to the buffer
if(codeStr.length()>0){
codeBuffer.append(ht.get(codeStr));
}
// add the separator's codeword to the buffer
codeBuffer.append(ht.get(ch.toString()));
wordBuffer = new StringBuffer();
}
if(codeBuffer.length() > 256){
while(codeBuffer.length() > 8){
int chr = Integer.parseInt(codeBuffer.substring(0, 8),2);
outputStream.write(chr);
codeBuffer.delete(0, 8);
compressedCost += 8;
}
}
}
compressedCost += codeBuffer.length();
while(codeBuffer.length() > 8){
int chr = Integer.parseInt(codeBuffer.substring(0, 8),2);
outputStream.write(chr);
codeBuffer.delete(0, 8);
}
outputStream.close();
duration = System.currentTimeMillis() - start;
System.out.println("Uncompressed file size: " + asciiCost/8 + " bytes");
System.out.println("Compressed file size: " + compressedCost/8 + " bytes");
System.out.println("Compression ratio: " + compressedCost*100/asciiCost + "%");
System.out.println("Running Time: " + duration + " milliseconds");
} finally {}
}
}