-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.java
More file actions
52 lines (48 loc) · 1.98 KB
/
Copy pathUtil.java
File metadata and controls
52 lines (48 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.io.*;
public class Util {
public static byte[] serialize(Serializable toFlatten) {
byte[] serialized = null;
ObjectOutputStream objectOutputStream = null;
try {
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(toFlatten);
serialized = byteArrayOutputStream.toByteArray();
} catch (Exception problem) {
Logger.logError("Unable to serialize the provided object to a byte array");
problem.printStackTrace();
} finally {
try {
objectOutputStream.close();
} catch (Exception problem) {
Logger.logError("Unable to serialize the provided object to a byte array");
problem.printStackTrace();
}
}
return serialized;
}
public static Object deserialize(final byte[] flattened) {
Object deserialized = null;
if (flattened == null || flattened.length == 0) {
Logger.logError("Cannot deserialize an empty or null byte array");
return deserialized;
}
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(new ByteArrayInputStream(flattened));
deserialized = objectInputStream.readObject();
} catch (Exception problem) {
Logger.logError("Unable to deserialize the provided byte array to an object");
problem.printStackTrace();
} finally {
if (objectInputStream != null)
try {
objectInputStream.close();
} catch (Exception problem) {
Logger.logError("Unable to deserialize the provided byte array to an object");
problem.printStackTrace();
}
}
return deserialized;
}
}