|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package net.sf.sojo.interchange.object; |
|
17 |
| |
|
18 |
| import java.io.ByteArrayInputStream; |
|
19 |
| import java.io.ByteArrayOutputStream; |
|
20 |
| import java.io.FileInputStream; |
|
21 |
| import java.io.FileOutputStream; |
|
22 |
| import java.io.IOException; |
|
23 |
| import java.io.InputStream; |
|
24 |
| import java.io.ObjectInputStream; |
|
25 |
| import java.io.ObjectOutputStream; |
|
26 |
| import java.io.OutputStream; |
|
27 |
| |
|
28 |
| import net.sf.sojo.core.NonCriticalExceptionHandler; |
|
29 |
| import net.sf.sojo.interchange.AbstractSerializer; |
|
30 |
| import net.sf.sojo.interchange.SerializerException; |
|
31 |
| |
|
32 |
| |
|
33 |
| public class ObjectSerializer extends AbstractSerializer { |
|
34 |
| |
|
35 |
| private boolean convertBySerialization = true; |
|
36 |
| |
|
37 |
| |
|
38 |
39
| public boolean getConvertBySerialization() { return convertBySerialization; }
|
|
39 |
2
| public void setConvertBySerialization(boolean pvConvertBySerialization) { convertBySerialization = pvConvertBySerialization; }
|
|
40 |
| |
|
41 |
19
| public Object serialize(Object pvRootObject, String[] pvExcludedProperties) {
|
|
42 |
19
| ObjectOutputStream lvObjectOutputStream = null;
|
|
43 |
19
| ByteArrayOutputStream lvArrayOutputStream = new ByteArrayOutputStream();
|
|
44 |
19
| try {
|
|
45 |
19
| Object lvSimple = pvRootObject;
|
|
46 |
19
| if (getConvertBySerialization()) {
|
|
47 |
17
| lvSimple = getObjectUtil().makeSimple(pvRootObject, pvExcludedProperties);
|
|
48 |
| } |
|
49 |
19
| lvObjectOutputStream = new ObjectOutputStream(lvArrayOutputStream);
|
|
50 |
19
| lvObjectOutputStream.writeObject(lvSimple);
|
|
51 |
| } catch (Exception e) { |
|
52 |
1
| throw new SerializerException("Exception by serialize object: " + pvRootObject + " - " + e, e);
|
|
53 |
| } finally { |
|
54 |
19
| try {
|
|
55 |
19
| lvObjectOutputStream.close();
|
|
56 |
| } catch (IOException e) { |
|
57 |
| if (NonCriticalExceptionHandler.isNonCriticalExceptionHandlerEnabled()) { |
|
58 |
| NonCriticalExceptionHandler.handleException(ObjectSerializer.class, "Exception by close ObjectOutputStream: " + lvObjectOutputStream); |
|
59 |
| } |
|
60 |
| } |
|
61 |
| } |
|
62 |
18
| return lvArrayOutputStream.toByteArray();
|
|
63 |
| } |
|
64 |
| |
|
65 |
| |
|
66 |
14
| public Object serialize(Object pvRootObject) {
|
|
67 |
14
| return serialize(pvRootObject, null);
|
|
68 |
| } |
|
69 |
| |
|
70 |
22
| public Object deserialize(Object pvSourceObject, Class pvRootClass) {
|
|
71 |
22
| Object lvReturn = null;
|
|
72 |
| if (pvSourceObject instanceof byte[]) { |
|
73 |
| ObjectInputStream lvObjectInputStream = null; |
|
74 |
| try { |
|
75 |
| byte lvBytes[] = (byte[]) pvSourceObject; |
|
76 |
| ByteArrayInputStream lvArrayInputStream = new ByteArrayInputStream(lvBytes); |
|
77 |
| lvObjectInputStream = new ObjectInputStream(lvArrayInputStream); |
|
78 |
| lvReturn = lvObjectInputStream.readObject(); |
|
79 |
| if (getConvertBySerialization()) { |
|
80 |
| lvReturn = getObjectUtil().makeComplex(lvReturn, pvRootClass); |
|
81 |
| } |
|
82 |
| } catch (Exception e) { |
|
83 |
| throw new SerializerException("Exception by deserialize object: " + lvReturn + " - " + e, e); |
|
84 |
| } finally { |
|
85 |
| try { |
|
86 |
| if (lvObjectInputStream != null) { |
|
87 |
| lvObjectInputStream.close(); |
|
88 |
| } |
|
89 |
| } catch (IOException e) { |
|
90 |
| if (NonCriticalExceptionHandler.isNonCriticalExceptionHandlerEnabled()) { |
|
91 |
| NonCriticalExceptionHandler.handleException(ObjectSerializer.class, e, "Exception by close ObjectInputStream: " + lvObjectInputStream); |
|
92 |
| } |
|
93 |
| } |
|
94 |
| } |
|
95 |
| |
|
96 |
| } else { |
|
97 |
| String lvClassName = (pvSourceObject == null ? null : pvSourceObject.getClass().getName()); |
|
98 |
| throw new SerializerException("The deserialize object must be an byte array and not: " + lvClassName); |
|
99 |
| } |
|
100 |
| |
|
101 |
20
| return lvReturn;
|
|
102 |
| } |
|
103 |
| |
|
104 |
| |
|
105 |
2
| public void serializeToFile(final Object pvRootObject, String pvPath) throws IOException {
|
|
106 |
2
| FileOutputStream lvFileOutputStream = null;
|
|
107 |
2
| try {
|
|
108 |
2
| lvFileOutputStream = new FileOutputStream(pvPath);
|
|
109 |
1
| serializeToOutputStream(pvRootObject, lvFileOutputStream);
|
|
110 |
| } finally { |
|
111 |
2
| try {
|
|
112 |
2
| if (lvFileOutputStream != null) {
|
|
113 |
1
| lvFileOutputStream.close();
|
|
114 |
| } |
|
115 |
| } catch (Exception e) { |
|
116 |
| if (NonCriticalExceptionHandler.isNonCriticalExceptionHandlerEnabled()) { |
|
117 |
| NonCriticalExceptionHandler.handleException(ObjectSerializer.class, e, "Exception by close FileOutputStream: " + lvFileOutputStream + " and path: " + pvPath); |
|
118 |
| } |
|
119 |
| } |
|
120 |
| } |
|
121 |
| } |
|
122 |
| |
|
123 |
1
| public void serializeToOutputStream(final Object pvRootObject, OutputStream pvOutputStream) throws IOException {
|
|
124 |
1
| byte lvBytes[] = (byte[]) serialize(pvRootObject);
|
|
125 |
1
| pvOutputStream.write(lvBytes);
|
|
126 |
| } |
|
127 |
| |
|
128 |
2
| public Object deserializeFromFile(String pvPath) throws IOException, ClassNotFoundException {
|
|
129 |
2
| FileInputStream lvFileInputStream = new FileInputStream(pvPath);
|
|
130 |
1
| Object lvReturn = deserializeFromInputStream(lvFileInputStream);
|
|
131 |
1
| return lvReturn;
|
|
132 |
| } |
|
133 |
| |
|
134 |
1
| public Object deserializeFromInputStream(final InputStream pvInputStream) throws IOException, ClassNotFoundException {
|
|
135 |
1
| Object lvReturn = null;
|
|
136 |
1
| int i = 1;
|
|
137 |
1
| byte b[] = new byte[1024];
|
|
138 |
1
| while (i > 0) {
|
|
139 |
2
| i = pvInputStream.read(b);
|
|
140 |
| } |
|
141 |
1
| lvReturn = deserialize(b);
|
|
142 |
1
| return lvReturn;
|
|
143 |
| } |
|
144 |
| |
|
145 |
| |
|
146 |
| } |