Revize 920327ea
Přidáno uživatelem Michal Horký před asi 5 roky(ů)
demo_mh/.gitignore | ||
---|---|---|
1 |
/.metadata/ |
demo_mh/Deserializer/.classpath | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<classpath> |
|
3 |
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> |
|
4 |
<classpathentry kind="src" path="src"/> |
|
5 |
<classpathentry kind="lib" path="libs/json-simple-1.1.1.jar"/> |
|
6 |
<classpathentry kind="output" path="bin"/> |
|
7 |
</classpath> |
demo_mh/Deserializer/.gitignore | ||
---|---|---|
1 |
/bin/ |
demo_mh/Deserializer/.project | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<projectDescription> |
|
3 |
<name>Deserializer</name> |
|
4 |
<comment></comment> |
|
5 |
<projects> |
|
6 |
</projects> |
|
7 |
<buildSpec> |
|
8 |
<buildCommand> |
|
9 |
<name>org.eclipse.jdt.core.javabuilder</name> |
|
10 |
<arguments> |
|
11 |
</arguments> |
|
12 |
</buildCommand> |
|
13 |
</buildSpec> |
|
14 |
<natures> |
|
15 |
<nature>org.eclipse.jdt.core.javanature</nature> |
|
16 |
</natures> |
|
17 |
</projectDescription> |
demo_mh/Deserializer/libs/Licenses | ||
---|---|---|
1 |
JSON Simple |
|
2 |
https://code.google.com/archive/p/json-simple/ |
|
3 |
Apache License 2.0 |
demo_mh/Deserializer/results.json | ||
---|---|---|
1 |
{"States":["Madhya Pradesh","Maharastra","Rajasthan"],"Population":1000000,"Name":"India"} |
demo_mh/Deserializer/src/deserialize/Deserializer.java | ||
---|---|---|
1 |
package deserialize; |
|
2 |
|
|
3 |
import java.io.BufferedInputStream; |
|
4 |
import java.io.File; |
|
5 |
import java.io.FileInputStream; |
|
6 |
import java.io.IOException; |
|
7 |
import java.io.ObjectInputStream; |
|
8 |
|
|
9 |
import general.*; |
|
10 |
|
|
11 |
public class Deserializer { |
|
12 |
|
|
13 |
public static void main(String[] args) { |
|
14 |
try { |
|
15 |
|
|
16 |
// Try to deserialize classically. |
|
17 |
FileInputStream fis = new FileInputStream(Constants.SERIALIZED_FILE); |
|
18 |
ObjectInputStream ois = new ObjectInputStream(fis); |
|
19 |
Deserializer d = (Deserializer) ois.readObject(); |
|
20 |
System.out.println(d.toString()); |
|
21 |
ois.close(); |
|
22 |
|
|
23 |
} catch (IOException e) { |
|
24 |
|
|
25 |
// Stream error. |
|
26 |
System.out.println("Chyba pri otevirani (uzavirani) streamu."); |
|
27 |
e.printStackTrace(); |
|
28 |
|
|
29 |
} catch (Exception e) { |
|
30 |
|
|
31 |
// Others: ClassNotFoundException, ClassCastException, ... |
|
32 |
// Usually due to problems with classical deserialization. |
|
33 |
byte buffer[]; |
|
34 |
try { |
|
35 |
File f = new File(Constants.SERIALIZED_FILE); |
|
36 |
buffer = new byte[(int) f.length()]; |
|
37 |
|
|
38 |
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(f)); |
|
39 |
fis.read(buffer); |
|
40 |
fis.close(); |
|
41 |
|
|
42 |
System.out.println("Doslo k chybe pri deserializaci."); |
|
43 |
System.out.println("Spoustim univerzalni deserializer."); |
|
44 |
|
|
45 |
ToJSON thread = new ToJSON(buffer); |
|
46 |
thread.start(); |
|
47 |
try { |
|
48 |
thread.join(); |
|
49 |
System.out.println("JSON byl vytvoren."); |
|
50 |
} catch (InterruptedException e1) { |
|
51 |
e1.printStackTrace(); |
|
52 |
} |
|
53 |
|
|
54 |
} catch (IOException e1) { |
|
55 |
e1.printStackTrace(); |
|
56 |
} |
|
57 |
|
|
58 |
} |
|
59 |
} |
|
60 |
|
|
61 |
} |
demo_mh/Deserializer/src/deserialize/Formatter.java | ||
---|---|---|
1 |
package deserialize; |
|
2 |
|
|
3 |
public class Formatter { |
|
4 |
|
|
5 |
public static int createInt(byte first, byte second) { |
|
6 |
return ((first & 0xFF) << 8) | (second & 0xFF); |
|
7 |
} |
|
8 |
|
|
9 |
} |
demo_mh/Deserializer/src/deserialize/Grammar.java | ||
---|---|---|
1 |
package deserialize; |
|
2 |
|
|
3 |
import java.util.HashMap; |
|
4 |
|
|
5 |
/** |
|
6 |
* https://docs.oracle.com/javase/7/docs/platform/serialization/spec/protocol.html |
|
7 |
*/ |
|
8 |
public class Grammar { |
|
9 |
|
|
10 |
private final static short STREAM_MAGIC = (short) 0xACED; |
|
11 |
private final static short STREAM_VERSION = 5; |
|
12 |
private final static byte TC_NULL = 0x70; |
|
13 |
private final static byte TC_REFERENCE = 0x71; |
|
14 |
private final static byte TC_CLASSDESC = 0x72; |
|
15 |
private final static byte TC_OBJECT = 0x73; |
|
16 |
private final static byte TC_STRING = 0x74; |
|
17 |
private final static byte TC_ARRAY = 0x75; |
|
18 |
private final static byte TC_CLASS = 0x76; |
|
19 |
private final static byte TC_BLOCKDATA = 0x77; |
|
20 |
private final static byte TC_ENDBLOCKDATA = 0x78; |
|
21 |
private final static byte TC_RESET = 0x79; |
|
22 |
private final static byte TC_BLOCKDATALONG = 0x7A; |
|
23 |
private final static byte TC_EXCEPTION = 0x7B; |
|
24 |
private final static byte TC_LONGSTRING = 0x7C; |
|
25 |
private final static byte TC_PROXYCLASSDESC = 0x7D; |
|
26 |
private final static byte TC_ENUM = 0x7E; |
|
27 |
private final static int baseWireHandle = 0x7E0000; |
|
28 |
|
|
29 |
final static byte SC_WRITE_METHOD = 0x01; // if SC_SERIALIZABLE |
|
30 |
final static byte SC_BLOCK_DATA = 0x08; // if SC_EXTERNALIZABLE |
|
31 |
final static byte SC_SERIALIZABLE = 0x02; |
|
32 |
final static byte SC_EXTERNALIZABLE = 0x04; |
|
33 |
final static byte SC_ENUM = 0x10; |
|
34 |
|
|
35 |
HashMap<Character, String> primTypeCodes; |
|
36 |
HashMap<Character, String> objTypeCodes; |
|
37 |
|
|
38 |
|
|
39 |
// Note. (XXX) This token has the XXX type specified |
|
40 |
|
|
41 |
|
|
42 |
public Grammar() { |
|
43 |
primTypeCodes = new HashMap<Character, String>(); |
|
44 |
primTypeCodes.put('B', "byte"); |
|
45 |
primTypeCodes.put('C', "char"); |
|
46 |
primTypeCodes.put('D', "double"); |
|
47 |
primTypeCodes.put('F', "float"); |
|
48 |
primTypeCodes.put('I', "integer"); |
|
49 |
primTypeCodes.put('J', "long"); |
|
50 |
primTypeCodes.put('S', "short"); |
|
51 |
primTypeCodes.put('Z', "boolean"); |
|
52 |
objTypeCodes = new HashMap<Character, String>(); |
|
53 |
objTypeCodes.put('[', "array"); |
|
54 |
objTypeCodes.put('L', "object"); |
|
55 |
} |
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
public void stream() { |
|
61 |
short x = STREAM_MAGIC; x = STREAM_VERSION; contents(); |
|
62 |
} |
|
63 |
|
|
64 |
public void contents() { |
|
65 |
content(); |
|
66 |
// OR |
|
67 |
contents(); content(); |
|
68 |
} |
|
69 |
|
|
70 |
public void content() { |
|
71 |
object(); |
|
72 |
// OR |
|
73 |
byte x = TC_BLOCKDATA; // TODO (unsigned byte)<size> (byte)[size]; |
|
74 |
// OR |
|
75 |
x = TC_BLOCKDATALONG; // TODO (int)<size> (byte)[size]; |
|
76 |
} |
|
77 |
|
|
78 |
public void object() { |
|
79 |
byte x = TC_OBJECT; classDesc(); newHandle(); classdata()/* TODO []*/; // data for each class |
|
80 |
// OR |
|
81 |
x = TC_CLASS; classDesc(); newHandle(); |
|
82 |
// OR |
|
83 |
x = TC_ARRAY; classDesc(); newHandle(); /* TODO (int)<size>*/ values()/*[size]*/; |
|
84 |
// OR |
|
85 |
x = TC_STRING; newHandle(); // TODO (utf) read 16-bit integer (string length) |
|
86 |
// OR |
|
87 |
x = TC_LONGSTRING; newHandle(); // TODO (long-utf) read 64-bit integer (string length) |
|
88 |
// OR |
|
89 |
x = TC_ENUM; classDesc(); newHandle(); /* TODO (String)*/ object(); |
|
90 |
// OR |
|
91 |
newClassDesc(); |
|
92 |
// OR |
|
93 |
x = TC_REFERENCE; // TODO (int) handle ?!?; |
|
94 |
// OR |
|
95 |
x = TC_NULL; |
|
96 |
// OR |
|
97 |
x = TC_EXCEPTION; reset(); /* TODO (Throwable)*/object(); reset(); |
|
98 |
// OR |
|
99 |
x = TC_RESET; |
|
100 |
} |
|
101 |
|
|
102 |
public void classDesc() { |
|
103 |
newClassDesc(); |
|
104 |
// OR |
|
105 |
byte x = TC_NULL; |
|
106 |
// OR |
|
107 |
/* TODO (ClassDesc)*/ x = TC_REFERENCE; // TODO (int) handle ?!?; |
|
108 |
} |
|
109 |
|
|
110 |
public void newClassDesc() { |
|
111 |
byte x = TC_CLASSDESC; /* TODO (utf) read 16-bit integer (string length); (long)*/; newHandle(); classDescFlags(); fields(); classAnnotation(); classDesc(); |
|
112 |
// OR |
|
113 |
x = TC_PROXYCLASSDESC; newHandle(); /* TODO (int)<count> => read int as count /(utf) read 16-bit integer (string length)/ [count]*/; classAnnotation(); classDesc(); |
|
114 |
} |
|
115 |
|
|
116 |
public void classDescFlags() { |
|
117 |
// TODO (byte) |
|
118 |
} |
|
119 |
|
|
120 |
public void fields() { |
|
121 |
/* TODO (short)<count> => read short as count */ fieldDesc() /*[count]*/; |
|
122 |
} |
|
123 |
|
|
124 |
public void fieldDesc() { |
|
125 |
// TODO primTypeCodes.get(key); (utf) read 16-bit integer (string length); |
|
126 |
// OR |
|
127 |
/* TODO objTypeCodes.get(key) - array or object; (utf) read 16-bit integer (string length);*/ /*(String)*/object(); // String containing the field's type, |
|
128 |
// in field descriptor format |
|
129 |
} |
|
130 |
|
|
131 |
public void classAnnotation() { |
|
132 |
byte x = TC_ENDBLOCKDATA; |
|
133 |
// OR |
|
134 |
contents(); x = TC_ENDBLOCKDATA; // contents written by annotateClass |
|
135 |
} |
|
136 |
|
|
137 |
public void classdata() { |
|
138 |
values(); // fields in order of class descriptor // SC_SERIALIZABLE & classDescFlag && |
|
139 |
// !(SC_WRITE_METHOD & classDescFlags) |
|
140 |
// OR |
|
141 |
values() /* fields in order of class descriptor*/; objectAnnotation(); // SC_SERIALIZABLE & classDescFlag && |
|
142 |
// SC_WRITE_METHOD & classDescFlags |
|
143 |
// OR |
|
144 |
externalContents(); // SC_EXTERNALIZABLE & classDescFlag && |
|
145 |
// !(SC_BLOCKDATA & classDescFlags |
|
146 |
// OR |
|
147 |
objectAnnotation(); // SC_EXTERNALIZABLE & classDescFlag&& |
|
148 |
// SC_BLOCKDATA & classDescFlags |
|
149 |
} |
|
150 |
|
|
151 |
public void objectAnnotation() { |
|
152 |
byte x = TC_ENDBLOCKDATA; |
|
153 |
// OR |
|
154 |
contents(); x = TC_ENDBLOCKDATA; // contents written by writeObject |
|
155 |
// or writeExternal PROTOCOL_VERSION_2. |
|
156 |
} |
|
157 |
|
|
158 |
public void externalContent() { // Only parseable by readExternal |
|
159 |
// TODO ( bytes) // primitive data |
|
160 |
// OR |
|
161 |
object(); |
|
162 |
} |
|
163 |
|
|
164 |
public void externalContents() {// externalContent written by |
|
165 |
externalContent(); // writeExternal in PROTOCOL_VERSION_1. |
|
166 |
// OR |
|
167 |
externalContents(); externalContent(); |
|
168 |
} |
|
169 |
|
|
170 |
public void values() { |
|
171 |
// The size and types are described by the |
|
172 |
// classDesc for the current object |
|
173 |
} |
|
174 |
|
|
175 |
public void newHandle() { |
|
176 |
// The next number in sequence is assigned |
|
177 |
// to the object being serialized or deserialized |
|
178 |
} |
|
179 |
|
|
180 |
public void reset() { |
|
181 |
// The set of known objects is discarded |
|
182 |
// so the objects of the exception do not |
|
183 |
// overlap with the previously sent objects |
|
184 |
// or with objects that may be sent after |
|
185 |
// the exception |
|
186 |
} |
|
187 |
} |
demo_mh/Deserializer/src/deserialize/ToJSON.java | ||
---|---|---|
1 |
package deserialize; |
|
2 |
|
|
3 |
import java.io.BufferedWriter; |
|
4 |
import java.io.FileWriter; |
|
5 |
import java.io.IOException; |
|
6 |
import java.nio.ByteBuffer; |
|
7 |
import java.util.HashMap; |
|
8 |
|
|
9 |
import org.json.simple.JSONArray; |
|
10 |
import org.json.simple.JSONObject; |
|
11 |
|
|
12 |
import general.Constants; |
|
13 |
|
|
14 |
public class ToJSON extends Thread { |
|
15 |
|
|
16 |
private final static byte TC_CLASSDESC = 0x72; |
|
17 |
private final static byte TC_ENDBLOCKDATA = 0x78; |
|
18 |
private final static byte SC_SERIALIZABLE = 0x02; |
|
19 |
|
|
20 |
HashMap<Character, String> typeCodes; |
|
21 |
|
|
22 |
private byte buffer[]; |
|
23 |
|
|
24 |
public ToJSON(byte buffer[]) { |
|
25 |
this.buffer = buffer; |
|
26 |
|
|
27 |
typeCodes = new HashMap<Character, String>(); |
|
28 |
typeCodes.put('B', "byte"); |
|
29 |
typeCodes.put('C', "char"); |
|
30 |
typeCodes.put('D', "double"); |
|
31 |
typeCodes.put('F', "float"); |
|
32 |
typeCodes.put('I', "integer"); |
|
33 |
typeCodes.put('J', "long"); |
|
34 |
typeCodes.put('S', "short"); |
|
35 |
typeCodes.put('Z', "boolean"); |
|
36 |
// --- |
|
37 |
typeCodes.put('[', "array"); |
|
38 |
typeCodes.put('L', "object"); |
|
39 |
} |
|
40 |
|
|
41 |
@Override |
|
42 |
public void run() { |
|
43 |
super.run(); |
|
44 |
|
|
45 |
for (int i = 0; i < buffer.length; i++) { |
|
46 |
if (buffer[i] == TC_CLASSDESC) { |
|
47 |
// Class name |
|
48 |
int nameLength = Formatter.createInt(buffer[++i], buffer[++i]); |
|
49 |
String name = new String(buffer, ++i, nameLength); |
|
50 |
i += nameLength - 1; |
|
51 |
System.out.println(name); |
|
52 |
|
|
53 |
// UID |
|
54 |
ByteBuffer toLong = ByteBuffer.allocate(Long.BYTES); |
|
55 |
toLong.put(buffer, i + 1, 8); |
|
56 |
toLong.flip(); |
|
57 |
System.out.println(toLong.getLong()); |
|
58 |
|
|
59 |
// implements ... |
|
60 |
i += 9; |
|
61 |
System.out.println(buffer[i] == SC_SERIALIZABLE); |
|
62 |
|
|
63 |
// Variables |
|
64 |
int vCount = Formatter.createInt(buffer[++i], buffer[++i]); |
|
65 |
for (int j = 0; j < vCount; j++) { |
|
66 |
// Type |
|
67 |
char vType = (char) (buffer[++i]); |
|
68 |
// Name |
|
69 |
int vNameLength = Formatter.createInt(buffer[++i], buffer[++i]); |
|
70 |
String vName = new String(buffer, ++i, vNameLength); |
|
71 |
i += vNameLength - 1; |
|
72 |
// toString() |
|
73 |
System.out.println(typeCodes.get(vType) + " " + new String(vName)); |
|
74 |
} |
|
75 |
|
|
76 |
// TODO |
|
77 |
System.out.println(buffer[++i] == TC_ENDBLOCKDATA); |
|
78 |
} |
|
79 |
} |
|
80 |
} |
|
81 |
|
|
82 |
@SuppressWarnings("unchecked") |
|
83 |
private void save() { |
|
84 |
/* |
|
85 |
JSONObject countryObj = new JSONObject(); |
|
86 |
countryObj.put("Name", "India"); |
|
87 |
countryObj.put("Population", new Integer(1000000)); |
|
88 |
|
|
89 |
JSONArray listOfStates = new JSONArray(); |
|
90 |
listOfStates.add("Madhya Pradesh"); |
|
91 |
listOfStates.add("Maharastra"); |
|
92 |
listOfStates.add("Rajasthan"); |
|
93 |
|
|
94 |
countryObj.put("States", listOfStates); |
|
95 |
|
|
96 |
try { |
|
97 |
// Writing to a file |
|
98 |
FileWriter fw = new FileWriter(Constants.JSON_FILE); |
|
99 |
BufferedWriter bw = new BufferedWriter(fw); |
|
100 |
System.out.println("Zapisuji JSON objekt do souboru."); |
|
101 |
bw.write(countryObj.toJSONString()); |
|
102 |
bw.flush(); |
|
103 |
bw.close(); |
|
104 |
} catch (IOException e) { |
|
105 |
e.printStackTrace(); |
|
106 |
} |
|
107 |
*/ |
|
108 |
} |
|
109 |
|
|
110 |
} |
demo_mh/Deserializer/src/general/Constants.java | ||
---|---|---|
1 |
package general; |
|
2 |
|
|
3 |
public class Constants { |
|
4 |
|
|
5 |
public static String SERIALIZED_FILE = "data.out"; |
|
6 |
public static String JSON_FILE = "results.json"; |
|
7 |
|
|
8 |
} |
demo_mh/Deserializer/src/serialize/Contain.java | ||
---|---|---|
1 |
package serialize; |
|
2 |
|
|
3 |
import java.io.Serializable; |
|
4 |
|
|
5 |
public class Contain implements Serializable { |
|
6 |
|
|
7 |
private static final long serialVersionUID = 1L; |
|
8 |
|
|
9 |
char array[] = new char[] {'W', 'X', 'Y', 'Z'}; |
|
10 |
|
|
11 |
} |
demo_mh/Deserializer/src/serialize/Example.java | ||
---|---|---|
1 |
package serialize; |
|
2 |
|
|
3 |
import java.io.FileOutputStream; |
|
4 |
import java.io.ObjectOutputStream; |
|
5 |
import java.io.Serializable; |
|
6 |
|
|
7 |
import general.*; |
|
8 |
|
|
9 |
public class Example extends Parent implements Serializable { |
|
10 |
|
|
11 |
private static final long serialVersionUID = 1L; |
|
12 |
|
|
13 |
private int number = 2; |
|
14 |
Contain variable = new Contain(); |
|
15 |
|
|
16 |
public long getNumber() { |
|
17 |
return number; |
|
18 |
} |
|
19 |
|
|
20 |
public static void main(String[] args) { |
|
21 |
try { |
|
22 |
FileOutputStream fos = new FileOutputStream(Constants.SERIALIZED_FILE); |
|
23 |
ObjectOutputStream oos = new ObjectOutputStream(fos); |
|
24 |
Example s = new Example(); |
|
25 |
oos.writeObject(s); |
|
26 |
oos.flush(); |
|
27 |
oos.close(); |
|
28 |
System.out.println("File " + Constants.SERIALIZED_FILE + " created."); |
|
29 |
|
|
30 |
/* |
|
31 |
FileInputStream fis = new FileInputStream(Constants.FILE_NAME); |
|
32 |
ObjectInputStream ois = new ObjectInputStream(fis); |
|
33 |
s = (Example) ois.readObject(); |
|
34 |
System.out.println("DATA: " + s.string + " " + s.number + " " + Arrays.toString(s.variable.array)); |
|
35 |
ois.close(); |
|
36 |
*/ |
|
37 |
} catch (Exception e) { |
|
38 |
e.printStackTrace(); |
|
39 |
} |
|
40 |
} |
|
41 |
|
|
42 |
} |
demo_mh/Deserializer/src/serialize/Parent.java | ||
---|---|---|
1 |
package serialize; |
|
2 |
|
|
3 |
import java.io.Serializable; |
|
4 |
|
|
5 |
public class Parent implements Serializable { |
|
6 |
|
|
7 |
private static final long serialVersionUID = 1L; |
|
8 |
|
|
9 |
String string = "ABCD"; |
|
10 |
|
|
11 |
} |
Také k dispozici: Unified diff
#7767
Základní projekt, rozpracovaná gramatika.