Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 9744d223

Přidáno uživatelem Michal Horký před více než 4 roky(ů)

#7767

Zprovoznění exportu do JSON. Přidáno zpracovávání binárních zazipovaných dat. Úprava struktury.

Zobrazit rozdíly:

demo_mh/Deserializer/src/deserialize/Grammar.java
1 1
package deserialize;
2 2

  
3
import java.io.ByteArrayInputStream;
4
import java.io.DataInputStream;
5
import java.nio.ByteBuffer;
6 3
import java.util.ArrayList;
7 4
import java.util.HashMap;
8 5
import java.util.List;
9 6

  
7
import org.json.simple.JSONObject;
8

  
10 9
/**
11 10
 * https://docs.oracle.com/javase/7/docs/platform/serialization/spec/protocol.html
12 11
 */
......
14 13
	
15 14
	private final static short STREAM_MAGIC = (short) 0xACED;
16 15
	private final static short STREAM_VERSION = 5;
17
	private final static byte TC_NULL = (byte) 0x70;
18
	private final static byte TC_REFERENCE = (byte) 0x71;
19
	private final static byte TC_CLASSDESC = (byte) 0x72;
20
	private final static byte TC_OBJECT = (byte) 0x73;
21
	private final static byte TC_STRING = (byte) 0x74;
22
	private final static byte TC_ARRAY = (byte) 0x75;
23
	private final static byte TC_CLASS = (byte) 0x76;
24
	private final static byte TC_BLOCKDATA = (byte) 0x77;
25
	private final static byte TC_ENDBLOCKDATA = (byte) 0x78;
26
	private final static byte TC_RESET = (byte) 0x79;
27
	private final static byte TC_BLOCKDATALONG = (byte) 0x7A;
28
	private final static byte TC_EXCEPTION = (byte) 0x7B;
29
	private final static byte TC_LONGSTRING = (byte) 0x7C;
30
	private final static byte TC_PROXYCLASSDESC = (byte) 0x7D;
31
	private final static byte TC_ENUM = (byte) 0x7E;
16
	private final static byte TC_NULL = 0x70;
17
	private final static byte TC_REFERENCE = 0x71;
18
	private final static byte TC_CLASSDESC = 0x72;
19
	private final static byte TC_OBJECT = 0x73;
20
	private final static byte TC_STRING = 0x74;
21
	private final static byte TC_ARRAY = 0x75;
22
	private final static byte TC_CLASS = 0x76;
23
	private final static byte TC_BLOCKDATA = 0x77;
24
	private final static byte TC_ENDBLOCKDATA = 0x78;
25
	private final static byte TC_RESET = 0x79;
26
	private final static byte TC_BLOCKDATALONG = 0x7A;
27
	private final static byte TC_EXCEPTION = 0x7B;
28
	private final static byte TC_LONGSTRING = 0x7C;
29
	private final static byte TC_PROXYCLASSDESC = 0x7D;
30
	private final static byte TC_ENUM = 0x7E;
32 31
	private final static int baseWireHandle = (int) 0x7E0000;
33 32
	
34
	final static byte SC_WRITE_METHOD = (byte) 0x01; // if SC_SERIALIZABLE
35
	final static byte SC_BLOCK_DATA = (byte) 0x08; // if SC_EXTERNALIZABLE
36
	final static byte SC_SERIALIZABLE = (byte) 0x02;
37
	final static byte SC_EXTERNALIZABLE = (byte) 0x04;
38
	final static byte SC_ENUM = (byte) 0x10;
33
	final static byte SC_WRITE_METHOD = 0x01; // if SC_SERIALIZABLE
34
	final static byte SC_BLOCK_DATA = 0x08; // if SC_EXTERNALIZABLE
35
	final static byte SC_SERIALIZABLE = 0x02;
36
	final static byte SC_EXTERNALIZABLE = 0x04;
37
	final static byte SC_ENUM = 0x10;
39 38

  
40
	HashMap<Character, String> primTypeCodes;
41
	HashMap<Character, String> objTypeCodes;
42
	
43
	
44
	// Note. (XXX) This token has the XXX type specified
45
	
46
	
47 39
	private List<Byte> buffer;
48
	public Grammar(List<Byte> buffer) {
49
		this.buffer = buffer;
50
		
51
		primTypeCodes = new HashMap<Character, String>();
52
		primTypeCodes.put('B', "byte");
53
		primTypeCodes.put('C', "char");
54
		primTypeCodes.put('D', "double");
55
		primTypeCodes.put('F', "float");
56
		primTypeCodes.put('I', "integer");
57
		primTypeCodes.put('J', "long");
58
		primTypeCodes.put('S', "short");
59
		primTypeCodes.put('Z', "boolean");
60
		
61
		objTypeCodes = new HashMap<Character, String>();
62
		objTypeCodes.put('[', "array");
63
		objTypeCodes.put('L', "object");
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
		}
