-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoder.java
More file actions
364 lines (282 loc) · 11.5 KB
/
Encoder.java
File metadata and controls
364 lines (282 loc) · 11.5 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package alda.huffman;
import java.util.TreeMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Iterator;
class Encoder {
private FileOutputStream stream = null;
private OutputStreamWriter writer = null;
private TreeMap<Integer, Integer> charMap;
private TreeMap<Integer, String> binaryMap = new TreeMap<>();
private HNode root = null;
//===========DEBUG MODE=====================
private boolean debug = false; //==
//==========================================
//Adds charMap to queue, , the queue will sort the chars
private void buildGraph() {
PriorityQueue<HNode> queue = new PriorityQueue<>(charMap.size(), new NodeComparator());
for (Map.Entry<Integer, Integer> entry : charMap.entrySet()) {
int i = entry.getValue();
int c = entry.getKey();
HNode hn = new HNode(i, (char) c);
queue.add(hn);
}
if(debug) {
System.out.println("charMap size: " + charMap.size());
for (HNode n : queue) {
System.out.print(n.getCharacter() + ""+n.getValue()+"s");
}
}
//adds the chars to correct node
while (queue.size() > 1) {
HNode x = queue.poll();
HNode y = queue.poll();
HNode newNode = new HNode(y.getValue() + x.getValue());
newNode.setLeft(x);
newNode.setRight(y);
root = newNode;
queue.add(newNode);
}
if(root == null){
throw new IndexOutOfBoundsException("Text must be atleast 2 characters.");
}
getCode(root, "");
}
void encryptText(String file) {
String inputFileName = file;
charMap = new TreeMap<>();
StringBuilder builder = new StringBuilder();
char current = CharReader.NULL;
CharReader charReader = new CharReader(file);
while (current != CharReader.EOF) { //Read the file until EOF
charReader.moveNext();
current = charReader.current();
builder.append(current);
if (current == CharReader.EOF) { // if current is EOF, abort
break;
}
if((int) current > 256){
throw new NumberFormatException(" Only ASCII-values below 256 supported. Your text has--> "+
current + "<-- which has value: "+(int) current);
}
if (charMap.get((int) current) == null) { // Does value exist in charmap?
charMap.put((int) current, 1); // No, add 1 instance of it.
}
else {
int charCount = charMap.get((int) current) + 1; // yes, just add one more.
charMap.put((int) current, charCount);
}
}
if(debug) {
Iterator it = charMap.entrySet().iterator();
while(it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
int key = (int) pair.getKey();
int value = (int) pair.getValue();
System.out.println("" + key + " : " + value);
}
}
buildGraph();
buildBinaryText(inputFileName);
}
private void buildBinaryText(String file) {
StringBuilder binaryValue = new StringBuilder();
CharReader charReader = new CharReader(file);
int current = CharReader.NULL;
getCode(root, "");
//Calculates binary for the graph and adds it first in the binaries.
binaryValue.append(saveGraph(binaryMap));
//then adds the encoded the message.
while (current != CharReader.EOF) {
charReader.moveNext();
current = charReader.current();
if (current == CharReader.EOF) {
break;
}
binaryValue.append(binaryMap.get(current));
}
if(debug) {
System.out.println(binaryValue.toString());
}
else
writeFile(binaryValue, "Encrypted");
}
private void getCode(HNode root, String s){
if(root.getLeft() == null && root.getRight() == null){
if(debug) {
System.out.println(s + " : " + root.getCharacter());
}
binaryMap.put((int)root.getCharacter(), s);
return;
}
getCode(root.getLeft(), s+0);
getCode(root.getRight(), s+1);
}
protected void decryptMessage(String file) {
StringBuilder builder = new StringBuilder();
int current = CharReader.NULL;
CharReader charReader = new CharReader(file);
generateGraphFromBinaries(charReader);
buildGraph();
HNode node = root;
while (current != CharReader.EOF) {
charReader.moveNext();
current = charReader.current();
if (current == CharReader.EOF) {
break;
}
if(current == 48){
node = node.getLeft();
}else if(current == 49){
node = node.getRight();
}
if(node.getLeft() == null && node.getRight() == null){
builder.append(node.getCharacter());
node = root;
}
}
if(debug) {
System.out.println("Message: "+ builder.toString());
}else {
writeFile(builder, "Decrypted");
}
}
private void addAtChar(StringBuilder builder, String string, int charAtValue, int padding){
int amountOfPadding = padding-charAtValue;
for(int j = 0; j<=amountOfPadding; j++){
if(amountOfPadding == 0){
break;
}
builder.append(getBinaryForAscii( intToAscii( 0 ) ));
amountOfPadding--;
}
for(int i= 0; i < charAtValue; i++){
builder.append(getBinaryForAscii(string.charAt(i)));
}
}
private String saveGraph(TreeMap graph){
StringBuilder graphSave = new StringBuilder();
Iterator it = graph.entrySet().iterator();
int graphSize = graph.size();
//------------ GraphMapSize----------//
if(graphSize > 1000){
throw new IndexOutOfBoundsException("Too big message to encrypt");
}
if(graphSize>=100) {
addAtChar(graphSave, (""+graphSize), 3,3 );
}else if(graphSize>=10) {
addAtChar(graphSave, (""+graphSize), 2,3 );
}else{
addAtChar(graphSave, (""+graphSize), 1,3 );
}
while(it.hasNext()){
Map.Entry pair = (Map.Entry)it.next();
int key = (int)pair.getKey();
int charMapValue = charMap.get(key);
//-------------Key--------//
graphSave.append(getBinaryForAscii(key));
//-------------CharMap Value--------//
//Adds charMap Value. must be 8bit x 2 since the value could be bigger than 9. Error if bigger than 99
if(charMapValue >= 1000){
throw new IndexOutOfBoundsException("Too big message to encrypt");
}
if(charMapValue >= 100){
addAtChar(graphSave, (""+charMapValue), 3, 3 );
}
else if(charMapValue >= 10){
addAtChar(graphSave, (""+charMapValue), 2, 3 );
}else{
addAtChar(graphSave, (""+charMapValue), 1, 3 );
}
if(debug) {
System.out.println("Key: (" + key + ") " + getBinaryForAscii(key));
System.out.println("Huffman Length: " + pair.getValue());
}
}
return graphSave.toString();
}
private void generateGraphFromBinaries(CharReader charReader){
charMap = new TreeMap<>();
int graphCounter = 0;
//-------------------------Reads how big the Graph is(size). Between 1 - 512 key so (3 x 8 bit)-------------------------
//---first 8 bit ---(hundreds)
String amount = charReader.getCharacters(8);
int graphCount = convertBinaryToAsciiValue(amount);
int firstInt = charIntToValue(graphCount);
//---middle 8 bit ---(tens)
amount = charReader.getCharacters(8);
graphCount = convertBinaryToAsciiValue(amount);
int secondInt = charIntToValue(graphCount);
//---last 8 bit ---(singulars)
amount = charReader.getCharacters(8);
graphCount = convertBinaryToAsciiValue(amount);
int thirdInt = charIntToValue(graphCount);
graphCount = Integer.parseInt(""+ firstInt + secondInt + thirdInt); //implicit cast from int to string then parse back to int(but added together
while (graphCounter < graphCount) {
//-------------------------binaryMap key + charMap key (1x 8 bit)-------------------------
amount = charReader.getCharacters(8);
int key = convertBinaryToAsciiValue(amount);
int charMapKey = key;
//------------------------- charMap value (3 x 8 bit) -------------------------
//---first 8 bit --- (hundreds)
amount = charReader.getCharacters(8);
int charMapValue = convertBinaryToAsciiValue(amount);
int firstCharMapValue = charIntToValue(charMapValue);
//--- second 8 bit---(tens)
amount = charReader.getCharacters(8);
charMapValue = convertBinaryToAsciiValue(amount);
int secondCharMapValue = charIntToValue(charMapValue);
//--- third 8 bit---(singulars)
amount = charReader.getCharacters(8);
charMapValue = convertBinaryToAsciiValue(amount);
int thirdCharMapValue = charIntToValue(charMapValue);
//--finally put both together att insert into charMap--
charMapValue = Integer.parseInt(firstCharMapValue + "" +secondCharMapValue + thirdCharMapValue); //implicit typecast
charMap.put(charMapKey, charMapValue);
graphCounter++; // keep track of how many Mapvalues should be inserted(first value of binary text)
if(debug) {
System.out.println("key: " + charMapKey + ". value: " + charMapValue);
System.out.println("--------" + graphCounter + "------");
}
}
}
private int charIntToValue(int i){
char convert = (char)i;
return Integer.parseInt( convert+"");
}
private int intToAscii(int i){
return (int)Character.forDigit(i, 10) ;
}
private String getBinaryForAscii(int info){
String asciiValue = String.format("%8s", Integer.toBinaryString(info)).replace(' ', '0');
return asciiValue;
}
private int convertBinaryToAsciiValue(String info){
int charCode = Integer.parseInt(info, 2);
return charCode;
}
private void writeFile(StringBuilder builder, String name){
System.out.println(name+" message: \n" + builder.toString());
try{
try{
stream = new FileOutputStream(System.getProperty("user.dir") + "/test_"+name+".txt");
writer = new OutputStreamWriter(stream);
writer.write(builder.toString());
}
catch (Exception exception) {
System.out.println("EXCEPTION: " + exception);
}
finally {
if (writer != null)
writer.close();
if (stream != null)
stream.close();
}
}
catch (Exception exception) {
System.out.println("EXCEPTION: " + exception);
}
}
}