01: /*
02: * Created on 24.08.2006
03: *
04: */
05: package org.jdesktop.test;
06:
07: import java.io.ByteArrayInputStream;
08: import java.io.ByteArrayOutputStream;
09: import java.io.IOException;
10: import java.io.ObjectInputStream;
11: import java.io.ObjectOutputStream;
12:
13: /**
14: * Support class to test serializable behaviour.
15: *
16: * @author <a href="mailto:jesse@swank.ca">Jesse Wilson</a>
17: */
18: public class SerializableSupport {
19:
20: /**
21: * Serialize the specified object to bytes, then deserialize it back.
22: */
23: public static <T> T serialize(T object) throws IOException,
24: ClassNotFoundException {
25: return (T) fromBytes(toBytes(object));
26: }
27:
28: public static byte[] toBytes(Object object) throws IOException {
29: ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
30: ObjectOutputStream objectsOut = new ObjectOutputStream(bytesOut);
31: objectsOut.writeObject(object);
32: return bytesOut.toByteArray();
33: }
34:
35: public static Object fromBytes(byte[] bytes) throws IOException,
36: ClassNotFoundException {
37: ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytes);
38: ObjectInputStream objectsIn = new ObjectInputStream(bytesIn);
39: return objectsIn.readObject();
40: }
41:
42: }
|