1 18 package org.apache.activemq.util; 19 20 import java.io.DataInput ; 21 import java.io.DataInputStream ; 22 import java.io.DataOutput ; 23 import java.io.DataOutputStream ; 24 import java.io.IOException ; 25 import java.io.OutputStreamWriter ; 26 import java.io.StringWriter ; 27 import java.io.UTFDataFormatException ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.Properties ; 34 35 43 public class MarshallingSupport { 44 45 public static final byte NULL = 0; 46 public static final byte BOOLEAN_TYPE = 1; 47 public static final byte BYTE_TYPE = 2; 48 public static final byte CHAR_TYPE = 3; 49 public static final byte SHORT_TYPE = 4; 50 public static final byte INTEGER_TYPE = 5; 51 public static final byte LONG_TYPE = 6; 52 public static final byte DOUBLE_TYPE = 7; 53 public static final byte FLOAT_TYPE = 8; 54 public static final byte STRING_TYPE = 9; 55 public static final byte BYTE_ARRAY_TYPE = 10; 56 public static final byte MAP_TYPE = 11; 57 public static final byte LIST_TYPE = 12; 58 public static final byte BIG_STRING_TYPE = 13; 59 60 static public void marshalPrimitiveMap(Map map, DataOutputStream out) throws IOException { 61 if( map == null ) { 62 out.writeInt(-1); 63 } else { 64 out.writeInt(map.size()); 65 for (Iterator iter = map.keySet().iterator(); iter.hasNext();) { 66 String name = (String ) iter.next(); 67 out.writeUTF(name); 68 Object value = map.get(name); 69 marshalPrimitive(out, value); 70 } 71 } 72 } 73 74 static public Map unmarshalPrimitiveMap(DataInputStream in) throws IOException { 75 return unmarshalPrimitiveMap(in, Integer.MAX_VALUE); 76 } 77 78 84 public static Map unmarshalPrimitiveMap(DataInputStream in, int max_property_size) throws IOException { 85 int size = in.readInt(); 86 if( size > max_property_size ) { 87 throw new IOException ("Primitive map is larger than the allowed size: "+size); 88 } 89 if( size < 0 ) { 90 return null; 91 } else { 92 HashMap rc = new HashMap (size); 93 for(int i=0; i < size; i++) { 94 String name = in.readUTF(); 95 rc.put(name, unmarshalPrimitive(in)); 96 } 97 return rc; 98 } 99 100 } 101 102 public static void marshalPrimitiveList(List list, DataOutputStream out) throws IOException { 103 out.writeInt(list.size()); 104 for (Iterator iter = list.iterator(); iter.hasNext();) { 105 Object element = (Object ) iter.next(); 106 marshalPrimitive(out, element); 107 } 108 } 109 110 public static List unmarshalPrimitiveList(DataInputStream in) throws IOException { 111 int size = in.readInt(); 112 List answer = new ArrayList (size); 113 while (size-- > 0) { 114 answer.add(unmarshalPrimitive(in)); 115 } 116 return answer; 117 } 118 119 static public void marshalPrimitive(DataOutputStream out, Object value) throws IOException { 120 if( value == null ) { 121 marshalNull(out); 122 } else if( value.getClass() == Boolean .class ) { 123 marshalBoolean(out, ((Boolean )value).booleanValue()); 124 } else if( value.getClass() == Byte .class ) { 125 marshalByte(out, ((Byte )value).byteValue()); 126 } else if( value.getClass() == Character .class ) { 127 marshalChar(out, ((Character )value).charValue()); 128 } else if( value.getClass() == Short .class ) { 129 marshalShort(out, ((Short )value).shortValue()); 130 } else if( value.getClass() == Integer .class ) { 131 marshalInt(out, ((Integer )value).intValue()); 132 } else if( value.getClass() == Long .class ) { 133 marshalLong(out, ((Long )value).longValue()); 134 } else if( value.getClass() == Float .class ) { 135 marshalFloat(out, ((Float )value).floatValue()); 136 } else if( value.getClass() == Double .class ) { 137 marshalDouble(out, ((Double )value).doubleValue()); 138 } else if( value.getClass() == byte[].class ) { 139 marshalByteArray(out, ((byte[])value)); 140 } else if( value.getClass() == String .class ) { 141 marshalString(out, (String )value); 142 } else if( value instanceof Map ) { 143 out.writeByte(MAP_TYPE); 144 marshalPrimitiveMap((Map ) value, out); 145 } else if( value instanceof List ) { 146 out.writeByte(LIST_TYPE); 147 marshalPrimitiveList((List ) value, out); 148 } else { 149 throw new IOException ("Object is not a primitive: "+value); 150 } 151 } 152 153 154 static public Object unmarshalPrimitive(DataInputStream in) throws IOException { 155 Object value=null; 156 switch( in.readByte() ) { 157 case BYTE_TYPE: 158 value = new Byte (in.readByte()); 159 break; 160 case BOOLEAN_TYPE: 161 value = in.readBoolean() ? Boolean.TRUE : Boolean.FALSE; 162 break; 163 case CHAR_TYPE: 164 value = new Character (in.readChar()); 165 break; 166 case SHORT_TYPE: 167 value = new Short (in.readShort()); 168 break; 169 case INTEGER_TYPE: 170 value = new Integer (in.readInt()); 171 break; 172 case LONG_TYPE: 173 value = new Long (in.readLong()); 174 break; 175 case FLOAT_TYPE: 176 value = new Float (in.readFloat()); 177 break; 178 case DOUBLE_TYPE: 179 value = new Double (in.readDouble()); 180 break; 181 case BYTE_ARRAY_TYPE: 182 value = new byte[in.readInt()]; 183 in.readFully((byte[])value); 184 break; 185 case STRING_TYPE: 186 value = in.readUTF(); 187 break; 188 case BIG_STRING_TYPE: 189 value = readUTF8(in); 190 break; 191 case MAP_TYPE: 192 value = unmarshalPrimitiveMap(in); 193 break; 194 case LIST_TYPE: 195 value = unmarshalPrimitiveList(in); 196 break; 197 } 198 return value; 199 } 200 201 public static void marshalNull(DataOutputStream out) throws IOException { 202 out.writeByte(NULL); 203 } 204 205 public static void marshalBoolean(DataOutputStream out, boolean value) throws IOException { 206 out.writeByte(BOOLEAN_TYPE); 207 out.writeBoolean(value); 208 } 209 210 public static void marshalByte(DataOutputStream out, byte value) throws IOException { 211 out.writeByte(BYTE_TYPE); 212 out.writeByte(value); 213 } 214 215 public static void marshalChar(DataOutputStream out, char value) throws IOException { 216 out.writeByte(CHAR_TYPE); 217 out.writeChar(value); 218 } 219 220 public static void marshalShort(DataOutputStream out, short value) throws IOException { 221 out.writeByte(SHORT_TYPE); 222 out.writeShort(value); 223 } 224 225 public static void marshalInt(DataOutputStream out, int value) throws IOException { 226 out.writeByte(INTEGER_TYPE); 227 out.writeInt(value); 228 } 229 230 public static void marshalLong(DataOutputStream out, long value) throws IOException { 231 out.writeByte(LONG_TYPE); 232 out.writeLong(value); 233 } 234 235 public static void marshalFloat(DataOutputStream out, float value) throws IOException { 236 out.writeByte(FLOAT_TYPE); 237 out.writeFloat(value); 238 } 239 240 public static void marshalDouble(DataOutputStream out, double value) throws IOException { 241 out.writeByte(DOUBLE_TYPE); 242 out.writeDouble(value); 243 } 244 245 public static void marshalByteArray(DataOutputStream out, byte[] value) throws IOException { 246 marshalByteArray(out, value, 0, value.length); 247 } 248 249 public static void marshalByteArray(DataOutputStream out, byte[] value, int offset, int length) throws IOException { 250 out.writeByte(BYTE_ARRAY_TYPE); 251 out.writeInt(length); 252 out.write(value, offset, length); 253 } 254 255 256 public static void marshalString(DataOutputStream out, String s) throws IOException { 257 if( s.length() < Short.MAX_VALUE/4 ) { 259 out.writeByte(STRING_TYPE); 260 out.writeUTF(s); 261 } else { 262 out.writeByte(BIG_STRING_TYPE); 263 writeUTF8(out, s); 264 } 265 } 266 267 static public void writeUTF8(DataOutput dataOut, String text) throws IOException { 268 if (text != null) { 269 int strlen = text.length(); 270 int utflen = 0; 271 char[] charr = new char[strlen]; 272 int c, count = 0; 273 274 text.getChars(0, strlen, charr, 0); 275 276 for (int i = 0; i < strlen; i++) { 277 c = charr[i]; 278 if ((c >= 0x0001) && (c <= 0x007F)) { 279 utflen++; 280 } else if (c > 0x07FF) { 281 utflen += 3; 282 } else { 283 utflen += 2; 284 } 285 } 286 byte[] bytearr = new byte[utflen + 4]; bytearr[count++] = (byte) ((utflen >>> 24) & 0xFF); bytearr[count++] = (byte) ((utflen >>> 16) & 0xFF); bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF); 291 bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF); 292 for (int i = 0; i < strlen; i++) { 293 c = charr[i]; 294 if ((c >= 0x0001) && (c <= 0x007F)) { 295 bytearr[count++] = (byte) c; 296 } else if (c > 0x07FF) { 297 bytearr[count++] = (byte) (0xE0 | ((c >> 12) & 0x0F)); 298 bytearr[count++] = (byte) (0x80 | ((c >> 6) & 0x3F)); 299 bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); 300 } else { 301 bytearr[count++] = (byte) (0xC0 | ((c >> 6) & 0x1F)); 302 bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); 303 } 304 } 305 dataOut.write(bytearr); 306 307 } else { 308 dataOut.writeInt(-1); 309 } 310 } 311 312 static public String readUTF8(DataInput dataIn) throws IOException { 313 int utflen = dataIn.readInt(); if (utflen > -1) { 315 StringBuffer str = new StringBuffer (utflen); 316 byte bytearr[] = new byte[utflen]; 317 int c, char2, char3; 318 int count = 0; 319 320 dataIn.readFully(bytearr, 0, utflen); 321 322 while (count < utflen) { 323 c = bytearr[count] & 0xff; 324 switch (c >> 4) { 325 case 0: 326 case 1: 327 case 2: 328 case 3: 329 case 4: 330 case 5: 331 case 6: 332 case 7: 333 334 count++; 335 str.append((char) c); 336 break; 337 case 12: 338 case 13: 339 340 count += 2; 341 if (count > utflen) { 342 throw new UTFDataFormatException (); 343 } 344 char2 = bytearr[count - 1]; 345 if ((char2 & 0xC0) != 0x80) { 346 throw new UTFDataFormatException (); 347 } 348 str.append((char) (((c & 0x1F) << 6) | (char2 & 0x3F))); 349 break; 350 case 14: 351 352 count += 3; 353 if (count > utflen) { 354 throw new UTFDataFormatException (); 355 } 356 char2 = bytearr[count - 2]; char3 = bytearr[count - 1]; if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) { 359 throw new UTFDataFormatException (); 360 } 361 str.append((char) (((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0))); 362 break; 363 default : 364 365 throw new UTFDataFormatException (); 366 } 367 } 368 return new String (str); 370 } else { 371 return null; 372 } 373 } 374 375 public static String propertiesToString(Properties props) throws IOException { 376 String result=""; 377 if(props!=null){ 378 DataByteArrayOutputStream dataOut=new DataByteArrayOutputStream(); 379 props.store(dataOut,""); 380 result=new String (dataOut.getData(),0,dataOut.size()); 381 } 382 return result; 383 } 384 385 public static Properties stringToProperties(String str) throws IOException { 386 Properties result = new Properties (); 387 if (str != null && str.length() > 0 ) { 388 DataByteArrayInputStream dataIn = new DataByteArrayInputStream(str.getBytes()); 389 result.load(dataIn); 390 } 391 return result; 392 } 393 394 395 } 396 | Popular Tags |