Projekt

Obecné

Profil

« Předchozí | Další » 

Revize d7ea8c04

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

#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.

Zobrazit rozdíly:

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();

Také k dispozici: Unified diff