64 45
	}
65 46
	
66 47
	
......
104 85
		return str;
105 86
	}
106 87
	
107
	
108
	
109
	
110
	
111 88
	public String readUtf() {
112 89
		short length = readShort();
113 90
		return readString(length);
......
118 95
		return readString(length);
119 96
	}
120 97
	
98
	public float readFloat() {
99
		return Float.intBitsToFloat(readInt());
100
	}
121 101
	
102
	public double readDouble() {
103
		return Double.longBitsToDouble(readLong());
104
	}
122 105
	
106
	public boolean readBoolean() {
107
		// TODO zkontrolovat
108
		return readByte() != 0;
109
	}
123 110
	
124 111
	
112
	List<ClassDescription> classes = new ArrayList<ClassDescription>();
113
	ClassDescription actClass = null;
125 114
	
126
	
127
	
128
	public void start() {
115
	public JSONObject process() {
129 116
		System.out.println("Kontrola hlavicky souboru: " + (readShort() == STREAM_MAGIC && readShort() == STREAM_VERSION));
117
		
130 118
		while (!buffer.isEmpty()) {
131
			object();
119
			try {
120
				object();
121
			} catch (Exception e) {
122
				break;
123
			}
132 124
		}
125
		
126
		return classes.get(0).getJSONObject();
127
		// System.out.println(classes.get(0).toString());
133 128
	}
134 129
	
135 130
	// FOR VALUES : 
......
185 180
			case TC_ARRAY: {
186 181
					System.out.println("X3");
187 182
					ClassDescription desc = (ClassDescription) object();
188
					int size = readInt();
189
					retValue = desc.className + "[" + size + "]";
183
					// TODO int size = readInt();
184
					retValue = desc.className;; // TODO + "[" + size + "]";
190 185
				}
191 186
				break;
192 187
			case TC_STRING: {
188
					System.out.println("X4");
193 189
					String str = readUtf();
194
					System.out.println("X4 " + str);
195 190
					retValue = str;
196 191
				}
197 192
				break;
......
218 213
					desc.serialVersionUID = readLong();
219 214
					desc.flags = readByte();
220 215
					desc.isClass = true;
221
					desc.variables = fields();
216
					desc.setVariables(fields());
217
					
218
					if (actClass == null) {
219
						actClass = desc;
220
					}
221
					// TODO
222
					actClass.addParent(desc);
223
					
222 224
					// TODO object(null); classDesc();
223
					System.out.println(desc.toString());
225
					// TODO System.out.println(desc.toString());
224 226
					retValue = desc;
225 227
				}
226 228
				break;
......
237 239
					// TODO
238 240
					System.out.println("---NULL---");
239 241
					retValue = null;
242
					
243
					classes.add(actClass);
244
					actClass = null;
245
					while (true) {
246
						Variable actVar = classes.get(classes.size() - 1).getActVariable();
247
						if (actVar == null) {
248
							break;
249
						}
250
						
251
						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;
260
						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();
268
							System.out.println("------------- VELIKOST POLE = " + count);
269
							for (int i = 0; i < count; i++) {
270
								System.out.println("------------- HODNOTA = " + (char) readShort());
271
							}
272
							//actVar.setValue(readByte());
273
							break;
274
						case 'L': actVar.setValue(object()); break;
275
						}
276
					}
240 277
				}
241 278
				break;
242 279
			case TC_REFERENCE: {
......
286 323
		return retValue;
287 324
	}
288 325
	
289
	public List<String> fields() {
290
		List<String> rows = new ArrayList<String>();
326
	public List<Variable> fields() {
327
		List<Variable> variables = new ArrayList<Variable>();
291 328
		
292 329
		short count = readShort();
293 330
		for (int i = 0; i < count; i++) {
294 331
			char type = (char) readByte();
295
			
296
			String row;
297
			if (primTypeCodes.containsKey(type)) {
298
				row = primTypeCodes.get(type) + " " + readUtf();
299
			} else {
300
				String variable = readUtf();
301
				String dataType = (String) object();
302
				dataType = dataType.substring(1);
303
				
304
				if (primTypeCodes.containsKey(dataType.charAt(0)))
305
					dataType = primTypeCodes.get(dataType.charAt(0)); // remove type
306
				
307
				row = dataType + " " + variable;
308
				if (objTypeCodes.get(type) == "array")
309
					row += "[]";
310
			}
311
			
312
			rows.add(row);
332
			String variable = readUtf();
333
			String typeName = null;
334
			if (!DataType.isPrimaryType(type))
335
				typeName = (String) object();
336
			variables.add(new Variable(type, variable, typeName));
313 337
		}
314 338
		
315
		return rows;
339
		return variables;
316 340
	}
317 341
	
318 342
}

Také k dispozici: Unified diff