KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > util > MarshallingSupport


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.util;
19
20 import java.io.DataInput JavaDoc;
21 import java.io.DataInputStream JavaDoc;
22 import java.io.DataOutput JavaDoc;
23 import java.io.DataOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import java.io.UTFDataFormatException JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 /**
36  *
37  *
38  * The fixed version of the UTF8 encoding function. Some older JVM's UTF8 encoding function
39  * breaks when handling large strings.
40  *
41  * @version $Revision$
42  */

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 JavaDoc map, DataOutputStream JavaDoc out) throws IOException JavaDoc {
61         if( map == null ) {
62             out.writeInt(-1);
63         } else {
64             out.writeInt(map.size());
65             for (Iterator JavaDoc iter = map.keySet().iterator(); iter.hasNext();) {
66                 String JavaDoc name = (String JavaDoc) iter.next();
67                 out.writeUTF(name);
68                 Object JavaDoc value = map.get(name);
69                 marshalPrimitive(out, value);
70             }
71         }
72     }
73
74     static public Map JavaDoc unmarshalPrimitiveMap(DataInputStream JavaDoc in) throws IOException JavaDoc {
75         return unmarshalPrimitiveMap(in, Integer.MAX_VALUE);
76     }
77
78     /**
79      * @param in
80      * @return
81      * @throws IOException
82      * @throws IOException
83      */

