1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.serializecircular;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.ObjectInputStream;
22 import java.io.ObjectOutputStream;
23
24 public class UtilityTester {
25
26 public static void serializeAndDeserializeObject(Object myObject) {
27
28 try {
29 deserialzeObject(serializeObject(myObject));
30 } catch (IOException e) {
31 System.out.println("Exception: " + e.toString());
32 }
33 }
34
35 private static byte[] serializeObject(Object myObject) throws IOException {
36 try {
37 ByteArrayOutputStream myByteArrayOutputStream = new ByteArrayOutputStream();
38
39
40 try (ObjectOutputStream myObjectOutputStream = new ObjectOutputStream(myByteArrayOutputStream)) {
41 myObjectOutputStream.writeObject(myObject);
42 }
43
44
45 byte[] myResult = myByteArrayOutputStream.toByteArray();
46 return myResult;
47 } catch (Exception anException) {
48 throw new RuntimeException("Problem serializing: " + anException.toString(), anException);
49 }
50 }
51
52 private static Object deserialzeObject(byte[] aSerializedObject) {
53
54 try (ObjectInputStream myObjectInputStream = new ObjectInputStream(new ByteArrayInputStream(aSerializedObject))) {
55 return myObjectInputStream.readObject();
56 } catch (Exception anException) {
57 throw new RuntimeException("Problem deserializing", anException);
58 }
59 }
60
61 }