001: /*
002: * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
003: *
004: * The Apache Software License, Version 1.1
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution, if
019: * any, must include the following acknowlegement:
020: * "This product includes software developed by the
021: * Caucho Technology (http://www.caucho.com/)."
022: * Alternately, this acknowlegement may appear in the software itself,
023: * if and wherever such third-party acknowlegements normally appear.
024: *
025: * 4. The names "Burlap", "Resin", and "Caucho" must not be used to
026: * endorse or promote products derived from this software without prior
027: * written permission. For written permission, please contact
028: * info@caucho.com.
029: *
030: * 5. Products derived from this software may not be called "Resin"
031: * nor may "Resin" appear in their names without prior written
032: * permission of Caucho Technology.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
038: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
039: * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
040: * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
041: * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
042: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
043: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
044: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
045: *
046: * @author Scott Ferguson
047: */
048:
049: package com.caucho.hessian.io;
050:
051: import java.io.IOException;
052: import java.util.Date;
053:
054: /**
055: * Serializing an object for known object types.
056: */
057: public class BasicSerializer extends AbstractSerializer {
058: public static final int NULL = 0;
059: public static final int BOOLEAN = NULL + 1;
060: public static final int BYTE = BOOLEAN + 1;
061: public static final int SHORT = BYTE + 1;
062: public static final int INTEGER = SHORT + 1;
063: public static final int LONG = INTEGER + 1;
064: public static final int FLOAT = LONG + 1;
065: public static final int DOUBLE = FLOAT + 1;
066: public static final int CHARACTER = DOUBLE + 1;
067: public static final int CHARACTER_OBJECT = CHARACTER + 1;
068: public static final int STRING = CHARACTER_OBJECT + 1;
069: public static final int DATE = STRING + 1;
070: public static final int NUMBER = DATE + 1;
071: public static final int OBJECT = NUMBER + 1;
072:
073: public static final int BOOLEAN_ARRAY = OBJECT + 1;
074: public static final int BYTE_ARRAY = BOOLEAN_ARRAY + 1;
075: public static final int SHORT_ARRAY = BYTE_ARRAY + 1;
076: public static final int INTEGER_ARRAY = SHORT_ARRAY + 1;
077: public static final int LONG_ARRAY = INTEGER_ARRAY + 1;
078: public static final int FLOAT_ARRAY = LONG_ARRAY + 1;
079: public static final int DOUBLE_ARRAY = FLOAT_ARRAY + 1;
080: public static final int CHARACTER_ARRAY = DOUBLE_ARRAY + 1;
081: public static final int STRING_ARRAY = CHARACTER_ARRAY + 1;
082: public static final int OBJECT_ARRAY = STRING_ARRAY + 1;
083:
084: private int code;
085:
086: public BasicSerializer(int code) {
087: this .code = code;
088: }
089:
090: public void writeObject(Object obj, AbstractHessianOutput out)
091: throws IOException {
092: switch (code) {
093: case BOOLEAN:
094: out.writeBoolean(((Boolean) obj).booleanValue());
095: break;
096:
097: case BYTE:
098: case SHORT:
099: case INTEGER:
100: out.writeInt(((Number) obj).intValue());
101: break;
102:
103: case LONG:
104: out.writeLong(((Number) obj).longValue());
105: break;
106:
107: case FLOAT:
108: case DOUBLE:
109: out.writeDouble(((Number) obj).doubleValue());
110: break;
111:
112: case CHARACTER:
113: case CHARACTER_OBJECT:
114: out.writeString(String.valueOf(obj));
115: break;
116:
117: case STRING:
118: out.writeString((String) obj);
119: break;
120:
121: case DATE:
122: out.writeUTCDate(((Date) obj).getTime());
123: break;
124:
125: case BOOLEAN_ARRAY: {
126: if (out.addRef(obj))
127: return;
128:
129: boolean[] data = (boolean[]) obj;
130: boolean hasEnd = out
131: .writeListBegin(data.length, "[boolean");
132: for (int i = 0; i < data.length; i++)
133: out.writeBoolean(data[i]);
134:
135: if (hasEnd)
136: out.writeListEnd();
137:
138: break;
139: }
140:
141: case BYTE_ARRAY: {
142: byte[] data = (byte[]) obj;
143: out.writeBytes(data, 0, data.length);
144: break;
145: }
146:
147: case SHORT_ARRAY: {
148: if (out.addRef(obj))
149: return;
150:
151: short[] data = (short[]) obj;
152: boolean hasEnd = out.writeListBegin(data.length, "[short");
153:
154: for (int i = 0; i < data.length; i++)
155: out.writeInt(data[i]);
156:
157: if (hasEnd)
158: out.writeListEnd();
159: break;
160: }
161:
162: case INTEGER_ARRAY: {
163: if (out.addRef(obj))
164: return;
165:
166: int[] data = (int[]) obj;
167:
168: boolean hasEnd = out.writeListBegin(data.length, "[int");
169:
170: for (int i = 0; i < data.length; i++)
171: out.writeInt(data[i]);
172:
173: if (hasEnd)
174: out.writeListEnd();
175:
176: break;
177: }
178:
179: case LONG_ARRAY: {
180: if (out.addRef(obj))
181: return;
182:
183: long[] data = (long[]) obj;
184:
185: boolean hasEnd = out.writeListBegin(data.length, "[long");
186:
187: for (int i = 0; i < data.length; i++)
188: out.writeLong(data[i]);
189:
190: if (hasEnd)
191: out.writeListEnd();
192: break;
193: }
194:
195: case FLOAT_ARRAY: {
196: if (out.addRef(obj))
197: return;
198:
199: float[] data = (float[]) obj;
200:
201: boolean hasEnd = out.writeListBegin(data.length, "[float");
202:
203: for (int i = 0; i < data.length; i++)
204: out.writeDouble(data[i]);
205:
206: if (hasEnd)
207: out.writeListEnd();
208: break;
209: }
210:
211: case DOUBLE_ARRAY: {
212: if (out.addRef(obj))
213: return;
214:
215: double[] data = (double[]) obj;
216: boolean hasEnd = out.writeListBegin(data.length, "[double");
217:
218: for (int i = 0; i < data.length; i++)
219: out.writeDouble(data[i]);
220:
221: if (hasEnd)
222: out.writeListEnd();
223: break;
224: }
225:
226: case STRING_ARRAY: {
227: if (out.addRef(obj))
228: return;
229:
230: String[] data = (String[]) obj;
231:
232: boolean hasEnd = out.writeListBegin(data.length, "[string");
233:
234: for (int i = 0; i < data.length; i++) {
235: out.writeString(data[i]);
236: }
237:
238: if (hasEnd)
239: out.writeListEnd();
240: break;
241: }
242:
243: case CHARACTER_ARRAY: {
244: char[] data = (char[]) obj;
245: out.writeString(data, 0, data.length);
246: break;
247: }
248:
249: case OBJECT_ARRAY: {
250: if (out.addRef(obj))
251: return;
252:
253: Object[] data = (Object[]) obj;
254:
255: boolean hasEnd = out.writeListBegin(data.length, "[object");
256:
257: for (int i = 0; i < data.length; i++) {
258: out.writeObject(data[i]);
259: }
260:
261: if (hasEnd)
262: out.writeListEnd();
263: break;
264: }
265:
266: case NULL:
267: out.writeNull();
268: break;
269:
270: default:
271: throw new RuntimeException(code + " "
272: + String.valueOf(obj.getClass()));
273: }
274: }
275: }
|