-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
205 lines (172 loc) · 5.49 KB
/
Copy pathMain.java
File metadata and controls
205 lines (172 loc) · 5.49 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
package LZ77;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
public class Main {
public static ArrayList<tag> compress(String data, binNum poi, binNum le) {
ArrayList<tag> list = new ArrayList<tag>();
data += '\1';
String temp = "";
int point;
char ch;
HashMap<String, Integer> map = new HashMap<String, Integer>();
// tag t = new tag();
// t.length = t.pointer = 0;
// t.cha = data.charAt(0);
// map.put(t.cha + "" , 0);
// list.add(t);
for (int i = 0; i < data.length(); i++) {
ch = data.charAt(i);
if (temp == "" && ch == '\1')
break;
if (!map.containsKey(temp + ch)) {
point = Math.max(data.substring(0, i - temp.length() ).lastIndexOf(temp),
(map.containsKey(temp) ? map.get(temp) : -1));
// System.out.println(" point " + point + " " +
// data.substring(0, i).lastIndexOf(temp) + " "
// +(map.containsKey(temp) ? map.get(temp) : -1) );
// /System.out.println(" point " + point);
if (point == -1)
point = i;
tag ta = new tag();
ta.cha = ch;
ta.length = temp.length();
if (ta.length != 0)
ta.pointer = i - point - ta.length;
else
ta.pointer = 0;
poi.maxNum(ta.pointer);
le.maxNum(ta.length);
// ta.prepareTag(poi, le);
// System.out.println(ta.toString());
list.add(ta);
map.put(temp + ch, i - temp.length());
for (int z = 0; z < temp.length(); z++)
map.put(temp.substring(0, z), i - temp.length());
temp = "";
ch = '\1';
continue;
} // else
// map.put(temp + ch , i - temp.length() );
temp += ch;
}
return list;
}
public static String deCompress(ArrayList<tag> l) {
String data = "";
for (int i = 0; i < l.size(); i++) {
//System.out.println(l.get(i).toString());
if (l.get(i).pointer == 0)
data += l.get(i).cha;
else {
// System.out.println(data.length()+" "+l.get(i).toString() + " "
// + (data.length() - l.get(i).pointer) + " "
// + (data.length() - l.get(i).pointer + l.get(i).length) + "\n" + data);
data += data.substring(data.length() - l.get(i).pointer,
data.length() - l.get(i).pointer + l.get(i).length);
if (l.get(i).cha != '\1')
data += l.get(i).cha;
//System.out.println(data + " " + data.length() );
}
}
return data;
}
public static void main(String[] args) {
// ** // GUI
new LZ77APP();
// ** // number Handling
// binNum x = new binNum();
// ** // write to file
// FileOutputStream outPut = new FileOutputStream("Copress.txt");
// DataOutputStream out = new DataOutputStream(outPut);
// out.close();
// ** // read from file
// File inpt = new File("Copress.txt");
// InputStream in = new FileInputStream(inpt);
// binNum le = new binNum();
// le.SetMaxBinLen(in.read());
// binNum poi = new binNum();
// poi.SetMaxBinLen(in.read());
// in .close();
// ** // comprassing
// binNum poi = new binNum(), le = new binNum();
// String data = "aablkjfslf;lawkvjnsdkhnsiovjcoisdjviwhiuwhjoiajpdoshbokooooonfbhddlifabab";
// ArrayList<tag> l = compress(data, poi, le);
// ArrayList<tag> l = compress("abaa");
// for (int i = 0; i <l.size(); i++) {
// System.out.println(l.get(i).toString());
// }
// ** // decomprassing
// String deCompresdData = deCompress(l);
// System.out.println(deCompresdData);
// System.out.print(data);
// ** // testing
// int x= 608571914;
// while(x > 0 ){
// System.out.print(x % 2);
// x /= 2;
// }
// System.out.print(Integer.toBinaryString(5));
// String data = "";
// //System.out.println(1 %2 + " "+0%2);
// Scanner reader = new Scanner(new File("mld.txt"));
// while (reader.hasNext()) {
// data += (reader.nextLine() + "\n");
// }
// //System.out.println(data);
//
// binNum le = new binNum(), poi = new binNum();
// FileOutputStream outPut = new FileOutputStream("1.Lz77");
// DataOutputStream out = new DataOutputStream(outPut);
//
// ArrayList<tag> l = compress(data, poi, le);
//
// le.genrate();
// poi.genrate();
//
// for (int i = 0; i < l.size(); i++) {
// l.get(i).prepareTag(poi, le);
// //System.out.println(l.get(i).toString());
// }
//
// // System.out.println(poi.binLen + "*" + le.binLen);
// tag.saveTag(out, poi, le);
// out.close();
// binNum le = new binNum(), poi = new binNum();
// String deCompreseddata = "";
// InputStream i1n = new FileInputStream(new File("1.Lz77"));
// DataInputStream in = new DataInputStream(i1n);
//
// tag.readTag(in, poi, le);
// ArrayList<tag> kk = tag.deGenrate(poi, le) ;
// in.close();
//
// //tag.saveTag(out, poi, le);
//
// deCompreseddata = deCompress(kk);
// System.out.println(data);
// System.out.print(deCompreseddata);
// for (int i = 0; i < kk.size(); i++) {
// System.out.println(l.get(i).toString());
// }
// Scanner x = new Scanner(new File("1.Lz77"));
// System.out.println(x.nextBoolean());
//
// for (int i1 = 0; i1 < kk.size(); i1++) {
// System.out.println(kk.get(i1).toString());
// }
}
}
/*
* for (int j = i - temp.length() - 1; j >= 0; j--) { //
* System.out.println(data.substring(j, j + // temp.length()+ 1) + "_" + temp +
* (ch == '\1' ? "" : // ch)); if (data.substring(j, j + temp.length() +
* 1).equals( (temp + (ch == '\1' ? "" : ch)))) { point = j;
* //System.out.println("Yes" + j); break; } else System.out.println("No" + j);
* }
*/