-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.java
186 lines (135 loc) · 4.74 KB
/
encoder.java
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package com.huffman;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
public class encoder {
static final String encoded = "encoded.bin";
public static void main(String args[]){
String input_file=args[0];
long startTimebin = System.currentTimeMillis();
try {
HuffmanCoding.build_freq_table(input_file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//HuffmanCoding.build_tree_using_pairing_heap();
//HuffmanCoding.build_tree_using_FourAry_heap();
HuffmanCoding.build_tree_using_binary_heap();
makeString(input_file);
long endTimebin = System.currentTimeMillis();
long totalTimebin = endTimebin - startTimebin;
System.out.println("Time using binary heap (millisecond): " +totalTimebin);
long startTimepair = System.currentTimeMillis();
try {
HuffmanCoding.build_freq_table(input_file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HuffmanCoding.build_tree_using_pairing_heap();
//HuffmanCoding.build_tree_using_FourAry_heap();
//HuffmanCoding.build_tree_using_binary_heap();
makeString(input_file);
long endTimepair = System.currentTimeMillis();
long totalTimepair = endTimepair - startTimepair;
System.out.println("Time using binary heap (millisecond): " +totalTimepair);
long startTime4 = System.currentTimeMillis();
try {
HuffmanCoding.build_freq_table(input_file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//HuffmanCoding.build_tree_using_pairing_heap();
//HuffmanCoding.build_tree_using_FourAry_heap();
//HuffmanCoding.build_tree_using_binary_heap();
makeString(input_file);
long endTime4 = System.currentTimeMillis();
long totalTime4 = endTime4 - startTime4;
System.out.println("Time using binary heap (millisecond): " +totalTime4);
}
/*public static void writeToBinary(String inputFile){
try {
//String fileName ="C:\\Sem_2\\Advanced_Data_Structures\\Project\\encoded_new_pairing.bin";
FileOutputStream fileOs;
BufferedReader br = new BufferedReader(new FileReader(inputFile));
String line = "";
fileOs = new FileOutputStream(encoded);
//ObjectOutputStream os= new ObjectOutputStream(fileOs);
String code = "";
String code_temp;
while ((line = br.readLine()) != null) {
// String a = Encode.encode(line2, hTree);
line = line.trim();
if(!line.equals("")){
int val = Integer.valueOf(line);
HashMapNode node = HuffmanCoding.hmap.get(val);
code+=node.bits;
while(code.length()>8){
code_temp = code.substring(0, 8);
code = code.substring(8);
int parsedByte = 0XFF & Integer.parseInt(code_temp,2);
//fileOs.write(parsedByte);
fileOs.write(parsedByte);
}
}
}
if(!code.equals("")){
int parsedByte = 0XFF & Integer.parseInt(code,2);
fileOs.write(parsedByte);
code = "";
}
fileOs.close();
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
*/
/* Make a string using codeTableHashmap after reading from input file */
public static void makeString(String input_file){
try{
BufferedReader br = new BufferedReader(new FileReader(input_file));
StringBuilder code = new StringBuilder();
String line = "";
while((line = br.readLine()) != null && !line.equals("")) {
line = line.trim();
int val = Integer.valueOf(line);
code.append(HuffmanCoding.hmap.get(val).bits);
}
br.close();
/* Write this fullCode string to encoded.bin file */
encode(code.toString());
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("File Not Found Exception makeString encoder: "+e.getMessage());
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("IO Exception makeString encode: "+e.getMessage());
e.printStackTrace();
}
}
public static void encode(String code) throws IOException {
FileOutputStream file_out = new FileOutputStream(encoded);
int i = 0;
byte[] bytearray = new byte[code.length()/8];
int count = 0;
while(i < code.length() - 7){
byte nextbyte = 0x00;
for(int j = 0 ; j < 8; j++){
nextbyte = (byte) (nextbyte << 1);
nextbyte += code.charAt(i+j) == '0'?0x0:0x1;
}
bytearray[count] = nextbyte;
count++;
i += 8;
}
file_out.write(bytearray);
}
}