1 48 49 package com.caucho.hessian.io; 50 51 import java.io.IOException ; 52 import java.util.Date ; 53 54 57 public class BasicSerializer extends AbstractSerializer { 58 public static final int NULL = 0; 59 public static final int BOOLEAN = NULL + 1; 60 public static final int BYTE = BOOLEAN + 1; 61 public static final int SHORT = BYTE + 1; 62 public static final int INTEGER = SHORT + 1; 63 public static final int LONG = INTEGER + 1; 64 public static final int FLOAT = LONG + 1; 65 public static final int DOUBLE = FLOAT + 1; 66 public static final int CHARACTER = DOUBLE + 1; 67 public static final int STRING = CHARACTER + 1; 68 public static final int DATE = STRING + 1; 69 public static final int NUMBER = DATE + 1; 70 public static final int OBJECT = NUMBER + 1; 71 72 public static final int BOOLEAN_ARRAY = OBJECT + 1; 73 public static final int BYTE_ARRAY = BOOLEAN_ARRAY + 1; 74 public static final int SHORT_ARRAY = BYTE_ARRAY + 1; 75 public static final int INTEGER_ARRAY = SHORT_ARRAY + 1; 76 public static final int LONG_ARRAY = INTEGER_ARRAY + 1; 77 public static final int FLOAT_ARRAY = LONG_ARRAY + 1; 78 public static final int DOUBLE_ARRAY = FLOAT_ARRAY + 1; 79 public static final int CHARACTER_ARRAY = DOUBLE_ARRAY + 1; 80 public static final int STRING_ARRAY = CHARACTER_ARRAY + 1; 81 public static final int OBJECT_ARRAY = STRING_ARRAY + 1; 82 83 private int code; 84 85 public BasicSerializer(int code) 86 { 87 this.code = code; 88 } 89 90 public void writeObject(Object obj, AbstractHessianOutput out) 91 throws IOException 92 { 93 switch (code) { 94 case BOOLEAN: 95 out.writeBoolean(((Boolean ) obj).booleanValue()); 96 break; 97 98 case BYTE: 99 case SHORT: 100 case INTEGER: 101 out.writeInt(((Number ) obj).intValue()); 102 break; 103 104 case LONG: 105 out.writeLong(((Number ) obj).longValue()); 106 break; 107 108 case FLOAT: 109 case DOUBLE: 110 out.writeDouble(((Number ) obj).doubleValue()); 111 break; 112 113 case CHARACTER: 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 { 127 if (out.addRef(obj)) 128 return; 129 130 boolean []data = (boolean []) obj; 131 boolean hasEnd = out.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 { 143 byte []data = (byte []) obj; 144 out.writeBytes(data, 0, data.length); 145 break; 146 } 147 148 case SHORT_ARRAY: 149 { 150 if (out.addRef(obj)) 151 return; 152 153 short []data = (short []) obj; 154 boolean hasEnd = out.writeListBegin(data.length, "[short"); 155 156 for (int i = 0; i < data.length; i++) 157 out.writeInt(data[i]); 158 159 if (hasEnd) 160 out.writeListEnd(); 161 break; 162 } 163 164 case INTEGER_ARRAY: 165 { 166 if (out.addRef(obj)) 167 return; 168 169 int []data = (int []) obj; 170 171 boolean hasEnd = out.writeListBegin(data.length, "[int"); 172 173 for (int i = 0; i < data.length; i++) 174 out.writeInt(data[i]); 175 176 if (hasEnd) 177 out.writeListEnd(); 178 179 break; 180 } 181 182 case LONG_ARRAY: 183 { 184 if (out.addRef(obj)) 185 return; 186 187 long []data = (long []) obj; 188 189 boolean hasEnd = out.writeListBegin(data.length, "[long"); 190 191 for (int i = 0; i < data.length; i++) 192 out.writeLong(data[i]); 193 194 if (hasEnd) 195 out.writeListEnd(); 196 break; 197 } 198 199 case FLOAT_ARRAY: 200 { 201 if (out.addRef(obj)) 202 return; 203 204 float []data = (float []) obj; 205 206 boolean hasEnd = out.writeListBegin(data.length, "[float"); 207 208 for (int i = 0; i < data.length; i++) 209 out.writeDouble(data[i]); 210 211 if (hasEnd) 212 out.writeListEnd(); 213 break; 214 } 215 216 case DOUBLE_ARRAY: 217 { 218 if (out.addRef(obj)) 219 return; 220 221 double []data = (double []) obj; 222 boolean hasEnd = out.writeListBegin(data.length, "[double"); 223 224 for (int i = 0; i < data.length; i++) 225 out.writeDouble(data[i]); 226 227 if (hasEnd) 228 out.writeListEnd(); 229 break; 230 } 231 232 case STRING_ARRAY: 233 { 234 if (out.addRef(obj)) 235 return; 236 237 String []data = (String []) obj; 238 239 boolean hasEnd = out.writeListBegin(data.length, "[string"); 240 241 for (int i = 0; i < data.length; i++) { 242 out.writeString(data[i]); 243 } 244 245 if (hasEnd) 246 out.writeListEnd(); 247 break; 248 } 249 250 case CHARACTER_ARRAY: 251 { 252 char []data = (char []) obj; 253 out.writeString(data, 0, data.length); 254 break; 255 } 256 257 case OBJECT_ARRAY: 258 { 259 if (out.addRef(obj)) 260 return; 261 262 Object []data = (Object []) obj; 263 264 boolean hasEnd = out.writeListBegin(data.length, "[object"); 265 266 for (int i = 0; i < data.length; i++) { 267 out.writeObject(data[i]); 268 } 269 270 if (hasEnd) 271 out.writeListEnd(); 272 break; 273 } 274 275 case NULL: 276 out.writeNull(); 277 break; 278 279 default: 280 throw new RuntimeException (code + " " + String.valueOf(obj.getClass())); 281 } 282 } 283 } 284 | Popular Tags |