Revize d7ea8c04
Přidáno uživatelem Michal Horký před asi 5 roky(ů)
demo_mh/Deserializer/results.json | ||
---|---|---|
1 |
{"parent":{"variables":{"str":"ABCD"},"name":"class serialize.Parent","interface":"java.io.Serializable"},"variables":{"x":10.0,"variable":{"variables":{"realNum":15.6},"name":"class serialize.Contain","interface":"java.io.Serializable"},"numberX":7},"name":"class serialize.Example","interface":"java.io.Serializable"} |
|
1 |
{"parent":{"variables":{"str":"ABCD","abc":true,"y":59.87},"name":"class serialize.Parent","interface":"java.io.Serializable"},"variables":{"x":10.0,"variable":{"variables":{"array":"NON_IMPLEMENTED"},"name":"class serialize.Contain","interface":"java.io.Serializable"},"numberX":7},"name":"class serialize.Example","interface":"java.io.Serializable"} |
demo_mh/Deserializer/src/deserialize/ByteReader.java | ||
---|---|---|
1 |
package deserialize; |
|
2 |
|
|
3 |
import java.nio.ByteBuffer; |
|
4 |
import java.util.Arrays; |
|
5 |
|
|
6 |
// TODO osetreni vyjimek... a to celkove vsude. Zatim ponechavano z duvodu testovani. |
|
7 |
|
|
8 |
public class ByteReader { |
|
9 |
|
|
10 |
private byte[] buffer; |
|
11 |
private int actPos; |
|
12 |
|
|
13 |
public ByteReader(byte buffer[]) { |
|
14 |
this.buffer = Arrays.copyOf(buffer, buffer.length); |
|
15 |
actPos = 0; |
|
16 |
} |
|
17 |
|
|
18 |
public boolean end() { |
|
19 |
return actPos >= buffer.length; |
|
20 |
} |
|
21 |
|
|
22 |
private byte readNext() { |
|
23 |
return buffer[actPos++]; |
|
24 |
} |
|
25 |
|
|
26 |
private long readNumber(int bytes) { |
|
27 |
long result = 0; |
|
28 |
for (int i = (bytes - 1) * Byte.SIZE; i >= 0; i -= Byte.SIZE) { |
|
29 |
result = result | ((readNext() & 0xFF) << i); |
|
30 |
} |
|
31 |
return result; |
|
32 |
} |
|
33 |
|
|
34 |
public byte readByte() { |
|
35 |
return readNext(); |
|
36 |
} |
|
37 |
|
|
38 |
public char readUnsignedByte() { |
|
39 |
return (char) readNumber(Byte.BYTES); |
|
40 |
} |
|
41 |
|
|
42 |
public short readShort() { |
|
43 |
return (short) readNumber(Short.BYTES); |
|
44 |
} |
|
45 |
|
|
46 |
public char readChar() { |
|
47 |
return (char) readShort(); |
|
48 |
} |
|
49 |
|
|
50 |
public int readInt() { |
|
51 |
return (int) readNumber(Integer.BYTES); |
|
52 |
} |
|
53 |
|
|
54 |
public long readLong() { |
|
55 |
return readNumber(Long.BYTES); |
|
56 |
} |
|
57 |
|
|
58 |
public String readString(long length) { |
|
59 |
String str = ""; |
|
60 |
for (int i = 0; i < length; i++) { |
|
61 |
str += (char) readByte(); |
|
62 |
} |
|
63 |
return str; |
|
64 |
} |
|
65 |
|
|
66 |
public String readUtf() { |
|
67 |
short length = readShort(); |
|
68 |
return readString(length); |
|
69 |
} |
|
70 |
|
|
71 |
public String readLongUtf() { |
|
72 |
long length = readLong(); |
|
73 |
return readString(length); |
|
74 |
} |
|
75 |
|
|
76 |
public float readFloat() { |
|
77 |
return Float.intBitsToFloat(readInt()); |
|
78 |
} |
|
79 |
|
|
80 |
public double readDouble() { |
|
81 |
// return Double.longBitsToDouble(readLong()); DOESNT WORK... |
|
82 |
byte b[] = new byte[Long.BYTES]; |
|
83 |
for (int i = 0; i < b.length; i++) { |
|
84 |
b[i] = readByte(); |
|
85 |
} |
|
86 |
return ByteBuffer.wrap(b).getDouble(); |
|
87 |
} |
|
88 |
|
|
89 |
public boolean readBoolean() { |
|
90 |
return readByte() != 0; |
|
91 |
} |
|
92 |
|
|
93 |
} |
demo_mh/Deserializer/src/deserialize/ClassDescription.java | ||
---|---|---|
21 | 21 |
private List<Variable> variables = new ArrayList<Variable>(); |
22 | 22 |
private int actVariableIndex = -1; |
23 | 23 |
|
24 |
|
|
25 |
|
|
26 |
public List<ClassDescription> getParents() { |
|
27 |
return parents; |
|
28 |
} |
|
29 |
|
|
30 |
public List<Variable> getVariables() { |
|
31 |
return variables; |
|
32 |
} |
|
33 |
|
|
34 |
|
|
35 |
|
|
24 | 36 |
public Variable nextVariable() { |
25 | 37 |
if (++actVariableIndex < variables.size()) { |
26 | 38 |
return variables.get(actVariableIndex); |
demo_mh/Deserializer/src/deserialize/DataType.java | ||
---|---|---|
3 | 3 |
import java.util.HashMap; |
4 | 4 |
|
5 | 5 |
public class DataType { |
6 |
|
|
6 |
|
|
7 | 7 |
public final static HashMap<Character, String> primTypeCodes; |
8 | 8 |
public final static HashMap<Character, String> objTypeCodes; |
9 | 9 |
|
demo_mh/Deserializer/src/deserialize/Grammar.java | ||
---|---|---|
1 | 1 |
package deserialize; |
2 | 2 |
|
3 |
import java.nio.ByteBuffer; |
|
3 | 4 |
import java.util.ArrayList; |
4 | 5 |
import java.util.HashMap; |
5 | 6 |
import java.util.List; |
... | ... | |
36 | 37 |
final static byte SC_EXTERNALIZABLE = 0x04; |
37 | 38 |
final static byte SC_ENUM = 0x10; |
38 | 39 |
|
39 |
private List<Byte> buffer; |
|
40 |
public Grammar(byte buffer[]) { |
|
41 |
this.buffer = new ArrayList<Byte>(); |
|
42 |
for (int i = 0; i < buffer.length; i++) { |
|
43 |
this.buffer.add(buffer[i]); |
|
44 |
} |
|
45 |
} |
|
46 |
|
|
47 |
|
|
48 |
private byte takeFirst() { |
|
49 |
return buffer.remove(0); |
|
50 |
} |
|
51 |
|
|
52 |
public byte readByte() { |
|
53 |
return takeFirst(); |
|
54 |
} |
|
55 |
|
|
56 |
private long readNumber(int bytes) { |
|
57 |
long result = 0; |
|
58 |
for (int i = (bytes - 1) * Byte.SIZE; i >= 0; i -= Byte.SIZE) { |
|
59 |
result = result | ((takeFirst() & 0xFF) << i); |
|
60 |
} |
|
61 |
return result; |
|
62 |
} |
|
63 |
|
|
64 |
public char readUnsignedByte() { |
|
65 |
return (char) readNumber(Byte.BYTES); |
|
66 |
} |
|
67 |
|
|
68 |
public short readShort() { |
|
69 |
return (short) readNumber(Short.BYTES); |
|
70 |
} |
|
71 |
|
|
72 |
public int readInt() { |
|
73 |
return (int) readNumber(Integer.BYTES); |
|
74 |
} |
|
40 |
private ByteReader reader; |
|
75 | 41 |
|
76 |
public long readLong() { |
|
77 |
return readNumber(Long.BYTES); |
|
78 |
} |
|
79 |
|
|
80 |
public String readString(long length) { |
|
81 |
String str = ""; |
|
82 |
for (int i = 0; i < length; i++) { |
|
83 |
str += (char) readByte(); |
|
84 |
} |
|
85 |
return str; |
|
86 |
} |
|
87 |
|
|
88 |
public String readUtf() { |
|
89 |
short length = readShort(); |
|
90 |
return readString(length); |
|
91 |
} |
|
92 |
|
|
93 |
public String readLongUtf() { |
|
94 |
long length = readLong(); |
|
95 |
return readString(length); |
|
96 |
} |
|
97 |
|
|
98 |
public float readFloat() { |
|
99 |
return Float.intBitsToFloat(readInt()); |
|
100 |
} |
|
101 |
|
|
102 |
public double readDouble() { |
|
103 |
return Double.longBitsToDouble(readLong()); |
|
104 |
} |
|
105 |
|
|
106 |
public boolean readBoolean() { |
|
107 |
// TODO zkontrolovat |
|
108 |
return readByte() != 0; |
|
42 |
public Grammar(byte buffer[]) { |
|
43 |
reader = new ByteReader(buffer); |
|
109 | 44 |
} |
110 | 45 |
|
111 | 46 |
|
... | ... | |
113 | 48 |
ClassDescription actClass = null; |
114 | 49 |
|
115 | 50 |
public JSONObject process() { |
116 |
System.out.println("Kontrola hlavicky souboru: " + (readShort() == STREAM_MAGIC && readShort() == STREAM_VERSION));
|
|
51 |
System.out.println("Kontrola hlavicky souboru: " + (reader.readShort() == STREAM_MAGIC && reader.readShort() == STREAM_VERSION));
|
|
117 | 52 |
|
118 |
while (!buffer.isEmpty()) {
|
|
53 |
while (!reader.end()) {
|
|
119 | 54 |
try { |
120 | 55 |
object(); |
121 | 56 |
} catch (Exception e) { |
57 |
e.printStackTrace(); |
|
122 | 58 |
break; |
123 | 59 |
} |
124 | 60 |
} |
... | ... | |
132 | 68 |
// classDesc for the current object |
133 | 69 |
public Object object() { |
134 | 70 |
Object retValue; |
135 |
byte b = readByte(); |
|
71 |
byte b = reader.readByte();
|
|
136 | 72 |
switch (b) { |
137 | 73 |
case TC_OBJECT: { |
138 | 74 |
System.out.println("X1"); |
... | ... | |
181 | 117 |
System.out.println("X3"); |
182 | 118 |
ClassDescription desc = (ClassDescription) object(); |
183 | 119 |
// TODO int size = readInt(); |
184 |
retValue = desc.className;; // TODO + "[" + size + "]";
|
|
120 |
retValue = desc; // TODO + "[" + size + "]"; |
|
185 | 121 |
} |
186 | 122 |
break; |
187 | 123 |
case TC_STRING: { |
188 | 124 |
System.out.println("X4"); |
189 |
String str = readUtf(); |
|
125 |
String str = reader.readUtf();
|
|
190 | 126 |
retValue = str; |
191 | 127 |
} |
192 | 128 |
break; |
193 | 129 |
case TC_LONGSTRING: { |
194 | 130 |
System.out.println("X5"); |
195 |
String str = readLongUtf(); |
|
131 |
String str = reader.readLongUtf();
|
|
196 | 132 |
retValue = str; |
197 | 133 |
} |
198 | 134 |
break; |
... | ... | |
209 | 145 |
case TC_CLASSDESC: { |
210 | 146 |
System.out.println("X7"); |
211 | 147 |
ClassDescription desc = new ClassDescription(); |
212 |
desc.className = readUtf(); |
|
213 |
desc.serialVersionUID = readLong(); |
|
214 |
desc.flags = readByte(); |
|
148 |
desc.className = reader.readUtf();
|
|
149 |
desc.serialVersionUID = reader.readLong();
|
|
150 |
desc.flags = reader.readByte();
|
|
215 | 151 |
desc.isClass = true; |
216 | 152 |
desc.setVariables(fields()); |
217 | 153 |
|
... | ... | |
228 | 164 |
break; |
229 | 165 |
case TC_PROXYCLASSDESC: { |
230 | 166 |
System.out.println("X8"); |
231 |
int count = readInt(); |
|
232 |
String name = readUtf(); |
|
167 |
int count = reader.readInt();
|
|
168 |
String name = reader.readUtf();
|
|
233 | 169 |
retValue = name + "[" + +count + "]"; |
234 | 170 |
// TODO object(null); classDesc(); |
235 | 171 |
|
... | ... | |
249 | 185 |
} |
250 | 186 |
|
251 | 187 |
switch (actVar.getType()) { |
252 |
case 'B': actVar.setValue(readByte()); break; |
|
253 |
case 'C': actVar.setValue((char) readByte()); break; |
|
254 |
case 'D': actVar.setValue(readDouble()); break; |
|
255 |
case 'F': actVar.setValue(readFloat()); break; |
|
256 |
case 'I': actVar.setValue(readInt()); break; |
|
257 |
case 'J': actVar.setValue(readLong()); break; |
|
258 |
case 'S': actVar.setValue(readShort()); break; |
|
259 |
case 'Z': actVar.setValue(readBoolean()); break; |
|
188 |
case 'B': actVar.setValue(reader.readByte()); break;
|
|
189 |
case 'C': actVar.setValue((char) reader.readByte()); break;
|
|
190 |
case 'D': actVar.setValue(reader.readDouble()); break;
|
|
191 |
case 'F': actVar.setValue(reader.readFloat()); break;
|
|
192 |
case 'I': actVar.setValue(reader.readInt()); break;
|
|
193 |
case 'J': actVar.setValue(reader.readLong()); break;
|
|
194 |
case 'S': actVar.setValue(reader.readShort()); break;
|
|
195 |
case 'Z': actVar.setValue(reader.readBoolean()); break;
|
|
260 | 196 |
case '[': |
261 |
// int count = readShort(); |
|
262 |
// TODO |
|
263 |
for (int i = 0; i < 3; i++) { |
|
264 |
object(); |
|
265 |
// System.out.println("DEF: " + " " + (char) xxx + " " + xxx); |
|
266 |
} |
|
267 |
int count = readInt(); |
|
197 |
ClassDescription desccc = (ClassDescription) object(); |
|
198 |
|
|
199 |
object(); // END BLOK |
|
200 |
object(); // NULL |
|
201 |
|
|
202 |
int count = reader.readInt(); |
|
268 | 203 |
System.out.println("------------- VELIKOST POLE = " + count); |
269 | 204 |
for (int i = 0; i < count; i++) { |
270 |
System.out.println("------------- HODNOTA = " + (char) readShort());
|
|
205 |
System.out.println("------------- HODNOTA = " + reader.readChar());
|
|
271 | 206 |
} |
272 | 207 |
//actVar.setValue(readByte()); |
273 | 208 |
break; |
... | ... | |
297 | 232 |
break; |
298 | 233 |
case TC_BLOCKDATA: { |
299 | 234 |
System.out.println("X13"); |
300 |
char size = readUnsignedByte(); |
|
235 |
char size = reader.readUnsignedByte();
|
|
301 | 236 |
// (unsigned byte)<size> (byte)[size]; |
302 | 237 |
retValue = "byte [" + (int) size + "]"; |
303 | 238 |
} |
304 | 239 |
break; |
305 | 240 |
case TC_BLOCKDATALONG: { |
306 | 241 |
System.out.println("X14"); |
307 |
int size = readInt(); |
|
242 |
int size = reader.readInt();
|
|
308 | 243 |
// (int)<size> (byte)[size]; |
309 | 244 |
retValue = "byte [" + size + "]"; |
310 | 245 |
} |
... | ... | |
326 | 261 |
public List<Variable> fields() { |
327 | 262 |
List<Variable> variables = new ArrayList<Variable>(); |
328 | 263 |
|
329 |
short count = readShort(); |
|
264 |
short count = reader.readShort();
|
|
330 | 265 |
for (int i = 0; i < count; i++) { |
331 |
char type = (char) readByte(); |
|
332 |
String variable = readUtf(); |
|
266 |
char type = (char) reader.readByte();
|
|
267 |
String variable = reader.readUtf();
|
|
333 | 268 |
String typeName = null; |
334 | 269 |
if (!DataType.isPrimaryType(type)) |
335 | 270 |
typeName = (String) object(); |
demo_mh/Deserializer/src/deserialize/ToJSON.java | ||
---|---|---|
67 | 67 |
System.out.println("Chyba pri otevirani (uzavirani) streamu pri pokusu o klasickou deserializaci."); |
68 | 68 |
e.printStackTrace(); |
69 | 69 |
|
70 |
return false; |
|
71 |
|
|
70 | 72 |
} catch (Exception e) { |
71 | 73 |
|
72 | 74 |
// Others: ClassNotFoundException, ClassCastException, ... |
... | ... | |
100 | 102 |
|
101 | 103 |
try { |
102 | 104 |
if (entry != null) { |
103 |
array = new byte[(int) entry.getSize()]; |
|
104 |
zipInputStream.read(array); |
|
105 |
//array = new byte[(int) entry.getSize()]; |
|
106 |
array = entry.getExtra(); |
|
107 |
System.out.println(array.length); |
|
108 |
//zipInputStream.read(array); |
|
105 | 109 |
zipInputStream.close(); |
106 | 110 |
} else { |
107 | 111 |
array = new byte[(int) input.length()]; |
demo_mh/Deserializer/src/serialize/Contain.java | ||
---|---|---|
6 | 6 |
|
7 | 7 |
private static final long serialVersionUID = 1L; |
8 | 8 |
|
9 |
// TODO jeste nefunguje char array[] = new char[] {'W', 'X', 'Y', 'Z'};
|
|
10 |
float realNum = 15.6f; |
|
9 |
char array[] = new char[] {'W', 'X', 'Y', 'Z'}; |
|
10 |
// float realNum = 15.6f;
|
|
11 | 11 |
|
12 | 12 |
} |
demo_mh/Deserializer/src/serialize/Parent.java | ||
---|---|---|
7 | 7 |
private static final long serialVersionUID = 1L; |
8 | 8 |
|
9 | 9 |
String str = "ABCD"; |
10 |
boolean abc = true; |
|
11 |
double y = 59.87; |
|
10 | 12 |
|
11 | 13 |
} |
Také k dispozici: Unified diff
#7794
Lehká uprava struktury v projektu. Hlavním důvodem commitu je prohrabání se testovacími daty od zákazníka - vyhovující testovací data ve složce test_input.