-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompression.java
More file actions
129 lines (103 loc) · 2.97 KB
/
compression.java
File metadata and controls
129 lines (103 loc) · 2.97 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import java.util.*;
import java.io.*;
/**
*
* @author Nour Benmohamed
*this is very inspired by the file input demo I found in the day 13 notes
*problem set 3
*5/3/2018
*
*/
public class compression {
protected static Map<Character, Integer> characterFreq;
protected static letter Letter;
protected PriorityQueue<BinaryTree<typeE>> pq;
protected BufferedBitWriter bitOutput;
protected boolean bit;
protected String pathName;
protected String compressedPathName;
protected BufferedReader input;
public compression (String name, String compressedName){
// constructor
characterFreq = new TreeMap<Character, Integer>();
Letter = new letter();
pathName = name;
compressedPathName=compressedName;
}
public void characterCount (Character c) {
/**
* this class makes a map of the character frequencies in the text.
* it modifies characterFreq map instance variable
*/
if (characterFreq.containsKey(c)) {
//TODO makes a map of the caracters as keys and frequency as values
characterFreq.put(c, characterFreq.get(c)+1);
}
else {
characterFreq.put(c,1);
}
}
public void compress() {
/**
* this class will take care of opening the file, and doing the rest of the compression
*/
try {
input = new BufferedReader(new FileReader(pathName)); // add name of file
bitOutput = new BufferedBitWriter(compressedPathName);
}
catch (FileNotFoundException e) {
System.out.println("cannot open file");
}
//reading the file
try {
int cI;
Character c;
Map<Character, String> codes;
String code;
/**
* this while loop will go thru all the character of the text file to create the frequency map
*/
while ((cI= input.read()) != -1 ) {
c=(char)(cI);
//System.out.println("read '"+c+"'");
characterCount (c);
}
System.out.println(characterFreq);
Letter.Queue(characterFreq);
Letter.codeMap();
/**
* this while loop will go thru all the characters again and using the code tree will write into a new file the binary codes of each character
* basically write out the compressed file
*/
codes = Letter.getCharactercodes();
System.out.println("got here");
input = new BufferedReader(new FileReader(pathName)); // add name of file
while ((cI= input.read()) != -1 ) {
c = new Character((char)(cI));
code= codes.get(c);
System.out.println("code is" + code);
for (int i=0; i< code.length(); i++) {
if (code.charAt(i)=='1') {
bitOutput.writeBit(true);
}
else {
bitOutput.writeBit(false);
}
}
}
}
catch (IOException e) {
System.out.println("IO error while reading");
}
//closing the file
finally {
try {
input.close();
bitOutput.close();
}
catch (IOException e) {
System.out.println("cannot close file");
}
}
}
}