Revize a2822466
Přidáno uživatelem Jan Havlíček před asi 5 roky(ů)
demo_jh/Deserializer_tests/src/app/App.java | ||
---|---|---|
6 | 6 |
import java.io.IOException; |
7 | 7 |
import java.io.ObjectInputStream; |
8 | 8 |
import java.io.ObjectOutputStream; |
9 |
import java.util.ArrayList; |
|
10 |
import jdeserialize.Getopt; |
|
11 |
import jdeserialize.classdesc; |
|
12 |
import jdeserialize.content; |
|
13 |
import jdeserialize.jdeserialize; |
|
14 |
|
|
15 |
public class App { |
|
9 | 16 |
|
10 |
public class App |
|
11 |
{ |
|
12 |
|
|
13 | 17 |
/** |
14 |
* When the Serialized and deserialized classes are same, but placed in diff |
|
15 |
* packages, the deserialization does not work. |
|
16 |
* When they are |
|
17 |
* **/ |
|
18 |
public static void main(String[] args) throws Exception |
|
19 |
{ |
|
20 |
App thisApp = new App(); |
|
18 |
* When the Serialized and deserialized classes are same, but placed in diff packages, the |
|
19 |
* deserialization does not work. When they are |
|
20 |
**/ |
|
21 |
public static void main(String[] args) throws Exception { |
|
22 |
App app = new App(); |
|
21 | 23 |
|
22 |
//thisApp.pureSerializationDeserialization();
|
|
24 |
// app.standartSerializationDeserialization();
|
|
23 | 25 |
|
24 |
//https://code.google.com/archive/p/jdeserialize/wikis/Documentation.wiki |
|
25 |
String[] myArgs= new String[1]; |
|
26 |
myArgs[0] = "serialized/simple.out"; |
|
27 |
//testing call without any options. Outputs the deserialized data into console. |
|
28 |
jdeserialize.jdeserialize.main(myArgs); |
|
26 |
// https://code.google.com/archive/p/jdeserialize/wikis/Documentation.wiki |
|
27 |
//test with calling the name of original jd project |
|
28 |
//app.jdCallMain(); |
|
29 |
|
|
30 |
// test directly through code call |
|
31 |
app.jdCallLib("serialized/simple.out"); |
|
29 | 32 |
} |
30 | 33 |
|
34 |
private void jdCallLib(String filename) |
|
35 |
{ |
|
36 |
try |
|
37 |
{ |
|
38 |
//FileInputStream as usual |
|
39 |
FileInputStream fis = null; |
|
40 |
fis = new FileInputStream(filename); |
|
41 |
|
|
42 |
//creating an instance of jdeserialize with given filename |
|
43 |
jdeserialize jd = new jdeserialize(filename); |
|
44 |
//running the main process of deserialization |
|
45 |
jd.run(fis, false); |
|
46 |
|
|
47 |
//gets the "contents" into an array - returnes the deserialization of all |
|
48 |
// 'writes' into serialized object via objectOutputStream |
|
49 |
ArrayList<content> cntnts = (ArrayList<content>) jd.getContent(); |
|
50 |
|
|
51 |
//gets first object of content type |
|
52 |
// - tested only with just one object. |
|
53 |
// the content here is just an interface. |
|
54 |
// The classes that implements the interface content (all of them through class contentbase) |
|
55 |
// are: arrayobj, blockdata, classdesc, classobj, enumobj, exceptionstate, instance, stringobj |
|
56 |
content locContent = cntnts.get(0); |
|
31 | 57 |
|
32 |
private void pureSerializationDeserialization() |
|
58 |
classdesc objectDescription = locContent. |
|
59 |
|
|
60 |
|
|
61 |
int c = 0; |
|
62 |
} |
|
63 |
catch (IOException e) |
|
64 |
{ |
|
65 |
|
|
66 |
} |
|
67 |
} |
|
68 |
|
|
69 |
private void jdCallMain(String filename) |
|
33 | 70 |
{ |
34 |
try{ |
|
71 |
String[] myArgs= new String[1]; |
|
72 |
myArgs[0] = filename; |
|
73 |
jdeserialize.main(myArgs); //testing call without any options. Outputs the deserialized data into console. |
|
74 |
} |
|
75 |
|
|
76 |
private void standartSerializationDeserialization() { |
|
77 |
try { |
|
35 | 78 |
System.out.println("Starting..."); |
36 | 79 |
|
37 | 80 |
this.serialize(new samples.ok.Simple(), "serialized/simple.out"); |
38 |
|
|
81 |
|
|
39 | 82 |
samples.nok.Simple test1 = this.deserialize("serialized/simple.out"); |
40 | 83 |
|
41 |
}catch(Exception e) |
|
42 |
{ |
|
84 |
} catch (Exception e) { |
|
43 | 85 |
System.out.println(e.toString()); |
44 | 86 |
System.out.println(e.getMessage()); |
45 | 87 |
} |
46 | 88 |
} |
47 | 89 |
|
48 |
/**
|
|
49 |
* Standard way of deserializing objects with java.
|
|
90 |
/** |
|
91 |
* Standard way of deserializing objects with java. |
|
50 | 92 |
* |
51 | 93 |
* Taken from example method, doc |
52 |
**/ |
|
53 |
private <T> T deserialize(String inputFilePath) |
|
54 |
{ |
|
55 |
try |
|
56 |
{ |
|
94 |
**/ |
|
95 |
private <T> T deserialize(String inputFilePath) { |
|
96 |
try { |
|
57 | 97 |
FileInputStream fis = new FileInputStream(inputFilePath); |
58 | 98 |
ObjectInputStream ois = new ObjectInputStream(fis); |
59 | 99 |
T deserialized = (T) ois.readObject(); |
... | ... | |
61 | 101 |
ois.close(); |
62 | 102 |
|
63 | 103 |
return deserialized; |
64 |
} |
|
65 |
catch (IOException e) |
|
66 |
{ |
|
104 |
} catch (IOException e) { |
|
67 | 105 |
e.printStackTrace(); |
68 |
} |
|
69 |
catch (ClassNotFoundException e) |
|
70 |
{ |
|
106 |
} catch (ClassNotFoundException e) { |
|
71 | 107 |
e.printStackTrace(); |
72 | 108 |
} |
73 | 109 |
return null; |
74 | 110 |
} |
75 | 111 |
|
76 |
/**
|
|
77 |
* Standard way of serializing objects with java.
|
|
112 |
/** |
|
113 |
* Standard way of serializing objects with java. |
|
78 | 114 |
* |
79 | 115 |
* Taken from example method, doc |
80 |
**/ |
|
81 |
private void serialize(Object obj, String outputPath) |
|
82 |
{ |
|
83 |
try |
|
84 |
{ |
|
116 |
**/ |
|
117 |
private void serialize(Object obj, String outputPath) { |
|
118 |
try { |
|
85 | 119 |
File toWrite = new File(outputPath); |
86 | 120 |
toWrite.createNewFile(); |
87 | 121 |
|
88 |
FileOutputStream fos = new FileOutputStream(toWrite, false); |
|
89 |
ObjectOutputStream oos = new ObjectOutputStream(fos); |
|
90 |
|
|
91 |
oos.writeObject(obj); |
|
92 |
oos.flush(); |
|
93 |
oos.close(); |
|
94 |
System.out.println("serialized."); |
|
95 |
} |
|
96 |
catch (Exception e) |
|
97 |
{ |
|
98 |
e.printStackTrace(); |
|
99 |
} |
|
122 |
FileOutputStream fos = new FileOutputStream(toWrite, false); |
|
123 |
ObjectOutputStream oos = new ObjectOutputStream(fos); |
|
124 |
|
|
125 |
oos.writeObject(obj); |
|
126 |
oos.flush(); |
|
127 |
oos.close(); |
|
128 |
System.out.println("serialized."); |
|
129 |
} catch (Exception e) { |
|
130 |
e.printStackTrace(); |
|
131 |
} |
|
100 | 132 |
} |
101 |
} |
|
133 |
} |
Také k dispozici: Unified diff
#7768
Digging through jdeserialize and its data types of result