Revize 9744d223
Přidáno uživatelem Michal Horký před více než 4 roky(ů)
demo_mh/Deserializer/src/deserialize/Deserializer.java | ||
---|---|---|
1 | 1 |
package deserialize; |
2 | 2 |
|
3 |
import java.io.BufferedInputStream; |
|
4 | 3 |
import java.io.File; |
5 |
import java.io.FileInputStream; |
|
6 |
import java.io.IOException; |
|
7 |
import java.io.ObjectInputStream; |
|
8 | 4 |
|
9 |
import general.*; |
|
5 |
import javafx.application.Application; |
|
6 |
import javafx.geometry.Insets; |
|
7 |
import javafx.geometry.Pos; |
|
8 |
import javafx.scene.Parent; |
|
9 |
import javafx.scene.Scene; |
|
10 |
import javafx.scene.control.Button; |
|
11 |
import javafx.scene.control.Label; |
|
12 |
import javafx.scene.control.Separator; |
|
13 |
import javafx.scene.layout.HBox; |
|
14 |
import javafx.scene.layout.Pane; |
|
15 |
import javafx.scene.layout.Priority; |
|
16 |
import javafx.scene.layout.VBox; |
|
17 |
import javafx.stage.FileChooser; |
|
18 |
import javafx.stage.Stage; |
|
10 | 19 |
|
11 |
public class Deserializer { |
|
20 |
import serialize.Example; |
|
21 |
|
|
22 |
public class Deserializer extends Application { |
|
23 |
|
|
24 |
private Stage stage; |
|
25 |
private File inputFile; |
|
26 |
private File outputFile; |
|
27 |
|
|
28 |
// TODO delete after deserializer debugging is complete... |
|
29 |
private boolean testing; |
|
12 | 30 |
|
13 | 31 |
public static void main(String[] args) { |
14 |
try { |
|
15 |
|
|
16 |
// Try to deserialize classically. |
|
17 |
FileInputStream fis = new FileInputStream(Constants.SERIALIZED_FILE); |
|
18 |
ObjectInputStream ois = new ObjectInputStream(fis); |
|
19 |
Deserializer d = (Deserializer) ois.readObject(); |
|
20 |
System.out.println(d.toString()); |
|
21 |
ois.close(); |
|
22 |
|
|
23 |
} catch (IOException e) { |
|
24 |
|
|
25 |
// Stream error. |
|
26 |
System.out.println("Chyba pri otevirani (uzavirani) streamu."); |
|
27 |
e.printStackTrace(); |
|
28 |
|
|
29 |
} catch (Exception e) { |
|
30 |
|
|
31 |
// Others: ClassNotFoundException, ClassCastException, ... |
|
32 |
// Usually due to problems with classical deserialization. |
|
33 |
byte buffer[]; |
|
34 |
try { |
|
35 |
File f = new File(Constants.SERIALIZED_FILE); |
|
36 |
buffer = new byte[(int) f.length()]; |
|
37 |
|
|
38 |
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(f)); |
|
39 |
fis.read(buffer); |
|
40 |
fis.close(); |
|
41 |
|
|
42 |
System.out.println("Doslo k chybe pri deserializaci."); |
|
43 |
System.out.println("Spoustim univerzalni deserializer."); |
|
44 |
|
|
45 |
ToJSON thread = new ToJSON(buffer); |
|
46 |
thread.start(); |
|
47 |
try { |
|
48 |
thread.join(); |
|
49 |
System.out.println("JSON byl vytvoren."); |
|
50 |
} catch (InterruptedException e1) { |
|
51 |
e1.printStackTrace(); |
|
52 |
} |
|
53 |
|
|
54 |
} catch (IOException e1) { |
|
55 |
e1.printStackTrace(); |
|
56 |
} |
|
57 |
|
|
32 |
launch(args); |
|
33 |
} |
|
34 |
|
|
35 |
@Override |
|
36 |
public void init() throws Exception { |
|
37 |
super.init(); |
|
38 |
testing = false; |
|
39 |
} |
|
40 |
|
|
41 |
@Override |
|
42 |
public void start(Stage stage) throws Exception { |
|
43 |
this.stage = stage; |
|
44 |
stage.setTitle("Java object universal deserializer"); |
|
45 |
stage.setScene(createScene()); |
|
46 |
stage.setMinWidth(400); |
|
47 |
stage.show(); |
|
48 |
} |
|
49 |
|
|
50 |
private Scene createScene() { |
|
51 |
return new Scene(createLayout()); |
|
52 |
} |
|
53 |
|
|
54 |
private Parent createLayout() { |
|
55 |
VBox layout = new VBox(); |
|
56 |
layout.setPadding(new Insets(10.0)); |
|
57 |
layout.setSpacing(10.0); |
|
58 |
|
|
59 |
Label forDefaultBinFile = new Label("Chcete vytvo?it defaultn? bin?rn? soubor?"); |
|
60 |
Button createBinFile = new Button("Vytvo?it"); |
|
61 |
createBinFile.setOnAction(event -> { |
|
62 |
FileChooser fCh = new FileChooser(); |
|
63 |
fCh.setInitialDirectory(new File(".")); |
|
64 |
fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("Binary Files", "*.out")); |
|
65 |
File file = fCh.showSaveDialog(stage); |
|
66 |
if (file != null) { |
|
67 |
createSerializedFile(file); |
|
68 |
} |
|
69 |
}); |
|
70 |
final Pane spacer = new Pane(); |
|
71 |
HBox.setHgrow(spacer, Priority.ALWAYS); |
|
72 |
HBox header = new HBox(); |
|
73 |
header.setAlignment(Pos.CENTER_LEFT); |
|
74 |
header.getChildren().add(forDefaultBinFile); |
|
75 |
header.getChildren().add(spacer); |
|
76 |
header.getChildren().add(createBinFile); |
|
77 |
|
|
78 |
Label forInputFile = new Label("Soubor k deserializaci:"); |
|
79 |
Label inputFile = new Label(" Je?t? nebyl vybr?n ??dn? soubor..."); |
|
80 |
inputFile.setStyle("-fx-font-style: italic;"); |
|
81 |
VBox forInput = new VBox(); |
|
82 |
forInput.getChildren().add(forInputFile); |
|
83 |
forInput.getChildren().add(inputFile); |
|
84 |
|
|
85 |
Label forOutputFile = new Label("V?stupn? soubor:"); |
|
86 |
Label outputFile = new Label(" Je?t? nebyl vybr?n ??dn? soubor..."); |
|
87 |
outputFile.setStyle("-fx-font-style: italic;"); |
|
88 |
VBox forOutput = new VBox(); |
|
89 |
forOutput.getChildren().add(forOutputFile); |
|
90 |
forOutput.getChildren().add(outputFile); |
|
91 |
|
|
92 |
Button convert = new Button("P?ev?st"); |
|
93 |
convert.setOnAction(event -> { |
|
94 |
ToJSON thread = new ToJSON(testing ? new File("data.out") : this.inputFile, this.outputFile); |
|
95 |
thread.start(); |
|
96 |
}); |
|
97 |
convert.setDisable(!testing); |
|
98 |
Button setInputFile = new Button("Vstupn? soubor"); |
|
99 |
setInputFile.setOnAction(event -> { |
|
100 |
FileChooser fCh = new FileChooser(); |
|
101 |
fCh.setInitialDirectory(new File(".")); |
|
102 |
// The input can be a zip file or file without a specific extension -> file chooser without extension filters... |
|
103 |
// fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("Binary Files", "*.out")); |
|
104 |
File file = fCh.showOpenDialog(stage); |
|
105 |
if (file != null) { |
|
106 |
this.inputFile = file; |
|
107 |
inputFile.setText(this.inputFile == null ? "" : this.inputFile.getAbsolutePath()); |
|
108 |
inputFile.setStyle("-fx-font-weight: bold;"); |
|
109 |
convert.setDisable(this.inputFile == null || this.outputFile == null); |
|
110 |
} |
|
111 |
}); |
|
112 |
Button setOutputFile = new Button("V?stupn? soubor"); |
|
113 |
setOutputFile.setOnAction(event -> { |
|
114 |
FileChooser fCh = new FileChooser(); |
|
115 |
fCh.setInitialDirectory(new File(".")); |
|
116 |
fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("JSON Files", "*.json")); |
|
117 |
File file = fCh.showSaveDialog(stage); |
|
118 |
if (file != null) { |
|
119 |
this.outputFile = file; |
|
120 |
outputFile.setText(this.outputFile == null ? "" : this.outputFile.getAbsolutePath()); |
|
121 |
outputFile.setStyle("-fx-font-weight: bold;"); |
|
122 |
convert.setDisable(this.inputFile == null || this.outputFile == null); |
|
123 |
} |
|
124 |
}); |
|
125 |
HBox footer = new HBox(); |
|
126 |
footer.setSpacing(10.0); |
|
127 |
footer.setAlignment(Pos.CENTER_RIGHT); |
|
128 |
footer.getChildren().add(setInputFile); |
|
129 |
footer.getChildren().add(setOutputFile); |
|
130 |
footer.getChildren().add(convert); |
|
131 |
|
|
132 |
layout.getChildren().add(header); |
|
133 |
layout.getChildren().add(new Separator()); |
|
134 |
layout.getChildren().add(forInput); |
|
135 |
layout.getChildren().add(forOutput); |
|
136 |
layout.getChildren().add(footer); |
|
137 |
|
|
138 |
return layout; |
|
139 |
} |
|
140 |
|
|
141 |
private void createSerializedFile(File file) { |
|
142 |
String title = "Vytvo?en? defaultn?ho souboru"; |
|
143 |
if (Example.serialize(file)) { |
|
144 |
Report.info(title, null, "Vytvo?en? prob?hlo v po??dku."); |
|
145 |
} else { |
|
146 |
Report.error(title, null, "Vytvo?en? souboru se nepovedlo."); |
|
58 | 147 |
} |
59 | 148 |
} |
60 | 149 |
|
Také k dispozici: Unified diff
#7767
Zprovoznění exportu do JSON. Přidáno zpracovávání binárních zazipovaných dat. Úprava struktury.