-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtag.java
More file actions
136 lines (106 loc) · 3.16 KB
/
Copy pathtag.java
File metadata and controls
136 lines (106 loc) · 3.16 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
package LZ77;
import java.awt.List;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import javax.swing.text.AbstractDocument.BranchElement;
public class tag {
public int pointer;
public int length;
public char cha;
public static ArrayList<Boolean> binaryTag = new ArrayList<Boolean>(32);
public static ArrayList<Integer> binaryNum = new ArrayList<Integer>(32);
public static int numberTages;
public String toString() {
return pointer + " " + length + " " + cha;
}
public void prepareTag(binNum poi, binNum le) {
ArrayList<Boolean> arPoi = poi.toBinary(pointer), arLen = le
.toBinary(length), ch = binNum.fromChar(cha);
for (int i = 0; i < arPoi.size(); i++) {
binaryTag.add(arPoi.get(i));
}
for (int i = 0; i < arLen.size(); i++) {
binaryTag.add(arLen.get(i));
}
for (int i = 0; i < ch.size(); i++) {
binaryTag.add(ch.get(i));
}
}
public static void genrate() {
int num = 0;
for (int i = 0, j = 1; i < binaryTag.size(); i++, j++) {
if (j % 32 == 0) {
j = 1;
binaryNum.add(num);
num = (int) ((binaryTag.get(i) ? 1 : 0) * Math.pow(2, j - 1));
} else
num += (binaryTag.get(i) ? 1 : 0) * Math.pow(2, j - 1);
}
if (num > 0)
binaryNum.add(num);
}
public static void createBinaryNum() {
binaryTag = new ArrayList<Boolean>(32);
int num = 0;
for (int i = 0; i < binaryNum.size(); i++) {
num = binaryNum.get(i);
for (int j2 = 0; j2 < 31; j2++) {
binaryTag.add((num % 2 == 1 ? true : false));
num /= 2;
}
}
}
public static ArrayList<tag> deGenrate(binNum poi, binNum le) {
createBinaryNum();
ArrayList<tag> list = new ArrayList<tag>();
int tagSize = 8 + poi.binLen + le.binLen;
for (int i = 0, j2 = 0; i < binaryTag.size() && j2 < numberTages; i += tagSize, j2++) {
tag temp = new tag();
ArrayList<Boolean> po = new ArrayList<Boolean>();
for (int j = i; j < i + poi.binLen; j++) {
po.add(binaryTag.get(j));
}
temp.pointer = poi.toDecimal(po);
ArrayList<Boolean> l = new ArrayList<Boolean>();
for (int j = i + poi.binLen; j < i + poi.binLen + le.binLen; j++) {
l.add(binaryTag.get(j));
}
temp.length = le.toDecimal(l);
ArrayList<Boolean> ch = new ArrayList<Boolean>();
for (int j = i + poi.binLen + le.binLen; j < i + poi.binLen
+ le.binLen + 8
&& j < binaryTag.size(); j++) {
ch.add(binaryTag.get(j));
}
temp.cha = binNum.toChar(ch);
list.add(temp);
}
return list;
}
public static void saveTag(DataOutputStream out, binNum poi, binNum le)
throws IOException {
out.writeShort(poi.binLen);
out.writeShort(le.binLen);
genrate();
out.writeShort(binaryTag.size() / (poi.binLen + le.binLen + 8));
for (int i = 0; i < binaryNum.size(); i++) {
out.writeInt(binaryNum.get(i));
}
}
public static void readTag(DataInputStream in, binNum poi, binNum le) {
binaryNum = new ArrayList<Integer>(32);
try {
poi.SetMaxBinLen(in.readShort());
le.SetMaxBinLen(in.readShort());
numberTages = in.readShort();
for (int i = 0; true; i++) {
binaryNum.add(in.readInt());
}
} catch (Exception e) {
return;
}
}
}