1
|
package deserialize;
|
2
|
|
3
|
import java.io.BufferedInputStream;
|
4
|
import java.io.BufferedWriter;
|
5
|
import java.io.ByteArrayInputStream;
|
6
|
import java.io.File;
|
7
|
import java.io.FileInputStream;
|
8
|
import java.io.FileWriter;
|
9
|
import java.io.IOException;
|
10
|
import java.io.ObjectInputStream;
|
11
|
import java.util.zip.ZipEntry;
|
12
|
import java.util.zip.ZipInputStream;
|
13
|
|
14
|
import org.json.simple.JSONObject;
|
15
|
|
16
|
public class ToJSON extends Thread {
|
17
|
|
18
|
private File input;
|
19
|
private File output;
|
20
|
|
21
|
private byte buffer[];
|
22
|
|
23
|
public ToJSON(File inputFilename, File outputFilename) {
|
24
|
this.input = inputFilename;
|
25
|
this.output = outputFilename;
|
26
|
}
|
27
|
|
28
|
@Override
|
29
|
public void run() {
|
30
|
super.run();
|
31
|
|
32
|
buffer = readByteArray();
|
33
|
if (buffer == null) {
|
34
|
// TODO Chyba na??t?n? dat
|
35
|
} else if (deserializeClassically()) {
|
36
|
// TODO Chyba klasick? deserializace
|
37
|
} else {
|
38
|
// Univerzalni deserializer.
|
39
|
convert();
|
40
|
}
|
41
|
}
|
42
|
|
43
|
private void convert() {
|
44
|
System.out.println("Spoustim univerzalni deserializer.");
|
45
|
|
46
|
Grammar g = new Grammar(buffer);
|
47
|
JSONObject results = g.process();
|
48
|
|
49
|
save(results);
|
50
|
|
51
|
System.out.println("JSON byl vytvoren.");
|
52
|
}
|
53
|
|
54
|
private boolean deserializeClassically() {
|
55
|
try {
|
56
|
|
57
|
// Try to deserialize classically.
|
58
|
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(buffer));
|
59
|
@SuppressWarnings("unused")
|
60
|
Deserializer d = (Deserializer) ois.readObject();
|
61
|
System.out.println("Klasicka deserializace probehla v poradku.");
|
62
|
ois.close();
|
63
|
|
64
|
} catch (IOException e) {
|
65
|
|
66
|
// Stream error.
|
67
|
System.out.println("Chyba pri otevirani (uzavirani) streamu pri pokusu o klasickou deserializaci.");
|
68
|
e.printStackTrace();
|
69
|
|
70
|
return false;
|
71
|
|
72
|
} catch (Exception e) {
|
73
|
|
74
|
// Others: ClassNotFoundException, ClassCastException, ...
|
75
|
// Usually due to problems with classical deserialization.
|
76
|
return false;
|
77
|
|
78
|
}
|
79
|
return true;
|
80
|
}
|
81
|
|
82
|
private byte[] readByteArray() {
|
83
|
byte array[] = null;
|
84
|
|
85
|
/*
|
86
|
* Note.
|
87
|
* For ZIP - the first four bytes should be one of the following combinations:
|
88
|
* 50 4B 03 04
|
89
|
* 50 4B 05 06 (empty archive)
|
90
|
* 50 4B 07 08 (spanned archive)
|
91
|
* Source: https://en.wikipedia.org/wiki/List_of_file_signatures
|
92
|
*/
|
93
|
ZipInputStream zipInputStream = null;
|
94
|
ZipEntry entry = null;
|
95
|
try {
|
96
|
zipInputStream = new ZipInputStream(new FileInputStream(input));
|
97
|
entry = zipInputStream.getNextEntry();
|
98
|
System.out.println("Velikost ZIP dat je " + entry.getSize() + "B.");
|
99
|
} catch (Exception e) {
|
100
|
System.out.println("Soubor neni zazipovany...");
|
101
|
}
|
102
|
|
103
|
try {
|
104
|
if (entry != null) {
|
105
|
//array = new byte[(int) entry.getSize()];
|
106
|
array = entry.getExtra();
|
107
|
System.out.println(array.length);
|
108
|
//zipInputStream.read(array);
|
109
|
zipInputStream.close();
|
110
|
} else {
|
111
|
array = new byte[(int) input.length()];
|
112
|
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(input));
|
113
|
bis.read(array);
|
114
|
bis.close();
|
115
|
}
|
116
|
System.out.println("Byty ze souboru byly v poradku nacteny.");
|
117
|
} catch (IOException e) {
|
118
|
System.out.println("Pri nacitani bytu doslo k chybe.");
|
119
|
return null;
|
120
|
}
|
121
|
|
122
|
return array;
|
123
|
}
|
124
|
|
125
|
private void save(JSONObject obj) {
|
126
|
try {
|
127
|
FileWriter fw = new FileWriter(output);
|
128
|
BufferedWriter bw = new BufferedWriter(fw);
|
129
|
bw.write(obj.toJSONString());
|
130
|
bw.flush();
|
131
|
bw.close();
|
132
|
Report.info("JSON", null, "Ulo?en? souboru prob?hlo v po??dku.");
|
133
|
} catch (IOException e) {
|
134
|
e.printStackTrace();
|
135
|
Report.info("JSON", null, "P?i ukl?d?n? souboru nastala chyba.");
|
136
|
}
|
137
|
}
|
138
|
|
139
|
}
|