1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.orangesignal.csv;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25 import java.io.OutputStream;
26 import java.io.Serializable;
27
28
29
30
31
32
33
34 abstract class SerializationUtils {
35
36
37
38
39 protected SerializationUtils() {
40 }
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public static Object clone(final Serializable object) {
56 return deserialize(serialize(object));
57 }
58
59
60
61
62
63
64
65
66
67
68
69 public static void serialize(final Serializable obj, final OutputStream outputStream) {
70 if (outputStream == null) {
71 throw new IllegalArgumentException("The OutputStream must not be null");
72 }
73 ObjectOutputStream out = null;
74 try {
75 out = new ObjectOutputStream(outputStream);
76 out.writeObject(obj);
77 } catch (final IOException e) {
78 throw new IllegalStateException(e.getMessage(), e);
79 } finally {
80 Csv.closeQuietly(out);
81 }
82 }
83
84
85
86
87
88
89
90
91 public static byte[] serialize(final Serializable obj) {
92 final ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
93 serialize(obj, baos);
94 return baos.toByteArray();
95 }
96
97
98
99
100
101
102
103
104
105
106
107 public static Object deserialize(final InputStream inputStream) {
108 if (inputStream == null) {
109 throw new IllegalArgumentException("The InputStream must not be null");
110 }
111 ObjectInputStream in = null;
112 try {
113 in = new ObjectInputStream(inputStream);
114 return in.readObject();
115 } catch (final ClassNotFoundException e) {
116 throw new IllegalStateException(e.getMessage(), e);
117 } catch (final IOException e) {
118 throw new IllegalStateException(e.getMessage(), e);
119 } finally {
120 Csv.closeQuietly(in);
121 }
122 }
123
124
125
126
127
128
129
130
131
132 public static Object deserialize(final byte[] objectData) {
133 if (objectData == null) {
134 throw new IllegalArgumentException("The byte[] must not be null");
135 }
136 return deserialize(new ByteArrayInputStream(objectData));
137 }
138
139 }