CompareSerialization.java
01 package com.antwerkz.articles.serialization;
02 
03 import java.beans.XMLDecoder;
04 import java.beans.XMLEncoder;
05 import java.io.FileInputStream;
06 import java.io.FileOutputStream;
07 import java.io.FileReader;
08 import java.io.FileWriter;
09 import java.io.IOException;
10 import java.io.PrintStream;
11 import java.io.FileNotFoundException;
12 import JSX.ObjIn;
13 import JSX.ObjOut;
14 import com.thoughtworks.xstream.XStream;
15 import com.thoughtworks.xstream.io.xml.DomDriver;
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 
19 /**
20  * Created Jan 18, 2005
21  *
22  @author <a href="mailto:jlee@antwerkz.com">Justin Lee</a>
23  */
24 @SuppressWarnings({"IOResourceOpenedButNotSafelyClosed"})
25 public class CompareSerialization {
26     private Person _person;
27     private static Log log = LogFactory.getLog(CompareSerialization.class);
28     private static final String FILES_DIR = "files/";
29 
30     public CompareSerialization() throws FileNotFoundException {
31         System.setOut(new PrintStream(new FileOutputStream(FILES_DIR + "sys.out")));
32     }
33 
34     public void execute() {
35         try {
36             createData();
37             serializeWithJDK();
38             serializeWithJSX();
39             serializeWithXStream();
40         catch(Exception e) {
41             log.error("Some test failed", e);
42         }
43     }
44 
45     private void serializeWithXStream() throws IOException {
46         XStream stream = new XStream(new DomDriver());
47         String file = FILES_DIR + "XStream.xml";
48         stream.toXML(_person, new FileWriter(file));
49 
50         Person decoded = (Person)stream.fromXML(new FileReader(file));
51         System.out.println("Are they equal with XStream? "
52             + _person.equals(decoded));
53     }
54 
55     private void serializeWithJSX() throws IOException, ClassNotFoundException {
56         String file = FILES_DIR + "JSX.xml";
57         ObjOut out = new ObjOut(new FileOutputStream(file));
58         out.writeObject(_person);
59 
60         ObjIn in = new ObjIn(new FileReader(file));
61         Person decoded = (Person)in.readObject();
62         System.out.println("Are they equal with JSX? "
63             + _person.equals(decoded));
64     }
65 
66     private void serializeWithJDK() throws IOException {
67         String file = FILES_DIR + "XMLEncoder.xml";
68         XMLEncoder encoder = new XMLEncoder(new FileOutputStream(file));
69         encoder.writeObject(_person);
70         encoder.close();
71 
72         XMLDecoder decoder = new XMLDecoder(new FileInputStream(file));
73         Person decoded = (Person)decoder.readObject();
74         System.out.println("Are they equal with XMLEncoder? "
75             + _person.equals(decoded));
76     }
77 
78     private void createData() {
79         _person = new Person("Test""Person");
80         createItems(_person);
81     }
82 
83     private void createItems(Person person) {
84         person.add(new Item("item1"1.00D"I'm Item 1!"));
85         person.add(new Item("item1"2.00D"I'm Item 2!"));
86         person.add(new Item("item1"3.00D"I'm Item 3!"));
87         person.add(new Item("item1"4.00D"I'm Item 4!"));
88         person.add(new Item("item1"5.00D"I'm Item 5!"));
89     }
90 
91     public static void main(String[] args) {
92         try {
93             CompareSerialization c = new CompareSerialization();
94             c.execute();
95         catch(Exception e) {
96             log.error("oops", e);
97         }
98     }
99 }