Projekt

Obecné

Profil

Stáhnout (3.45 KB) Statistiky
| Větev: | Tag: | Revize:
1
package deserialize;
2

    
3
import java.io.BufferedInputStream;
4
import java.io.BufferedWriter;
5
import java.io.ByteArrayInputStream;
6
import java.io.File;
7
import java.io.FileInputStream;
8
import java.io.FileWriter;
9
import java.io.IOException;
10
import java.io.ObjectInputStream;
11
import java.util.zip.ZipEntry;
12
import java.util.zip.ZipFile;
13

    
14
import org.json.simple.JSONObject;
15

    
16
public class ToJSON extends Thread {
17
	
18
	private File input;
19
	private File output;
20
	
21
	private byte buffer[];
22
	
23
	public ToJSON(File inputFilename, File outputFilename) {
24
		this.input = inputFilename;
25
		this.output = outputFilename;
26
	}
27
	
28
	@Override
29
	public void run() {
30
		super.run();
31
		
32
		buffer = readByteArray();
33
		if (buffer == null) {
34
			// TODO Chyba na??t?n? dat
35
		} else if (deserializeClassically()) {
36
			// TODO Chyba klasick? deserializace
37
		} else {
38
			// Univerzalni deserializer.
39
			convert();
40
		}
41
	}
42
	
43
	private void convert() {
44
		System.out.println("Spoustim univerzalni deserializer.");
45
		
46
		Grammar g = new Grammar(buffer);
47
		JSONObject results = g.process();
48
		
49
		save(results);
50
		
51
		System.out.println("JSON byl vytvoren.");
52
	}
53
	
54
	private boolean deserializeClassically() {
55
		try {
56
			
57
			// Try to deserialize classically.
58
			ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(buffer));
59
			@SuppressWarnings("unused")
60
			Deserializer d = (Deserializer) ois.readObject();
61
			System.out.println("Klasicka deserializace probehla v poradku.");
62
			ois.close();
63
			return true;
64
			
65
		} catch (IOException e) {
66
			
67
			// Stream error.
68
			System.out.println("Chyba pri otevirani (uzavirani) streamu pri pokusu o klasickou deserializaci.");
69
			e.printStackTrace();
70
			
71
		} catch (Exception e) {
72
			
73
			// Others: ClassNotFoundException, ClassCastException, ...
74
			// Usually due to problems with classical deserialization.
75
			
76
		}
77
		return false;
78
	}
79
	
80
	private byte[] readByteArray() {
81
		byte array[] = null;
82
		
83
		/*
84
		 * Note.
85
		 * For ZIP - the first four bytes should be one of the following combinations:
86
		 *     50 4B 03 04
87
		 *     50 4B 05 06 (empty archive)
88
		 *     50 4B 07 08 (spanned archive)
89
		 * Source: https://en.wikipedia.org/wiki/List_of_file_signatures
90
		 */
91
		ZipFile zipFile = null;
92
		ZipEntry entry = null;
93
	    try {
94
	    	zipFile = new ZipFile(input);
95
	        entry = (ZipEntry) zipFile.entries().nextElement();
96
	        System.out.println("Velikost ZIP dat je " + entry.getSize() + "B.");
97
		} catch (Exception e) {
98
			System.out.println("Soubor neni zazipovany...");
99
		}
100
	    
101
	    try {
102
	    	if (entry != null) {
103
	    		array = new byte[(int) entry.getSize()];
104
	    		zipFile.getInputStream(entry).read(array);
105
		    } else {
106
		    	array = new byte[(int) input.length()];
107
				BufferedInputStream bis = new BufferedInputStream(new FileInputStream(input));
108
				bis.read(array);
109
				bis.close();
110
		    }
111
	    	System.out.println("Byty ze souboru byly v poradku nacteny.");
112
	    } catch (IOException e) {
113
	    	System.out.println("Pri nacitani bytu doslo k chybe.");
114
	    	return null;
115
	    }
116
	    
117
	    return array;
118
	}
119
	
120
	private void save(JSONObject obj) {
121
        try {
122
            FileWriter fw = new FileWriter(output);
123
            BufferedWriter bw = new BufferedWriter(fw);
124
            bw.write(obj.toJSONString());
125
            bw.flush();
126
            bw.close();
127
            Report.info("JSON", null, "Ulo?en? souboru prob?hlo v po??dku.");
128
        } catch (IOException e) {
129
            e.printStackTrace();
130
            Report.info("JSON", null, "P?i ukl?d?n? souboru nastala chyba.");
131
        }
132
	}
133
	
134
}
(8-8/9)