-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecode.java
More file actions
82 lines (64 loc) · 2.2 KB
/
Decode.java
File metadata and controls
82 lines (64 loc) · 2.2 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
import java.util.ArrayList ;
import java.util.Scanner ;
public class Decode
{
// This list keeps frequency list.
private ArrayList <Node <Character , Integer>> __codes = null ;
public static void main (String [] args)
{
FileOperations operation = new FileOperations () ;
Decode decode = new Decode () ;
Scanner reference = operation.openFile ("encoded.txt") ;
String line = null ;
decode.__codes = operation.readCodes ("codes.bin") ;
/**
* This loop reads each line from encoded file and passes these lines to function
* which using for decoding.
*/
while ((line = operation.readFile (reference)) != null)
{
decode.__decoding (line , "") ;
}
System.out.println("File was decoded.!");
}
private void __decoding (String line , String letters)
{
if (line.length () == 0)
{
FileOperations operation = new FileOperations () ;
operation.writeFile (letters , "decoded.txt") ;
return ;
}
int start = 0 ; // this variable keeps begin index for creating substring
for (int i = 0 ; i < line.length () ; i ++)
{
String code = "" ;
boolean flag = false ; // to break both loops
/**
* At this point the codes which read from encoded file are comparing with
* frequency list and if equality has found, decoded letter is added to
* letter variable. After that loops are broken and new line passes to the
* decoding function recursively.
*/
for (int j = 0 ; j <= i ; j ++)
{
code += line.charAt (j) ;
}
for (Node <Character , Integer> n : this.__codes)
{
if (code.equals (n.getCode ()))
{
letters += n.getKey () ;
start = i + 1 ;
flag = true ;
break ;
}
}
if (flag)
{
break ;
}
}
this.__decoding (line.substring (start) , letters) ;
}
}