Revize 49fd8648
Přidáno uživatelem Michal Horký před téměř 5 roky(ů)
project/Deserializer/.classpath | ||
---|---|---|
1 | 1 |
<?xml version="1.0" encoding="UTF-8"?> |
2 | 2 |
<classpath> |
3 |
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"> |
|
4 |
<attributes> |
|
5 |
<attribute name="module" value="true"/> |
|
6 |
</attributes> |
|
7 |
</classpathentry> |
|
8 | 3 |
<classpathentry kind="src" path="src"/> |
9 |
<classpathentry kind="lib" path="libs/javafx-sdk-11.0.2/lib/javafx.controls.jar"/> |
|
10 |
<classpathentry kind="lib" path="libs/javafx-sdk-11.0.2/lib/javafx-swt.jar"/> |
|
11 |
<classpathentry kind="lib" path="libs/javafx-sdk-11.0.2/lib/javafx.base.jar"/> |
|
12 |
<classpathentry kind="lib" path="libs/javafx-sdk-11.0.2/lib/javafx.fxml.jar"/> |
|
13 |
<classpathentry kind="lib" path="libs/javafx-sdk-11.0.2/lib/javafx.graphics.jar"/> |
|
14 |
<classpathentry kind="lib" path="libs/javafx-sdk-11.0.2/lib/javafx.media.jar"/> |
|
15 |
<classpathentry kind="lib" path="libs/javafx-sdk-11.0.2/lib/javafx.swing.jar"/> |
|
16 |
<classpathentry kind="lib" path="libs/javafx-sdk-11.0.2/lib/javafx.web.jar"/> |
|
4 |
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre1.8.0_241"/> |
|
17 | 5 |
<classpathentry kind="output" path="bin"/> |
18 | 6 |
</classpath> |
project/Deserializer/.settings/org.eclipse.core.resources.prefs | ||
---|---|---|
1 |
eclipse.preferences.version=1 |
|
2 |
encoding/<project>=UTF-8 |
project/Deserializer/css/main.css | ||
---|---|---|
1 |
.menu-graphics { |
|
2 |
-fx-font-size: 13px; |
|
3 |
-fx-text-fill: #000000; |
|
4 |
} |
|
5 |
|
|
6 |
.menu-item:focused .menu-graphics { |
|
7 |
-fx-text-fill: #ffffff; |
|
8 |
} |
|
9 |
|
project/Deserializer/results.json | ||
---|---|---|
1 |
{ |
|
2 |
class java.util.Hashtable : { |
|
3 |
"loadFactor" : "0.75", |
|
4 |
"threshold" : "8" |
|
5 |
} |
|
6 |
}{ |
|
7 |
class java.util.Hashtable : { |
|
8 |
"loadFactor" : "0.75", |
|
9 |
"threshold" : "17" |
|
10 |
} |
|
11 |
}{ |
|
12 |
class java.util.Hashtable : { |
|
13 |
"loadFactor" : "0.75", |
|
14 |
"threshold" : "8" |
|
15 |
} |
|
16 |
}[blockdata 0x00: 8 bytes]{ |
|
17 |
class java.util.Hashtable : { |
|
18 |
"loadFactor" : "0.75", |
|
19 |
"threshold" : "8" |
|
20 |
} |
|
21 |
}{ |
|
22 |
class java.util.Hashtable : { |
|
23 |
"loadFactor" : "0.75", |
|
24 |
"threshold" : "8" |
|
25 |
} |
|
26 |
}[blockdata 0x00: 8 bytes]{ |
|
27 |
class java.util.Hashtable : { |
|
28 |
"loadFactor" : "0.75", |
|
29 |
"threshold" : "8" |
|
30 |
} |
|
31 |
}{ |
|
32 |
class java.util.Hashtable : { |
|
33 |
"loadFactor" : "0.75", |
|
34 |
"threshold" : "8" |
|
35 |
} |
|
36 |
}[blockdata 0x00: 8 bytes]{ |
|
37 |
class java.util.Hashtable : { |
|
38 |
"loadFactor" : "0.75", |
|
39 |
"threshold" : "8" |
|
40 |
} |
|
41 |
}{ |
|
42 |
class java.util.Hashtable : { |
|
43 |
"loadFactor" : "0.75", |
|
44 |
"threshold" : "8" |
|
45 |
} |
|
46 |
}[blockdata 0x00: 8 bytes]{ |
|
47 |
class java.util.Hashtable : { |
|
48 |
"loadFactor" : "0.75", |
|
49 |
"threshold" : "8" |
|
50 |
} |
|
51 |
}{ |
|
52 |
class java.util.Hashtable : { |
|
53 |
"loadFactor" : "0.75", |
|
54 |
"threshold" : "8" |
|
55 |
} |
|
56 |
}[blockdata 0x00: 8 bytes]{ |
|
57 |
class java.util.Hashtable : { |
|
58 |
"loadFactor" : "0.75", |
|
59 |
"threshold" : "8" |
|
60 |
} |
|
61 |
}{ |
|
62 |
class java.util.Hashtable : { |
|
63 |
"loadFactor" : "0.75", |
|
64 |
"threshold" : "8" |
|
65 |
} |
|
66 |
}[blockdata 0x00: 8 bytes]{ |
|
67 |
class java.util.Hashtable : { |
|
68 |
"loadFactor" : "0.75", |
|
69 |
"threshold" : "8" |
|
70 |
} |
|
71 |
}{ |
|
72 |
class java.util.Hashtable : { |
|
73 |
"loadFactor" : "0.75", |
|
74 |
"threshold" : "8" |
|
75 |
} |
|
76 |
}[blockdata 0x00: 8 bytes]{ |
|
77 |
class java.util.Hashtable : { |
|
78 |
"loadFactor" : "0.75", |
|
79 |
"threshold" : "8" |
|
80 |
} |
|
81 |
}{ |
|
82 |
class java.util.Hashtable : { |
|
83 |
"loadFactor" : "0.75", |
|
84 |
"threshold" : "8" |
|
85 |
} |
|
86 |
}[blockdata 0x00: 8 bytes]{ |
|
87 |
class java.util.Hashtable : { |
|
88 |
"loadFactor" : "0.75", |
|
89 |
"threshold" : "8" |
|
90 |
} |
|
91 |
}{ |
|
92 |
class java.util.Hashtable : { |
|
93 |
"loadFactor" : "0.75", |
|
94 |
"threshold" : "8" |
|
95 |
} |
|
96 |
}[blockdata 0x00: 8 bytes]{ |
|
97 |
class java.util.Hashtable : { |
|
98 |
"loadFactor" : "0.75", |
|
99 |
"threshold" : "8" |
|
100 |
} |
|
101 |
}{ |
|
102 |
class java.util.Hashtable : { |
|
103 |
"loadFactor" : "0.75", |
|
104 |
"threshold" : "8" |
|
105 |
} |
|
106 |
}[blockdata 0x00: 8 bytes] |
|
1 |
Ahoj |
project/Deserializer/src/CLI.java | ||
---|---|---|
1 |
import java.io.File; |
|
2 |
import java.io.IOException; |
|
3 |
import java.util.HashMap; |
|
4 |
|
|
5 |
import io.FileWorker; |
|
6 |
import javafx.application.Application; |
|
7 |
|
|
8 |
public class CLI implements IConversionResults { |
|
9 |
|
|
10 |
private static class Option { |
|
11 |
String abbr; |
|
12 |
String about; |
|
13 |
|
|
14 |
public Option(String abbr) { |
|
15 |
// Convert input argument to option. |
|
16 |
this.abbr = abbr; |
|
17 |
this.about = null; |
|
18 |
} |
|
19 |
|
|
20 |
public Option(String abbr, String about) { |
|
21 |
this.abbr = abbr; |
|
22 |
this.about = about; |
|
23 |
} |
|
24 |
|
|
25 |
@Override |
|
26 |
public String toString() { |
|
27 |
return abbr + "\t" + about; |
|
28 |
} |
|
29 |
|
|
30 |
@Override |
|
31 |
public int hashCode() { |
|
32 |
return abbr.hashCode(); |
|
33 |
} |
|
34 |
|
|
35 |
@Override |
|
36 |
public boolean equals(Object obj) { |
|
37 |
if (this == obj) { |
|
38 |
return true; |
|
39 |
} |
|
40 |
if (obj == null) { |
|
41 |
return false; |
|
42 |
} |
|
43 |
if (this.getClass() != obj.getClass()) { |
|
44 |
return false; |
|
45 |
} |
|
46 |
Option other = (Option) obj; |
|
47 |
return this.abbr.equals(other.abbr); |
|
48 |
} |
|
49 |
} |
|
50 |
|
|
51 |
private static HashMap<Option, Runnable> options; |
|
52 |
private static boolean printJSON; |
|
53 |
private static File outputFile; |
|
54 |
|
|
55 |
static { |
|
56 |
options = new HashMap<Option, Runnable>(); |
|
57 |
options.put(new Option("-?", "\tPrint this help message."), () -> { |
|
58 |
help(); |
|
59 |
}); |
|
60 |
options.put(new Option("-help", "Print this help message."), () -> { |
|
61 |
help(); |
|
62 |
}); |
|
63 |
options.put(new Option("-print", "Text..."), () -> { |
|
64 |
setPrintJSON(); |
|
65 |
}); |
|
66 |
printJSON = false; |
|
67 |
} |
|
68 |
|
|
69 |
private static String printOptions() { |
|
70 |
String result = ""; |
|
71 |
for (Option option : options.keySet()) { |
|
72 |
result += " " + option + "\n"; |
|
73 |
} |
|
74 |
return result; |
|
75 |
} |
|
76 |
|
|
77 |
private static void help() { |
|
78 |
System.out.println("\n" |
|
79 |
+ "Usage:" + "\n" |
|
80 |
+ " java ... -jar JOU_Deserializer.jar [-options] [args...]" + "\n" |
|
81 |
+ "\n" |
|
82 |
+ "...:" + "\n" |
|
83 |
+ " When running the app in the JVM 11, the following VM arguments may be required:" + "\n" |
|
84 |
+ " --module-path libs\\javafx-sdk-11.0.2\\lib --add-modules=javafx.controls" + "\n" |
|
85 |
+ "\n" |
|
86 |
+ "-options:" + "\n" |
|
87 |
+ printOptions() |
|
88 |
+ "\n" |
|
89 |
+ "args...:" + "\n" |
|
90 |
+ " Input and output file. Output file need not be specified, if options contains -print." + "\n" |
|
91 |
+ "\n" |
|
92 |
+ "Note: if no options and arguments are specified, the GUI is created." + "\n" |
|
93 |
+ "\n" |
|
94 |
+ "Example:" + "\n" |
|
95 |
+ " CLI: java -jar JOU_Deserializer.jar -print input.data output.json" + "\n" |
|
96 |
+ " GUI: java -jar JOU_Deserializer.jar" + "\n"); |
|
97 |
} |
|
98 |
|
|
99 |
private static void setPrintJSON() { |
|
100 |
printJSON = true; |
|
101 |
} |
|
102 |
|
|
103 |
public static void main(String[] args) { |
|
104 |
if (args.length == 0) { |
|
105 |
Application.launch(Deserializer.class, args); |
|
106 |
} else { |
|
107 |
String input = null, output = null; |
|
108 |
|
|
109 |
for (int i = 0; i < args.length; i++) { |
|
110 |
Option o = new Option(args[i]); |
|
111 |
if (options.containsKey(o)) { |
|
112 |
options.get(o).run(); |
|
113 |
} else { |
|
114 |
input = args[i]; |
|
115 |
if (i == args.length - 1) { |
|
116 |
// If we are at the end of the list, check printJSON. |
|
117 |
if (!printJSON) { |
|
118 |
errorMessage("output not defined"); |
|
119 |
return; |
|
120 |
} |
|
121 |
} else { |
|
122 |
output = args[i + 1]; |
|
123 |
break; |
|
124 |
} |
|
125 |
} |
|
126 |
} |
|
127 |
|
|
128 |
if (input == null) { |
|
129 |
// Only options. |
|
130 |
return; |
|
131 |
} |
|
132 |
|
|
133 |
File inputFile = new File(input); |
|
134 |
if (!inputFile.exists()) { |
|
135 |
errorMessage("the input file does not exist"); |
|
136 |
return; |
|
137 |
} |
|
138 |
outputFile = output == null ? null : new File(output); |
|
139 |
|
|
140 |
Converter thread = new Converter(new CLI()); |
|
141 |
thread.start(); |
|
142 |
thread.setInput(inputFile); |
|
143 |
thread.end(); |
|
144 |
try { |
|
145 |
thread.join(); |
|
146 |
} catch (InterruptedException e) { |
|
147 |
errorMessage("while waiting for the converter thread"); |
|
148 |
} |
|
149 |
} |
|
150 |
} |
|
151 |
|
|
152 |
private static void errorMessage(String message) { |
|
153 |
System.out.println("\nERROR: " + message.toUpperCase() + "!\n"); |
|
154 |
} |
|
155 |
|
|
156 |
@Override |
|
157 |
public void loadingInputFileError() { |
|
158 |
System.out.println("There was an error loading the file."); |
|
159 |
} |
|
160 |
|
|
161 |
@Override |
|
162 |
public void completed(String json) { |
|
163 |
if (json != null) { |
|
164 |
System.out.println("Deserialization completed."); |
|
165 |
saveJson(json); |
|
166 |
} else { |
|
167 |
System.out.println("An error occurred while deserializing!"); |
|
168 |
} |
|
169 |
} |
|
170 |
|
|
171 |
private void saveJson(String json) { |
|
172 |
if (outputFile != null) { |
|
173 |
try { |
|
174 |
FileWorker.saveJson(outputFile, json); |
|
175 |
System.out.println("The JSON file was saved correctly."); |
|
176 |
} catch (IOException e) { |
|
177 |
System.out.println("There was an error saving the JSON file."); |
|
178 |
} |
|
179 |
} else { |
|
180 |
// Options contain -print. |
|
181 |
System.out.println(json); |
|
182 |
} |
|
183 |
} |
|
184 |
|
|
185 |
} |
project/Deserializer/src/Converter.java | ||
---|---|---|
1 | 1 |
import java.io.File; |
2 | 2 |
import java.io.IOException; |
3 | 3 |
import java.util.List; |
4 |
import java.util.concurrent.Semaphore; |
|
5 |
import java.util.concurrent.atomic.AtomicBoolean; |
|
6 |
import java.util.concurrent.atomic.AtomicReference; |
|
4 | 7 |
|
5 | 8 |
import jdeserialize.content; |
6 | 9 |
import jdeserialize.jdeserialize; |
7 | 10 |
|
11 |
import io.FileWorker; |
|
12 |
|
|
8 | 13 |
public class Converter extends Thread { |
9 | 14 |
|
10 |
private Deserializer main; |
|
15 |
private IConversionResults ui; |
|
16 |
private AtomicBoolean active; |
|
17 |
|
|
18 |
private AtomicReference<File> input; |
|
19 |
private Semaphore inputAvailable; |
|
20 |
|
|
21 |
public Converter(IConversionResults ui) { |
|
22 |
this.ui = ui; |
|
23 |
active = new AtomicBoolean(true); |
|
24 |
input = new AtomicReference<File>(null); |
|
25 |
inputAvailable = new Semaphore(0); |
|
26 |
} |
|
11 | 27 |
|
12 |
private File input; |
|
13 |
private File output; |
|
28 |
public void end() { |
|
29 |
active.set(false); |
|
30 |
setInput(null); |
|
31 |
} |
|
14 | 32 |
|
15 |
public Converter(Deserializer main, File inputFilename, File outputFilename) { |
|
16 |
this.main = main; |
|
17 |
this.input = inputFilename; |
|
18 |
this.output = outputFilename; |
|
33 |
public void setInput(File input) { |
|
34 |
this.input.set(input); |
|
35 |
inputAvailable.release(); |
|
19 | 36 |
} |
20 | 37 |
|
21 | 38 |
@Override |
22 | 39 |
public void run() { |
23 | 40 |
super.run(); |
41 |
while (active.get()) { |
|
42 |
try { |
|
43 |
inputAvailable.acquire(); |
|
44 |
File input = this.input.get(); |
|
45 |
if (input != null) { |
|
46 |
process(input); |
|
47 |
} |
|
48 |
} catch (InterruptedException e) { |
|
49 |
continue; |
|
50 |
} |
|
51 |
} |
|
52 |
} |
|
53 |
|
|
54 |
private void process(File input) { |
|
55 |
byte array[]; |
|
56 |
try { |
|
57 |
array = FileWorker.loadByteArray(input); |
|
58 |
} catch (IOException e) { |
|
59 |
ui.loadingInputFileError(); |
|
60 |
return; |
|
61 |
} |
|
24 | 62 |
|
63 |
String json; |
|
25 | 64 |
try { |
26 |
String json = convert(FileWorker.loadByteArray(input)); |
|
27 |
|
|
28 |
String title = "Uložení JSON"; |
|
29 |
if (FileWorker.saveJson(output, json)) { |
|
30 |
System.out.println("The JSON file was saved correctly."); |
|
31 |
Report.info(title, null, "Uložení JSON souboru proběhlo v pořádku."); |
|
32 |
} else { |
|
33 |
System.out.println("There was an error saving the JSON file."); |
|
34 |
Report.error(title, null, "Při ukládání JSON souboru nastala chyba."); |
|
35 |
} |
|
65 |
json = convert(array); |
|
36 | 66 |
} catch (IOException e) { |
37 |
System.out.println("There was an error loading the file."); |
|
38 |
e.printStackTrace(); |
|
39 |
Report.error("Načítání souboru", null, "Při načítání souboru došlo k chybě."); |
|
67 |
json = null; |
|
40 | 68 |
} |
41 |
|
|
42 |
main.convertIsComplete(); |
|
69 |
ui.completed(json); |
|
43 | 70 |
} |
44 | 71 |
|
45 |
private String convert(byte[] buffer) { |
|
72 |
private String convert(byte[] buffer) throws IOException {
|
|
46 | 73 |
String json = ""; |
47 |
try { |
|
48 |
System.out.println("Deserialization begins."); |
|
49 |
jdeserialize deserializer = new jdeserialize(buffer); |
|
50 |
|
|
51 |
// gets the "contents" into an array - returnes the deserialization of all |
|
52 |
// 'writes' into serialized object via objectOutputStream |
|
53 |
List<content> cntnts = deserializer.getContent(); |
|
74 |
|
|
75 |
jdeserialize deserializer = new jdeserialize(buffer); |
|
76 |
|
|
77 |
// gets the "contents" into an array - returnes the deserialization of all |
|
78 |
// 'writes' into serialized object via objectOutputStream |
|
79 |
List<content> cntnts = deserializer.getContent(); |
|
54 | 80 |
|
55 |
// testing with only first of the contents |
|
56 |
//ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); |
|
57 |
|
|
58 |
for(content cnt : cntnts) |
|
81 |
// testing with only first of the contents |
|
82 |
//ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); |
|
83 |
|
|
84 |
for(content cnt : cntnts) |
|
85 |
{ |
|
86 |
if(cnt != null) |
|
59 | 87 |
{ |
60 |
if(cnt != null) |
|
61 |
{ |
|
62 |
json += cnt.toString(); |
|
63 |
} |
|
88 |
json += cnt.toString(); |
|
64 | 89 |
} |
65 |
|
|
66 |
System.out.println("Task completed."); |
|
67 |
|
|
68 |
//just for tests |
|
69 |
//System.out.println(json); |
|
70 |
|
|
71 |
return json; |
|
72 |
} catch (IOException e) { |
|
73 |
System.out.println("An error occurred while processing!"); |
|
74 |
e.printStackTrace(); |
|
75 |
return null; |
|
76 | 90 |
} |
91 |
|
|
92 |
return json; |
|
77 | 93 |
} |
78 | 94 |
|
79 | 95 |
} |
project/Deserializer/src/Deserializer.java | ||
---|---|---|
1 | 1 |
import java.io.File; |
2 |
import java.io.IOException; |
|
2 | 3 |
|
4 |
import io.FileWorker; |
|
3 | 5 |
import javafx.application.Application; |
6 |
import javafx.application.Platform; |
|
7 |
import javafx.beans.value.ChangeListener; |
|
8 |
import javafx.beans.value.ObservableValue; |
|
4 | 9 |
import javafx.geometry.Insets; |
10 |
import javafx.geometry.Orientation; |
|
5 | 11 |
import javafx.geometry.Pos; |
12 |
import javafx.scene.Node; |
|
6 | 13 |
import javafx.scene.Parent; |
7 | 14 |
import javafx.scene.Scene; |
8 | 15 |
import javafx.scene.control.Button; |
9 | 16 |
import javafx.scene.control.Label; |
17 |
import javafx.scene.control.Menu; |
|
18 |
import javafx.scene.control.MenuBar; |
|
19 |
import javafx.scene.control.MenuItem; |
|
20 |
import javafx.scene.control.ProgressIndicator; |
|
21 |
import javafx.scene.control.Separator; |
|
22 |
import javafx.scene.control.SeparatorMenuItem; |
|
23 |
import javafx.scene.control.Alert.AlertType; |
|
24 |
import javafx.scene.image.Image; |
|
25 |
import javafx.scene.image.ImageView; |
|
26 |
import javafx.scene.input.KeyCode; |
|
27 |
import javafx.scene.input.KeyCodeCombination; |
|
28 |
import javafx.scene.input.KeyCombination; |
|
29 |
import javafx.scene.layout.BorderPane; |
|
10 | 30 |
import javafx.scene.layout.HBox; |
31 |
import javafx.scene.layout.Priority; |
|
32 |
import javafx.scene.layout.Region; |
|
11 | 33 |
import javafx.scene.layout.VBox; |
34 |
import javafx.scene.web.WebEngine; |
|
35 |
import javafx.scene.web.WebView; |
|
12 | 36 |
import javafx.stage.FileChooser; |
13 | 37 |
import javafx.stage.Stage; |
14 | 38 |
|
15 |
public class Deserializer extends Application { |
|
39 |
/** |
|
40 |
* VM arguments for Java 11: --module-path libs\javafx-sdk-11.0.2\lib --add-modules=javafx.controls |
|
41 |
*/ |
|
42 |
public class Deserializer extends Application implements IConversionResults { |
|
16 | 43 |
|
17 | 44 |
private Stage stage; |
18 |
private File inputFile; |
|
19 |
private File outputFile; |
|
45 |
private Label fullScreen; |
|
46 |
private WebEngine webEngine; |
|
47 |
private VBox outputLayout; |
|
20 | 48 |
|
21 |
Button convert;
|
|
49 |
private Converter converter;
|
|
22 | 50 |
|
23 | 51 |
// TODO delete after deserializer debugging is complete... |
52 |
private File defaultInput = new File("a2"); |
|
53 |
private File defaultOutput = new File("results.json"); |
|
24 | 54 |
private boolean testing; |
25 | 55 |
|
26 | 56 |
public static void main(String[] args) { |
... | ... | |
30 | 60 |
@Override |
31 | 61 |
public void init() throws Exception { |
32 | 62 |
super.init(); |
33 |
testing = false; |
|
63 |
testing = true; |
|
64 |
converter = new Converter(this); |
|
65 |
converter.start(); |
|
66 |
} |
|
67 |
|
|
68 |
@Override |
|
69 |
public void stop() throws Exception { |
|
70 |
super.stop(); |
|
71 |
converter.end(); |
|
72 |
converter.join(); |
|
34 | 73 |
} |
35 | 74 |
|
36 | 75 |
@Override |
37 | 76 |
public void start(Stage stage) throws Exception { |
38 | 77 |
this.stage = stage; |
39 |
stage.setTitle("Java object universal deserializer"); |
|
78 |
stage.getIcons().add(new Image("img/logo.png")); |
|
79 |
stage.setTitle("Java Object Universal Deserializer"); |
|
40 | 80 |
stage.setScene(createScene()); |
41 | 81 |
stage.setMinWidth(400); |
42 | 82 |
stage.show(); |
43 | 83 |
|
84 |
stage.fullScreenProperty().addListener((obs, oldState, newState) -> { |
|
85 |
changeFullScreenMenuItem(); |
|
86 |
}); |
|
87 |
|
|
88 |
stage.focusedProperty().addListener(new ChangeListener<Boolean>() |
|
89 |
{ |
|
90 |
@Override |
|
91 |
public void changed(ObservableValue<? extends Boolean> obs, Boolean oldValue, Boolean newValue) |
|
92 |
{ |
|
93 |
System.out.println(newValue); |
|
94 |
|
|
95 |
/* |
|
96 |
byte arr[] = FileWorker.loadByteArray(new File("b2")); |
|
97 |
byte arr2[] = FileWorker.loadByteArray(new File("b")); |
|
98 |
|
|
99 |
byte con[] = FileWorker.load(new File("b")); |
|
100 |
|
|
101 |
if (arr.length == arr2.length && arr2.length == con.length) { |
|
102 |
System.out.println("TRRUE"); |
|
103 |
for (int i = 0; i < arr.length; i++) { |
|
104 |
if (arr[i] != arr2[i] || arr2[i] != con[i]) { |
|
105 |
System.out.println("CHCHYBA!!!"); |
|
106 |
break; |
|
107 |
} |
|
108 |
} |
|
109 |
} |
|
110 |
System.out.println("COMPLETE"); |
|
111 |
*/ |
|
112 |
/*Set<DataFormat> types = Clipboard.getSystemClipboard().getContentTypes(); |
|
113 |
System.out.println("Detected types: " + types.size()); |
|
114 |
for (DataFormat s : types) { |
|
115 |
System.out.println(s); |
|
116 |
} |
|
117 |
|
|
118 |
try { |
|
119 |
byte[] orig = FileWorker.loadByteArray(new File("b")); |
|
120 |
//byte[] orig = FileWorker.getBBB(); |
|
121 |
|
|
122 |
DataFormat df = DataFormat.lookupMimeType("text/plain"); |
|
123 |
if (df != null) { |
|
124 |
// System.out.println(Clipboard.getSystemClipboard().getContent(df).getClass()); |
|
125 |
String data = (String) Clipboard.getSystemClipboard().getContent(df); |
|
126 |
byte[] buffer = data.getBytes(); |
|
127 |
|
|
128 |
boolean changeBites = false; |
|
129 |
|
|
130 |
List<Byte> bl = new ArrayList<Byte>(); |
|
131 |
for (int i = 0; i < buffer.length; i++) { |
|
132 |
if (changeBites) { |
|
133 |
buffer[i] = (byte) ((buffer[i] & 0x0F) | 0xE0); |
|
134 |
changeBites = false; |
|
135 |
} else if (buffer[i] == (byte) 0xC2 || buffer[i] == (byte) 0x0D) { |
|
136 |
continue; |
|
137 |
} else if (buffer[i] == (byte) 0xC3) { |
|
138 |
changeBites = true; |
|
139 |
continue; |
|
140 |
} |
|
141 |
bl.add(buffer[i]); |
|
142 |
} |
|
143 |
|
|
144 |
System.out.println(bl.size() + " " + orig.length); |
|
145 |
for (int i = 0; i < orig.length; i++) { |
|
146 |
if (bl.get(i) != orig[i]) { |
|
147 |
System.out.println(i); |
|
148 |
for (int j = i - 1; j < i + 50; j++) { |
|
149 |
System.out.println(String.format("%02X", bl.get(j)) + " " + String.format("%02X", orig[j])); |
|
150 |
} |
|
151 |
break; |
|
152 |
} |
|
153 |
} |
|
154 |
} |
|
155 |
} catch (Exception e) { |
|
156 |
e.printStackTrace(); |
|
157 |
}*/ |
|
158 |
} |
|
159 |
}); |
|
160 |
|
|
44 | 161 |
//tests |
45 | 162 |
//Converter thread = new Converter(this, new File("simple_arrays.out"), new File("bcha.json")); |
46 | 163 |
//thread.start(); |
47 | 164 |
} |
48 | 165 |
|
49 | 166 |
private Scene createScene() { |
50 |
return new Scene(createLayout()); |
|
167 |
createMenu(); |
|
168 |
Scene scene = new Scene(createLayout()); |
|
169 |
scene.getStylesheets().add("css/main.css"); |
|
170 |
return scene; |
|
51 | 171 |
} |
52 | 172 |
|
53 | 173 |
private Parent createLayout() { |
54 |
VBox layout = new VBox(); |
|
55 |
layout.setPadding(new Insets(10.0)); |
|
56 |
layout.setSpacing(10.0); |
|
174 |
BorderPane layout = new BorderPane(); |
|
175 |
|
|
176 |
layout.setTop(createMenu()); |
|
177 |
layout.setCenter(createBodyLayout()); |
|
57 | 178 |
|
179 |
return layout; |
|
180 |
} |
|
181 |
|
|
182 |
private Parent createBodyLayout() { |
|
58 | 183 |
Label forInputFile = new Label("Soubor k deserializaci:"); |
59 |
Label inputFile = new Label(" Ještě nebyl vybrán žádný soubor..."); |
|
60 |
inputFile.setStyle("-fx-font-style: italic;"); |
|
61 |
VBox forInput = new VBox(); |
|
62 |
forInput.getChildren().add(forInputFile); |
|
63 |
forInput.getChildren().add(inputFile); |
|
184 |
Label inputFile; |
|
185 |
if (!testing) { |
|
186 |
inputFile = new Label(" Ještě nebyl vybrán žádný soubor..."); |
|
187 |
inputFile.setStyle("-fx-font-style: italic;"); |
|
188 |
} else { |
|
189 |
inputFile = new Label(defaultOutput.getAbsolutePath()); |
|
190 |
inputFile.setStyle("-fx-font-weight: bold;"); |
|
191 |
} |
|
192 |
// TODO inputFile.setPrefWidth(400); |
|
193 |
inputFile.setWrapText(true); |
|
64 | 194 |
|
65 |
Label forOutputFile = new Label("Výstupní soubor:"); |
|
66 |
Label outputFile = new Label(" Ještě nebyl vybrán žádný soubor..."); |
|
67 |
outputFile.setStyle("-fx-font-style: italic;"); |
|
68 |
VBox forOutput = new VBox(); |
|
69 |
forOutput.getChildren().add(forOutputFile); |
|
70 |
forOutput.getChildren().add(outputFile); |
|
71 |
|
|
72 |
convert = new Button("Převést"); |
|
73 |
convert.setOnAction(event -> { |
|
74 |
// a.data a b.data are customers test files (binary files). |
|
75 |
convert.setDisable(true); |
|
76 |
Converter thread = new Converter(this, testing ? new File("a.data") : this.inputFile, testing ? new File("results.json") : this.outputFile); |
|
77 |
thread.start(); |
|
78 |
}); |
|
79 |
convert.setDisable(!testing); |
|
80 | 195 |
Button setInputFile = new Button("Vstupní soubor"); |
81 | 196 |
setInputFile.setOnAction(event -> { |
82 |
FileChooser fCh = new FileChooser(); |
|
83 |
fCh.setInitialDirectory(new File(".")); |
|
84 |
// The input can be a zip file or file without a specific extension -> file chooser without extension filters... |
|
85 |
// fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("Binary Files", "*.out")); |
|
86 |
File file = fCh.showOpenDialog(stage); |
|
87 |
if (file != null) { |
|
88 |
this.inputFile = file; |
|
89 |
inputFile.setText(this.inputFile == null ? "" : this.inputFile.getAbsolutePath()); |
|
90 |
inputFile.setStyle("-fx-font-weight: bold;"); |
|
91 |
convert.setDisable(this.inputFile == null || this.outputFile == null); |
|
197 |
if (testing) { |
|
198 |
converter.setInput(defaultInput); |
|
199 |
} else { |
|
200 |
FileChooser fCh = new FileChooser(); |
|
201 |
fCh.setInitialDirectory(new File(".")); |
|
202 |
// The input can be a zip file or file without a specific extension -> file chooser without extension filters... |
|
203 |
// fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("Binary Files", "*.out")); |
|
204 |
File file = fCh.showOpenDialog(stage); |
|
205 |
if (file != null) { |
|
206 |
setOutputLayoutDisabled(true); |
|
207 |
|
|
208 |
inputFile.setText(file.getAbsolutePath()); |
|
209 |
inputFile.setStyle("-fx-font-weight: bold;"); |
|
210 |
|
|
211 |
converter.setInput(file); |
|
212 |
} |
|
92 | 213 |
} |
93 | 214 |
}); |
94 |
Button setOutputFile = new Button("Výstupní soubor"); |
|
215 |
|
|
216 |
HBox bIF = new HBox(); |
|
217 |
bIF.setAlignment(Pos.CENTER_RIGHT); |
|
218 |
bIF.getChildren().add(setInputFile); |
|
219 |
|
|
220 |
VBox forInput = new VBox(); |
|
221 |
forInput.setSpacing(5.0); |
|
222 |
forInput.getChildren().addAll(forInputFile, inputFile, bIF); |
|
223 |
|
|
224 |
|
|
225 |
Label forClipBoardInput = new Label("Vstup ze schránky"); |
|
226 |
ProgressIndicator indicator = new ProgressIndicator(); |
|
227 |
indicator.setMaxHeight(38); // TODO |
|
228 |
Button setInputAccToCB = new Button("Použít"); |
|
229 |
setInputAccToCB.setOnAction(event -> { |
|
230 |
// TODO |
|
231 |
}); |
|
232 |
|
|
233 |
HBox indicatorLayout = new HBox(); |
|
234 |
Region region2 = new Region(); |
|
235 |
HBox.setHgrow(region2, Priority.ALWAYS); |
|
236 |
indicatorLayout.getChildren().addAll(forClipBoardInput, region2, indicator); |
|
237 |
|
|
238 |
HBox bInputCB = new HBox(); |
|
239 |
bInputCB.setAlignment(Pos.CENTER_RIGHT); |
|
240 |
bInputCB.getChildren().add(setInputAccToCB); |
|
241 |
|
|
242 |
VBox forInput2 = new VBox(); |
|
243 |
forInput2.setSpacing(5.0); |
|
244 |
forInput2.setMinWidth(175); |
|
245 |
forInput2.getChildren().addAll(indicatorLayout, bInputCB); |
|
246 |
|
|
247 |
|
|
248 |
|
|
249 |
Label forOutputFile = new Label("Výstupní soubor:"); |
|
250 |
Label outputFile; |
|
251 |
if (!testing) { |
|
252 |
outputFile = new Label(" Ještě nebyl vybrán žádný soubor..."); |
|
253 |
outputFile.setStyle("-fx-font-style: italic;"); |
|
254 |
} else { |
|
255 |
outputFile = new Label(defaultOutput.getAbsolutePath()); |
|
256 |
outputFile.setStyle("-fx-font-weight: bold;"); |
|
257 |
} |
|
258 |
|
|
259 |
Button setOutputFile = new Button("Uložit"); |
|
95 | 260 |
setOutputFile.setOnAction(event -> { |
96 |
FileChooser fCh = new FileChooser(); |
|
97 |
fCh.setInitialDirectory(new File(".")); |
|
98 |
fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("JSON Files", "*.json")); |
|
99 |
File file = fCh.showSaveDialog(stage); |
|
100 |
if (file != null) { |
|
101 |
this.outputFile = file; |
|
102 |
outputFile.setText(this.outputFile == null ? "" : this.outputFile.getAbsolutePath()); |
|
103 |
outputFile.setStyle("-fx-font-weight: bold;"); |
|
104 |
convert.setDisable(this.inputFile == null || this.outputFile == null); |
|
261 |
File jsonFile = null; |
|
262 |
|
|
263 |
if (testing) { |
|
264 |
jsonFile = defaultOutput; |
|
265 |
} else { |
|
266 |
FileChooser fCh = new FileChooser(); |
|
267 |
fCh.setInitialDirectory(new File(".")); |
|
268 |
fCh.getExtensionFilters().add(new FileChooser.ExtensionFilter("JSON Files", "*.json")); |
|
269 |
File file = fCh.showSaveDialog(stage); |
|
270 |
if (file != null) { |
|
271 |
jsonFile = file; |
|
272 |
outputFile.setText(file.getAbsolutePath()); |
|
273 |
outputFile.setStyle("-fx-font-weight: bold;"); |
|
274 |
} |
|
275 |
} |
|
276 |
|
|
277 |
if (jsonFile != null && webEngine.getDocument() != null) { |
|
278 |
String title = "Uložení JSON"; |
|
279 |
try { |
|
280 |
FileWorker.saveJson(jsonFile, "Ahoj"/* TODO webEngine.getDocument().getTextContent()*/); |
|
281 |
Report.info(title, null, "Uložení JSON souboru proběhlo v pořádku."); |
|
282 |
} catch (Exception e) { |
|
283 |
Report.error(title, null, "Při ukládání JSON souboru nastala chyba."); |
|
284 |
} |
|
105 | 285 |
} |
106 | 286 |
}); |
107 |
HBox footer = new HBox(); |
|
108 |
footer.setSpacing(10.0); |
|
109 |
footer.setAlignment(Pos.CENTER_RIGHT); |
|
110 |
footer.getChildren().add(setInputFile); |
|
111 |
footer.getChildren().add(setOutputFile); |
|
112 |
footer.getChildren().add(convert); |
|
113 | 287 |
|
114 |
layout.getChildren().add(forInput); |
|
115 |
layout.getChildren().add(forOutput); |
|
116 |
layout.getChildren().add(footer); |
|
288 |
HBox bOF = new HBox(); |
|
289 |
bOF.setAlignment(Pos.CENTER_RIGHT); |
|
290 |
bOF.getChildren().add(setOutputFile); |
|
291 |
|
|
292 |
outputLayout = new VBox(); |
|
293 |
outputLayout.setSpacing(5.0); |
|
294 |
outputLayout.getChildren().add(forOutputFile); |
|
295 |
outputLayout.getChildren().add(outputFile); |
|
296 |
outputLayout.getChildren().add(bOF); |
|
297 |
setOutputLayoutDisabled(true); |
|
298 |
|
|
299 |
|
|
300 |
WebView resultantJson = new WebView(); |
|
301 |
resultantJson.setPrefHeight(400); |
|
302 |
webEngine = resultantJson.getEngine(); |
|
303 |
|
|
304 |
|
|
305 |
|
|
306 |
HBox header = new HBox(); |
|
307 |
header.setSpacing(10.0); |
|
308 |
HBox.setHgrow(forInput, Priority.ALWAYS); |
|
309 |
header.getChildren().addAll(forInput, new Separator(Orientation.VERTICAL), forInput2); |
|
310 |
|
|
311 |
|
|
312 |
|
|
313 |
VBox layout = new VBox(); |
|
314 |
layout.setPadding(new Insets(10.0)); |
|
315 |
layout.setSpacing(10.0); |
|
316 |
layout.getChildren().addAll(header, new Separator(), resultantJson, new Separator(), outputLayout); |
|
117 | 317 |
|
118 | 318 |
return layout; |
119 | 319 |
} |
120 | 320 |
|
121 |
public void convertIsComplete() { |
|
122 |
convert.setDisable(false); |
|
321 |
private Node createMenu() { |
|
322 |
Menu menu = new Menu("Aplikace"); |
|
323 |
|
|
324 |
MenuItem fullScreen = createMenuItem(null, null, new KeyCodeCombination(KeyCode.F, KeyCombination.CONTROL_DOWN)); |
|
325 |
fullScreen.setOnAction(event -> { |
|
326 |
stage.setFullScreen(!stage.isFullScreen()); |
|
327 |
}); |
|
328 |
this.fullScreen = (Label) fullScreen.getGraphic(); |
|
329 |
changeFullScreenMenuItem(); |
|
330 |
|
|
331 |
MenuItem close = createMenuItem("Ukončit", "img/close.png", new KeyCodeCombination(KeyCode.E, KeyCombination.CONTROL_DOWN)); |
|
332 |
close.setOnAction(event -> { |
|
333 |
Platform.exit(); |
|
334 |
}); |
|
335 |
|
|
336 |
menu.getItems().addAll(fullScreen, new SeparatorMenuItem(), close); |
|
337 |
|
|
338 |
MenuBar bar = new MenuBar(); |
|
339 |
bar.getMenus().add(menu); |
|
340 |
return bar; |
|
341 |
} |
|
342 |
|
|
343 |
private void changeFullScreenMenuItem() { |
|
344 |
fullScreen.setText(stage.isFullScreen() ? "Normální zobrazení" : "Plné zobrazení"); |
|
345 |
fullScreen.setGraphic(new ImageView(stage.isFullScreen() ? "img/normal.png" : "img/full.png")); |
|
346 |
} |
|
347 |
|
|
348 |
private MenuItem createMenuItem(String name, String icon, KeyCodeCombination keyCodeComb) { |
|
349 |
Label label = new Label(name); |
|
350 |
label.setGraphicTextGap(10); |
|
351 |
label.getStyleClass().add("menu-graphics"); |
|
352 |
label.setMinWidth(175); |
|
353 |
label.setMaxWidth(175); |
|
354 |
if (icon != null) |
|
355 |
label.setGraphic(new ImageView(new Image(icon))); |
|
356 |
|
|
357 |
MenuItem menuItem = new MenuItem(); |
|
358 |
menuItem.setGraphic(label); |
|
359 |
menuItem.setAccelerator(keyCodeComb); |
|
360 |
return menuItem; |
|
361 |
} |
|
362 |
|
|
363 |
private void setOutputLayoutDisabled(boolean disable) { |
|
364 |
outputLayout.setDisable(!testing && disable); |
|
365 |
} |
|
366 |
|
|
367 |
@Override |
|
368 |
public void loadingInputFileError() { |
|
369 |
Platform.runLater(()->{ |
|
370 |
Report.error("Načítání souboru", null, "Při načítání souboru došlo k chybě."); |
|
371 |
}); |
|
372 |
} |
|
373 |
|
|
374 |
@Override |
|
375 |
public void completed(final String json) { |
|
376 |
Platform.runLater(()->{ |
|
377 |
String _json = json; |
|
378 |
setOutputLayoutDisabled(_json != null); |
|
379 |
if (_json == null) { |
|
380 |
_json = "--- Chyba při deserializaci! ---"; |
|
381 |
} |
|
382 |
webEngine.loadContent(_json, "text/plain"); |
|
383 |
}); |
|
123 | 384 |
} |
124 | 385 |
|
125 | 386 |
} |
project/Deserializer/src/FileWorker.java | ||
---|---|---|
1 |
import java.io.BufferedInputStream; |
|
2 |
import java.io.BufferedWriter; |
|
3 |
import java.io.File; |
|
4 |
import java.io.FileInputStream; |
|
5 |
import java.io.FileWriter; |
|
6 |
import java.io.IOException; |
|
7 |
import java.util.ArrayList; |
|
8 |
import java.util.List; |
|
9 |
import java.util.NoSuchElementException; |
|
10 |
import java.util.zip.ZipException; |
|
11 |
import java.util.zip.ZipInputStream; |
|
12 |
|
|
13 |
public class FileWorker { |
|
14 |
|
|
15 |
public static byte[] loadByteArray(File file) throws IOException { |
|
16 |
byte buffer[]; |
|
17 |
|
|
18 |
/* |
|
19 |
* Note. |
|
20 |
* For ZIP - the first four bytes should be one of the following combinations: |
|
21 |
* 50 4B 03 04 |
|
22 |
* 50 4B 05 06 (empty archive) |
|
23 |
* 50 4B 07 08 (spanned archive) |
|
24 |
* Source: https://en.wikipedia.org/wiki/List_of_file_signatures |
|
25 |
*/ |
|
26 |
try { |
|
27 |
|
|
28 |
ZipInputStream zipIS = new ZipInputStream(new FileInputStream(file)); |
|
29 |
|
|
30 |
if (zipIS.getNextEntry() == null) { |
|
31 |
zipIS.close(); |
|
32 |
throw new NoSuchElementException(); |
|
33 |
} |
|
34 |
|
|
35 |
System.out.println("The file is a zip archive..."); |
|
36 |
List<Byte> _buffer = new ArrayList<Byte>(); |
|
37 |
while (true) { |
|
38 |
int value = zipIS.read(); |
|
39 |
if (value < 0) { |
|
40 |
break; |
|
41 |
} else { |
|
42 |
_buffer.add((byte) value); |
|
43 |
} |
|
44 |
} |
|
45 |
|
|
46 |
buffer = new byte[_buffer.size()]; |
|
47 |
for (int i = 0; i < buffer.length; i++) { |
|
48 |
buffer[i] = _buffer.get(i); |
|
49 |
} |
|
50 |
|
|
51 |
zipIS.close(); |
|
52 |
|
|
53 |
} catch (NoSuchElementException | ZipException e) { |
|
54 |
|
|
55 |
System.out.println("The file is not a zip archive..."); |
|
56 |
buffer = new byte[(int) file.length()]; |
|
57 |
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); |
|
58 |
bis.read(buffer); |
|
59 |
bis.close(); |
|
60 |
|
|
61 |
} |
|
62 |
|
|
63 |
System.out.println("Bytes from the file were loaded correctly."); |
|
64 |
return buffer; |
|
65 |
} |
|
66 |
|
|
67 |
public static boolean saveJson(File file, String json) { |
|
68 |
boolean retValue = false; |
|
69 |
try { |
|
70 |
FileWriter fw = new FileWriter(file); |
|
71 |
BufferedWriter bw = new BufferedWriter(fw); |
|
72 |
bw.write(json); |
|
73 |
bw.flush(); |
|
74 |
bw.close(); |
|
75 |
retValue = true; |
|
76 |
} catch (IOException e) { |
|
77 |
e.printStackTrace(); |
|
78 |
} |
|
79 |
return retValue; |
|
80 |
} |
|
81 |
|
|
82 |
} |
project/Deserializer/src/IConversionResults.java | ||
---|---|---|
1 |
|
|
2 |
public interface IConversionResults { |
|
3 |
|
|
4 |
public void loadingInputFileError(); |
|
5 |
public void completed(String json); |
|
6 |
|
|
7 |
} |
project/Deserializer/src/Report.java | ||
---|---|---|
1 |
import javafx.application.Platform; |
|
1 |
import com.sun.javafx.stage.StageHelper; |
|
2 |
|
|
2 | 3 |
import javafx.geometry.Rectangle2D; |
3 | 4 |
import javafx.scene.control.Alert; |
4 | 5 |
import javafx.scene.control.Alert.AlertType; |
... | ... | |
11 | 12 |
private static final int WIDTH = 500; |
12 | 13 |
|
13 | 14 |
private static void setPosition(Alert alert) { |
15 |
// For Java 8. |
|
16 |
if (StageHelper.getStages() != null && StageHelper.getStages().size() != 0) { |
|
17 |
if (Screen.getPrimary() != null && Screen.getPrimary().getVisualBounds() != null) { |
|
18 |
Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds(); |
|
19 |
alert.setX((primScreenBounds.getWidth() - WIDTH - 20) / 2); |
|
20 |
alert.setY((primScreenBounds.getHeight() - HEIGHT - 20) / 3); |
|
21 |
} |
|
22 |
alert.initOwner(StageHelper.getStages().get(StageHelper.getStages().size() - 1)); |
|
23 |
} |
|
24 |
/* For Java 11. |
|
14 | 25 |
if (Window.getWindows() != null && Window.getWindows().size() != 0) { |
15 | 26 |
if (Screen.getPrimary() != null && Screen.getPrimary().getVisualBounds() != null) { |
16 | 27 |
Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds(); |
... | ... | |
18 | 29 |
alert.setY((primScreenBounds.getHeight() - HEIGHT - 20) / 3); |
19 | 30 |
} |
20 | 31 |
alert.initOwner(Window.getWindows().get(Window.getWindows().size() - 1)); |
21 |
} |
|
32 |
}*/
|
|
22 | 33 |
} |
23 | 34 |
|
24 | 35 |
private static Alert createAlert(AlertType type, String title, String header, String content) { |
... | ... | |
33 | 44 |
} |
34 | 45 |
|
35 | 46 |
public static void error(String title, String header, String content) { |
36 |
Platform.runLater(()->{ |
|
37 |
createAlert(AlertType.WARNING, title, header, content).showAndWait(); |
|
38 |
}); |
|
47 |
createAlert(AlertType.WARNING, title, header, content).showAndWait(); |
|
39 | 48 |
} |
40 | 49 |
|
41 | 50 |
public static void info(String title, String header, String content) { |
42 |
Platform.runLater(()->{ |
|
43 |
createAlert(AlertType.INFORMATION, title, header, content).showAndWait(); |
|
44 |
}); |
|
51 |
createAlert(AlertType.INFORMATION, title, header, content).showAndWait(); |
|
45 | 52 |
} |
46 | 53 |
|
47 | 54 |
public static boolean confirm(String title, String header, String content) { |
project/Deserializer/src/io/FileWorker.java | ||
---|---|---|
1 |
package io; |
|
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.util.ArrayList; |
|
11 |
import java.util.List; |
|
12 |
import java.util.NoSuchElementException; |
|
13 |
|
|
14 |
public class FileWorker { |
|
15 |
|
|
16 |
public static byte[] loadByteArray(File file) throws IOException { |
|
17 |
byte fileContent[] = loadFileContent(file); |
|
18 |
byte buffer[]; |
|
19 |
|
|
20 |
IZipReader input; |
|
21 |
try { |
|
22 |
input = new ZipInStream(new ByteArrayInputStream(fileContent)); |
|
23 |
} catch (NoSuchElementException e) { |
|
24 |
System.out.println("Note: the file may be a special type of ZIP archive."); |
|
25 |
input = new InflaterInStream(new FileInputStream(file)); |
|
26 |
} |
|
27 |
|
|
28 |
try { |
|
29 |
List<Byte> _buffer = new ArrayList<Byte>(); |
|
30 |
while (true) { |
|
31 |
int value = input.read(); |
|
32 |
if (value < 0) { |
|
33 |
break; |
|
34 |
} else { |
|
35 |
_buffer.add((byte) value); |
|
36 |
} |
|
37 |
} |
|
38 |
|
|
39 |
buffer = new byte[_buffer.size()]; |
|
40 |
for (int i = 0; i < buffer.length; i++) { |
|
41 |
buffer[i] = _buffer.get(i); |
|
42 |
} |
|
43 |
|
|
44 |
System.out.println(String.format("The file is a%s ZIP archive...", input.getClass() == InflaterInStream.class ? " special" : "")); |
|
45 |
} catch (Exception e) { |
|
46 |
e.printStackTrace(); |
|
47 |
System.out.println("The file is not a ZIP archive..."); |
|
48 |
buffer = fileContent; |
|
49 |
} finally { |
|
50 |
input.close(); |
|
51 |
} |
|
52 |
|
|
53 |
System.out.println("Bytes from the file were loaded correctly."); |
|
54 |
return buffer; |
|
55 |
} |
|
56 |
|
|
57 |
private static byte[] loadFileContent(File file) throws IOException { |
|
58 |
byte buffer[] = new byte[(int) file.length()]; |
|
59 |
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); |
|
60 |
bis.read(buffer); |
|
61 |
bis.close(); |
|
62 |
return buffer; |
|
63 |
} |
|
64 |
|
|
65 |
public static void saveJson(File file, String json) throws IOException { |
|
66 |
FileWriter fw = new FileWriter(file); |
|
67 |
BufferedWriter bw = new BufferedWriter(fw); |
|
68 |
bw.write(json); |
|
69 |
bw.flush(); |
|
70 |
bw.close(); |
|
71 |
} |
|
72 |
|
|
73 |
} |
project/Deserializer/src/io/IZipReader.java | ||
---|---|---|
1 |
package io; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
|
|
5 |
public interface IZipReader { |
|
6 |
|
|
7 |
public int read() throws IOException; |
|
8 |
public void close() throws IOException; |
|
9 |
|
|
10 |
} |
project/Deserializer/src/io/InflaterInStream.java | ||
---|---|---|
1 |
package io; |
|
2 |
|
|
3 |
import java.io.FilterInputStream; |
|
4 |
import java.io.IOException; |
|
5 |
import java.io.InputStream; |
|
6 |
import java.util.zip.Inflater; |
|
7 |
import java.util.zip.InflaterInputStream; |
|
8 |
|
|
9 |
public class InflaterInStream extends FilterInputStream implements IZipReader { |
|
10 |
|
|
11 |
private Inflater inflater; |
|
12 |
|
|
13 |
public InflaterInStream(InputStream in) { |
|
14 |
super(null); |
|
15 |
inflater = new Inflater(); |
|
16 |
this.in = new InflaterInputStream(in, inflater); |
|
17 |
} |
|
18 |
|
|
19 |
public int read() throws IOException { |
|
20 |
return in.read(); |
|
21 |
} |
|
22 |
|
|
23 |
public void close() throws IOException { |
|
24 |
try { |
|
25 |
super.close(); |
|
26 |
} |
|
27 |
finally { |
|
28 |
if (inflater != null) { |
|
29 |
inflater.end(); |
|
30 |
} |
|
31 |
} |
|
32 |
} |
|
33 |
|
|
34 |
} |
project/Deserializer/src/io/ZipInStream.java | ||
---|---|---|
1 |
package io; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import java.io.InputStream; |
|
5 |
import java.util.NoSuchElementException; |
|
6 |
import java.util.zip.ZipInputStream; |
|
7 |
|
|
8 |
/** |
|
9 |
* For ZIP - the first four bytes should be one of the following |
|
10 |
* combinations: 50 4B 03 04 |
|
11 |
* 50 4B 05 06 (empty archive) |
|
12 |
* 50 4B 07 08 (spanned archive) |
|
13 |
* Source: https://en.wikipedia.org/wiki/List_of_file_signatures |
|
14 |
*/ |
|
15 |
public class ZipInStream implements IZipReader { |
|
16 |
|
|
17 |
private ZipInputStream zipIS; |
|
18 |
|
|
19 |
public ZipInStream(InputStream in) throws IOException { |
|
20 |
zipIS = new ZipInputStream(in); |
|
21 |
|
|
22 |
if (zipIS.getNextEntry() == null) { |
|
23 |
zipIS.close(); |
|
24 |
throw new NoSuchElementException(); |
|
25 |
} |
|
26 |
} |
|
27 |
|
|
28 |
@Override |
|
29 |
public int read() throws IOException { |
|
30 |
return zipIS.read(); |
|
31 |
} |
|
32 |
|
|
33 |
@Override |
|
34 |
public void close() throws IOException { |
|
35 |
zipIS.close(); |
|
36 |
} |
|
37 |
|
|
38 |
} |
project/Deserializer/src/jdeserialize/instance.java | ||
---|---|---|
48 | 48 |
// v this.fielddata najit element, jehoz key == classdesc |
49 | 49 |
// v tomto prvku fielddata najdu value (element) podle key == f |
50 | 50 |
// jeho value je chtena hodnota |
51 |
var val = this.fielddata.get(classdesc).get(f).toString();
|
|
51 |
String val = this.fielddata.get(classdesc).get(f).toString();
|
|
52 | 52 |
sb.append("\n\t\t"); |
53 | 53 |
// Datový typ není potřeba - json jej nepoužívá! |
54 | 54 |
sb.append("\"")./* append(f.type.getJavaType()).append(" "). */append(f.name).append("\""); |
Také k dispozici: Unified diff
#re #7879
Ještě není dokončeno - commit kvůli merge.