Projekt

Obecné

Profil

Stáhnout (2.61 KB) Statistiky
| Větev: | Tag: | Revize:
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.ArrayList;
8
import java.util.HashMap;
9
import java.util.List;
10
import java.util.concurrent.atomic.AtomicInteger;
11

    
12
import org.json.simple.JSONArray;
13
import org.json.simple.JSONObject;
14

    
15
import general.Constants;
16

    
17
public class ToJSON extends Thread {
18
	
19
	private byte buffer[];
20
	
21
	public ToJSON(byte buffer[]) {
22
		this.buffer = buffer;
23
	}
24
	
25
	@Override
26
	public void run() {
27
		super.run();
28
		
29
		List<Byte> buffer = new ArrayList<Byte>();
30
		for (int i = 0; i < this.buffer.length; i++) {
31
			buffer.add(this.buffer[i]);
32
		}
33
		
34
		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
		}*/
72
	}
73
	
74
	@SuppressWarnings("unchecked")
75
	private void save() {
76
		/*
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
 
88
        try {
89
            // Writing to a file
90
            FileWriter fw = new FileWriter(Constants.JSON_FILE);
91
            BufferedWriter bw = new BufferedWriter(fw);
92
            System.out.println("Zapisuji JSON objekt do souboru.");
93
            bw.write(countryObj.toJSONString());
94
            bw.flush();
95
            bw.close();
96
        } catch (IOException e) {
97
            e.printStackTrace();
98
        }
99
        */
100
	}
101
	
102
}
(5-5/5)