84     public static Map JavaDoc unmarshalPrimitiveMap(DataInputStream JavaDoc in, int max_property_size) throws IOException JavaDoc {
85         int size = in.readInt();
86         if( size > max_property_size ) {
87             throw new IOException JavaDoc("Primitive map is larger than the allowed size: "+size);
88         }
89         if( size < 0 ) {
90             return null;
91         } else {
92             HashMap JavaDoc rc = new HashMap JavaDoc(size);
93             for(int i=0; i < size; i++) {
94                 String JavaDoc name = in.readUTF();
95                 rc.put(name, unmarshalPrimitive(in));
96             }
97             return rc;
98         }
99         
100     }
101
102     public static void marshalPrimitiveList(List JavaDoc list, DataOutputStream JavaDoc out) throws IOException JavaDoc {
103         out.writeInt(list.size());
104         for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
105             Object JavaDoc element = (Object JavaDoc) iter.next();
106             marshalPrimitive(out, element);
107         }
108     }
109
110     public static List JavaDoc unmarshalPrimitiveList(DataInputStream JavaDoc in) throws IOException JavaDoc {
111         int size = in.readInt();
112         List JavaDoc answer = new ArrayList JavaDoc(size);
113         while (size-- > 0) {
114             answer.add(unmarshalPrimitive(in));
115         }
116         return answer;
117     }
118
119     static public void marshalPrimitive(DataOutputStream JavaDoc out, Object JavaDoc value) throws IOException JavaDoc {
120         if( value == null ) {
121             marshalNull(out);
122         } else if( value.getClass() == Boolean JavaDoc.class ) {
123             marshalBoolean(out, ((Boolean JavaDoc)value).booleanValue());
124         } else if( value.getClass() == Byte JavaDoc.class ) {
125             marshalByte(out, ((Byte JavaDoc)value).byteValue());
126         } else if( value.getClass() == Character JavaDoc.class ) {
127             marshalChar(out, ((Character JavaDoc)value).charValue());
128         } else if( value.getClass() == Short JavaDoc.class ) {
129             marshalShort(out, ((Short JavaDoc)value).shortValue());
130         } else if( value.getClass() == Integer JavaDoc.class ) {
131             marshalInt(out, ((Integer JavaDoc)value).intValue());
132         } else if( value.getClass() == Long JavaDoc.class ) {
133             marshalLong(out, ((Long JavaDoc)value).longValue());
134         } else if( value.getClass() == Float JavaDoc.class ) {
135             marshalFloat(out, ((Float JavaDoc)value).floatValue());
136         } else if( value.getClass() == Double JavaDoc.class ) {
137             marshalDouble(out, ((Double JavaDoc)value).doubleValue());
138         } else if( value.getClass() == byte[].class ) {
139             marshalByteArray(out, ((byte[])value));
140         } else if( value.getClass() == String JavaDoc.class ) {
141             marshalString(out, (String JavaDoc)value);
142         } else if( value instanceof Map JavaDoc) {
143             out.writeByte(MAP_TYPE);
144             marshalPrimitiveMap((Map JavaDoc) value, out);
145         } else if( value instanceof List JavaDoc) {
146             out.writeByte(LIST_TYPE);
147             marshalPrimitiveList((List JavaDoc) value, out);
148         } else {
149             throw new IOException JavaDoc("Object is not a primitive: "+value);
150         }
151     }
152
153
154     static public Object JavaDoc unmarshalPrimitive(DataInputStream JavaDoc in) throws IOException JavaDoc {
155         Object JavaDoc value=null;
156         switch( in.readByte() ) {
157         case BYTE_TYPE:
158             value = new Byte JavaDoc(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 JavaDoc(in.readChar());
165             break;
166         case SHORT_TYPE:
167             value = new Short JavaDoc(in.readShort());
168             break;
169         case INTEGER_TYPE:
170             value = new Integer JavaDoc(in.readInt());
171             break;
172         case LONG_TYPE:
173             value = new Long JavaDoc(in.readLong());
174             break;
175         case FLOAT_TYPE:
176             value = new Float JavaDoc(in.readFloat());
177             break;
178         case DOUBLE_TYPE:
179             value = new Double JavaDoc(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 JavaDoc out) throws IOException JavaDoc {
202         out.writeByte(NULL);
203     }
204
205     public static void marshalBoolean(DataOutputStream JavaDoc out, boolean value) throws IOException JavaDoc {
206         out.writeByte(BOOLEAN_TYPE);
207         out.writeBoolean(value);
208     }
209
210     public static void marshalByte(DataOutputStream JavaDoc out, byte value) throws IOException JavaDoc {
211         out.writeByte(BYTE_TYPE);
212         out.writeByte(value);
213     }
214
215     public static void marshalChar(DataOutputStream JavaDoc out, char value) throws IOException JavaDoc {
216         out.writeByte(CHAR_TYPE);
217         out.writeChar(value);
218     }
219
220     public static void marshalShort(DataOutputStream JavaDoc out, short value) throws IOException JavaDoc {
221         out.writeByte(SHORT_TYPE);
222         out.writeShort(value);
223     }
224
225     public static void marshalInt(DataOutputStream JavaDoc out, int value) throws IOException JavaDoc {
226         out.writeByte(INTEGER_TYPE);
227         out.writeInt(value);
228     }
229
230     public static void marshalLong(DataOutputStream JavaDoc out, long value) throws IOException JavaDoc {
231         out.writeByte(LONG_TYPE);
232         out.writeLong(value);
233     }
234
235     public static void marshalFloat(DataOutputStream JavaDoc out, float value) throws IOException JavaDoc {
236         out.writeByte(FLOAT_TYPE);
237         out.writeFloat(value);
238     }
239
240     public static void marshalDouble(DataOutputStream JavaDoc out, double value) throws IOException JavaDoc {
241         out.writeByte(DOUBLE_TYPE);
242         out.writeDouble(value);
243     }
244
245     public static void marshalByteArray(DataOutputStream JavaDoc out, byte[] value) throws IOException JavaDoc {
246         marshalByteArray(out, value, 0, value.length);
247     }
248
249     public static void marshalByteArray(DataOutputStream JavaDoc out, byte[] value, int offset, int length) throws IOException JavaDoc {
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 JavaDoc out, String JavaDoc s) throws IOException JavaDoc {
257         // If it's too big, out.writeUTF may not able able to write it out.
258
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 JavaDoc dataOut, String JavaDoc text) throws IOException JavaDoc {
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             //TODO diff: Sun code - removed
287
byte[] bytearr = new byte[utflen + 4]; //TODO diff: Sun code
288
bytearr[count++] = (byte) ((utflen >>> 24) & 0xFF); //TODO diff: Sun code
289
bytearr[count++] = (byte) ((utflen >>> 16) & 0xFF); //TODO diff: Sun code
290
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 JavaDoc readUTF8(DataInput JavaDoc dataIn) throws IOException JavaDoc {
313         int utflen = dataIn.readInt(); //TODO diff: Sun code
314
if (utflen > -1) {
315             StringBuffer JavaDoc str = new StringBuffer JavaDoc(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                         /* 0xxxxxxx */
334                         count++;
335                         str.append((char) c);
336                         break;
337                     case 12:
338                     case 13:
339                         /* 110x xxxx 10xx xxxx */
340                         count += 2;
341                         if (count > utflen) {
342                             throw new UTFDataFormatException JavaDoc();
343                         }
344                         char2 = bytearr[count - 1];
345                         if ((char2 & 0xC0) != 0x80) {
346                             throw new UTFDataFormatException JavaDoc();
347                         }
348                         str.append((char) (((c & 0x1F) << 6) | (char2 & 0x3F)));
349                         break;
350                     case 14:
351                         /* 1110 xxxx 10xx xxxx 10xx xxxx */
352                         count += 3;
353                         if (count > utflen) {
354                             throw new UTFDataFormatException JavaDoc();
355                         }
356                         char2 = bytearr[count - 2]; //TODO diff: Sun code
357
char3 = bytearr[count - 1]; //TODO diff: Sun code
358
if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) {
359                             throw new UTFDataFormatException JavaDoc();
360                         }
361                         str.append((char) (((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)));
362                         break;
363                     default :
364                         /* 10xx xxxx, 1111 xxxx */
365                         throw new UTFDataFormatException JavaDoc();
366                 }
367             }
368             // The number of chars produced may be less than utflen
369
return new String JavaDoc(str);
370         } else {
371             return null;
372         }
373     }
374     
375     public static String JavaDoc propertiesToString(Properties JavaDoc props) throws IOException JavaDoc{
376         String JavaDoc result="";
377         if(props!=null){
378             DataByteArrayOutputStream dataOut=new DataByteArrayOutputStream();
379             props.store(dataOut,"");
380             result=new String JavaDoc(dataOut.getData(),0,dataOut.size());
381         }
382         return result;
383     }
384     
385     public static Properties JavaDoc stringToProperties(String JavaDoc str) throws IOException JavaDoc {
386         Properties JavaDoc result = new Properties JavaDoc();
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