001: package example;
002:
003: import java.io.*;
004: import java.util.*;
005:
006: import javax.servlet.*;
007: import javax.servlet.http.*;
008:
009: import com.caucho.hessian.io.*;
010:
011: public class HessianSerializeServlet extends HttpServlet {
012: /**
013: * The servlet serializes three Car objects into a byte[] array
014: * using Hessian 2.0, Hessian 2.0 with a Deflation envelope, and
015: * java.io.ObjectOutputStream serialization.
016: */
017: public void service(HttpServletRequest req, HttpServletResponse res)
018: throws IOException, ServletException {
019: res.setContentType("text/html");
020:
021: PrintWriter out = res.getWriter();
022:
023: byte[] data = hessianSerialize();
024:
025: out.println("<pre>");
026: out.println("Hessian serialize size: " + data.length);
027: out.println("Deserialize: " + hessianDeserialize(data));
028: out.println("");
029:
030: data = hessianDeflate();
031:
032: out.println("Deflate serialize size: " + data.length);
033: out.println("Inflate: " + hessianInflate(data));
034: out.println("");
035:
036: data = javaSerialize();
037: out.println("java.io serialize size: " + data.length);
038: }
039:
040: /**
041: * Hessian 2.0 serialization API resembles the java.io.ObjectOutputStream.
042: */
043: private byte[] hessianSerialize() throws IOException {
044: ByteArrayOutputStream bos = new ByteArrayOutputStream();
045:
046: Hessian2Output out = new Hessian2Output(bos);
047:
048: hessianSerialize(out);
049:
050: out.close();
051:
052: return bos.toByteArray();
053: }
054:
055: /**
056: * Hessian 2.0 deserialization API resembles the java.io.ObjectOutputStream.
057: */
058: private Object hessianDeserialize(byte[] data) throws IOException {
059: ByteArrayInputStream bis = new ByteArrayInputStream(data);
060:
061: Hessian2Input in = new Hessian2Input(bis);
062:
063: return hessianDeserialize(in);
064: }
065:
066: /**
067: * Serialization with compression is wraps the Hessian2Output stream
068: * in a compression envelope. The serialization itself is identical
069: * with and without the envelope.
070: */
071: private byte[] hessianDeflate() throws IOException {
072: Deflation envelope = new Deflation();
073:
074: ByteArrayOutputStream bos = new ByteArrayOutputStream();
075:
076: Hessian2Output out = new Hessian2Output(bos);
077:
078: out = envelope.wrap(out);
079:
080: hessianSerialize(out);
081:
082: out.close();
083:
084: return bos.toByteArray();
085: }
086:
087: private Object hessianInflate(byte[] data) throws IOException {
088: Deflation envelope = new Deflation();
089:
090: ByteArrayInputStream bis = new ByteArrayInputStream(data);
091:
092: Hessian2Input in = new Hessian2Input(bis);
093:
094: in = envelope.unwrap(in);
095:
096: return hessianDeserialize(in);
097: }
098:
099: /**
100: * The example serializes three Car objects into the message. The
101: * application can use any sequence of <code>writeXXX</code> calls
102: * as long as the deserialization follows the same order.
103: */
104: private void hessianSerialize(Hessian2Output out)
105: throws IOException {
106: out.startMessage();
107:
108: out.writeInt(3);
109:
110: Car car1 = new Car(Model.EDSEL, Color.GREEN, 1954);
111:
112: out.writeObject(car1);
113:
114: Car car2 = new Car(Model.MODEL_T, Color.BLACK, 1937);
115:
116: out.writeObject(car2);
117:
118: Car car3 = new Car(Model.CIVIC, Color.BLUE, 1998);
119:
120: out.writeObject(car3);
121:
122: out.completeMessage();
123: }
124:
125: private Object hessianDeserialize(Hessian2Input in)
126: throws IOException {
127: in.startMessage();
128:
129: ArrayList list = new ArrayList();
130:
131: int length = in.readInt();
132:
133: for (int i = 0; i < length; i++) {
134: list.add(in.readObject());
135: }
136:
137: in.completeMessage();
138:
139: return list;
140: }
141:
142: private byte[] javaSerialize() throws IOException {
143: ByteArrayOutputStream bos = new ByteArrayOutputStream();
144:
145: ObjectOutputStream out = new ObjectOutputStream(bos);
146:
147: out.writeInt(3);
148:
149: Car car1 = new Car(Model.EDSEL, Color.GREEN, 1954);
150:
151: out.writeObject(car1);
152:
153: Car car2 = new Car(Model.MODEL_T, Color.BLACK, 1937);
154:
155: out.writeObject(car2);
156:
157: Car car3 = new Car(Model.CIVIC, Color.BLUE, 1998);
158:
159: out.writeObject(car3);
160:
161: out.close();
162:
163: return bos.toByteArray();
164: }
165: }
|