Revize 9744d223
Přidáno uživatelem Michal Horký před více než 4 roky(ů)
demo_mh/Deserializer/src/deserialize/ToJSON.java | ||
---|---|---|
1 | 1 |
package deserialize; |
2 | 2 |
|
3 |
import java.io.BufferedInputStream; |
|
3 | 4 |
import java.io.BufferedWriter; |
5 |
import java.io.ByteArrayInputStream; |
|
6 |
import java.io.File; |
|
7 |
import java.io.FileInputStream; |
|
4 | 8 |
import java.io.FileWriter; |
5 | 9 |
import java.io.IOException; |
6 |
import java.nio.ByteBuffer; |
|
7 |
import java.util.ArrayList; |
|
8 |
import java.util.HashMap; |
|
9 |
import java.util.List; |
|
10 |
import java.util.concurrent.atomic.AtomicInteger; |
|
10 |
import java.io.ObjectInputStream; |
|
11 |
import java.util.zip.ZipEntry; |
|
12 |
import java.util.zip.ZipInputStream; |
|
11 | 13 |
|
12 |
import org.json.simple.JSONArray; |
|
13 | 14 |
import org.json.simple.JSONObject; |
14 | 15 |
|
15 |
import general.Constants; |
|
16 |
|
|
17 | 16 |
public class ToJSON extends Thread { |
18 | 17 |
|
18 |
private File input; |
|
19 |
private File output; |
|
20 |
|
|
19 | 21 |
private byte buffer[]; |
20 | 22 |
|
21 |
public ToJSON(byte buffer[]) { |
|
22 |
this.buffer = buffer; |
|
23 |
public ToJSON(File inputFilename, File outputFilename) { |
|
24 |
this.input = inputFilename; |
|
25 |
this.output = outputFilename; |
|
23 | 26 |
} |
24 | 27 |
|
25 | 28 |
@Override |
26 | 29 |
public void run() { |
27 | 30 |
super.run(); |
28 | 31 |
|
29 |
List<Byte> buffer = new ArrayList<Byte>(); |
|
30 |
for (int i = 0; i < this.buffer.length; i++) { |
|
31 |
buffer.add(this.buffer[i]); |
|
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(); |
|
32 | 40 |
} |
41 |
} |
|
42 |
|
|
43 |
private void convert() { |
|
44 |
System.out.println("Spoustim univerzalni deserializer."); |
|
33 | 45 |
|
34 | 46 |
Grammar g = new Grammar(buffer); |
35 |
g.start(); |
|
36 |
|
|
37 |
/*for (int i = 0; i < buffer.length; i++) { |
|
38 |
if (buffer[i] == TC_CLASSDESC) { |
|
39 |
// Class name |
|
40 |
int nameLength = Formatter.createInt(buffer[++i], buffer[++i]); |
|
41 |
String name = new String(buffer, ++i, nameLength); |
|
42 |
i += nameLength - 1; |
|
43 |
System.out.println(name); |
|
44 |
|
|
45 |
// UID |
|
46 |
ByteBuffer toLong = ByteBuffer.allocate(Long.BYTES); |
|
47 |
toLong.put(buffer, i + 1, 8); |
|
48 |
toLong.flip(); |
|
49 |
System.out.println(toLong.getLong()); |
|
50 |
|
|
51 |
// implements ... |
|
52 |
i += 9; |
|
53 |
System.out.println(buffer[i] == SC_SERIALIZABLE); |
|
54 |
|
|
55 |
// Variables |
|
56 |
int vCount = Formatter.createInt(buffer[++i], buffer[++i]); |
|
57 |
for (int j = 0; j < vCount; j++) { |
|
58 |
// Type |
|
59 |
char vType = (char) (buffer[++i]); |
|
60 |
// Name |
|
61 |
int vNameLength = Formatter.createInt(buffer[++i], buffer[++i]); |
|
62 |
String vName = new String(buffer, ++i, vNameLength); |
|
63 |
i += vNameLength - 1; |
|
64 |
// toString() |
|
65 |
System.out.println(typeCodes.get(vType) + " " + new String(vName)); |
|
66 |
} |
|
67 |
|
|
68 |
// TODO |
|
69 |
System.out.println(buffer[++i] == TC_ENDBLOCKDATA); |
|
70 |
} |
|
71 |
}*/ |
|
47 |
JSONObject results = g.process(); |
|
48 |
|
|
49 |
save(results); |
|
50 |
|
|
51 |
System.out.println("JSON byl vytvoren."); |
|
72 | 52 |
} |
73 | 53 |
|
74 |
@SuppressWarnings("unchecked") |
|
75 |
private void save() { |
|
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 |
} catch (Exception e) { |
|
71 |
|
|
72 |
// Others: ClassNotFoundException, ClassCastException, ... |
|
73 |
// Usually due to problems with classical deserialization. |
|
74 |
return false; |
|
75 |
|
|
76 |
} |
|
77 |
return true; |
|
78 |
} |
|
79 |
|
|
80 |
private byte[] readByteArray() { |
|
81 |
byte array[] = null; |
|
82 |
|
|
76 | 83 |
/* |
77 |
JSONObject countryObj = new JSONObject(); |
|
78 |
countryObj.put("Name", "India"); |
|
79 |
countryObj.put("Population", new Integer(1000000)); |
|
80 |
|
|
81 |
JSONArray listOfStates = new JSONArray(); |
|
82 |
listOfStates.add("Madhya Pradesh"); |
|
83 |
listOfStates.add("Maharastra"); |
|
84 |
listOfStates.add("Rajasthan"); |
|
85 |
|
|
86 |
countryObj.put("States", listOfStates); |
|
87 |
|
|
84 |
* Note. |
|
85 |
* For ZIP - the first four bytes should be one of the following combinations: |
|
86 |
* 50 4B 03 04 |
|
87 |
* 50 4B 05 06 (empty archive) |
|
88 |
* 50 4B 07 08 (spanned archive) |
|
89 |
* Source: https://en.wikipedia.org/wiki/List_of_file_signatures |
|
90 |
*/ |
|
91 |
ZipInputStream zipInputStream = null; |
|
92 |
ZipEntry entry = null; |
|
93 |
try { |
|
94 |
zipInputStream = new ZipInputStream(new FileInputStream(input)); |
|
95 |
entry = zipInputStream.getNextEntry(); |
|
96 |
System.out.println("Velikost ZIP dat je " + entry.getSize() + "B."); |
|
97 |
} catch (Exception e) { |
|
98 |
System.out.println("Soubor neni zazipovany..."); |
|
99 |
} |
|
100 |
|
|
101 |
try { |
|
102 |
if (entry != null) { |
|
103 |
array = new byte[(int) entry.getSize()]; |
|
104 |
zipInputStream.read(array); |
|
105 |
zipInputStream.close(); |
|
106 |
} else { |
|
107 |
array = new byte[(int) input.length()]; |
|
108 |
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(input)); |
|
109 |
bis.read(array); |
|
110 |
bis.close(); |
|
111 |
} |
|
112 |
System.out.println("Byty ze souboru byly v poradku nacteny."); |
|
113 |
} catch (IOException e) { |
|
114 |
System.out.println("Pri nacitani bytu doslo k chybe."); |
|
115 |
return null; |
|
116 |
} |
|
117 |
|
|
118 |
return array; |
|
119 |
} |
|
120 |
|
|
121 |
private void save(JSONObject obj) { |
|
88 | 122 |
try { |
89 |
// Writing to a file |
|
90 |
FileWriter fw = new FileWriter(Constants.JSON_FILE); |
|
123 |
FileWriter fw = new FileWriter(output); |
|
91 | 124 |
BufferedWriter bw = new BufferedWriter(fw); |
92 |
System.out.println("Zapisuji JSON objekt do souboru."); |
|
93 |
bw.write(countryObj.toJSONString()); |
|
125 |
bw.write(obj.toJSONString()); |
|
94 | 126 |
bw.flush(); |
95 | 127 |
bw.close(); |
128 |
Report.info("JSON", null, "Ulo?en? souboru prob?hlo v po??dku."); |
|
96 | 129 |
} catch (IOException e) { |
97 | 130 |
e.printStackTrace(); |
131 |
Report.info("JSON", null, "P?i ukl?d?n? souboru nastala chyba."); |
|
98 | 132 |
} |
99 |
*/ |
|
100 | 133 |
} |
101 | 134 |
|
102 | 135 |
} |
Také k dispozici: Unified diff
#7767
Zprovoznění exportu do JSON. Přidáno zpracovávání binárních zazipovaných dat. Úprava struktury.