001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.util;
018    
019    import java.io.DataInput;
020    import java.io.DataInputStream;
021    import java.io.DataOutput;
022    import java.io.DataOutputStream;
023    import java.io.IOException;
024    import java.io.UTFDataFormatException;
025    import java.util.ArrayList;
026    import java.util.HashMap;
027    import java.util.Iterator;
028    import java.util.List;
029    import java.util.Map;
030    import java.util.Properties;
031    
032    /**
033     * The fixed version of the UTF8 encoding function. Some older JVM's UTF8
034     * encoding function breaks when handling large strings.
035     * 
036     * 
037     */
038    public final class MarshallingSupport {
039    
040        public static final byte NULL = 0;
041        public static final byte BOOLEAN_TYPE = 1;
042        public static final byte BYTE_TYPE = 2;
043        public static final byte CHAR_TYPE = 3;
044        public static final byte SHORT_TYPE = 4;
045        public static final byte INTEGER_TYPE = 5;
046        public static final byte LONG_TYPE = 6;
047        public static final byte DOUBLE_TYPE = 7;
048        public static final byte FLOAT_TYPE = 8;
049        public static final byte STRING_TYPE = 9;
050        public static final byte BYTE_ARRAY_TYPE = 10;
051        public static final byte MAP_TYPE = 11;
052        public static final byte LIST_TYPE = 12;
053        public static final byte BIG_STRING_TYPE = 13;
054    
055        private MarshallingSupport() {
056        }
057        
058        public static void marshalPrimitiveMap(Map map, DataOutputStream out) throws IOException {
059            if (map == null) {
060                out.writeInt(-1);
061            } else {
062                out.writeInt(map.size());
063                for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
064                    String name = (String)iter.next();
065                    out.writeUTF(name);
066                    Object value = map.get(name);
067                    marshalPrimitive(out, value);
068                }
069            }
070        }
071    
072        public static Map<String, Object> unmarshalPrimitiveMap(DataInputStream in) throws IOException {
073            return unmarshalPrimitiveMap(in, Integer.MAX_VALUE);
074        }
075    
076        /**
077         * @param in
078         * @return
079         * @throws IOException
080         * @throws IOException
081         */
082        public static Map<String, Object> unmarshalPrimitiveMap(DataInputStream in, int maxPropertySize) throws IOException {
083            int size = in.readInt();
084            if (size > maxPropertySize) {
085                throw new IOException("Primitive map is larger than the allowed size: " + size);
086            }
087            if (size < 0) {
088                return null;
089            } else {
090                Map<String, Object> rc = new HashMap<String, Object>(size);
091                for (int i = 0; i < size; i++) {
092                    String name = in.readUTF();
093                    rc.put(name, unmarshalPrimitive(in));
094                }
095                return rc;
096            }
097    
098        }
099    
100        public static void marshalPrimitiveList(List list, DataOutputStream out) throws IOException {
101            out.writeInt(list.size());
102            for (Iterator iter = list.iterator(); iter.hasNext();) {
103                Object element = (Object)iter.next();
104                marshalPrimitive(out, element);
105            }
106        }
107    
108        public static List<Object> unmarshalPrimitiveList(DataInputStream in) throws IOException {
109            int size = in.readInt();
110            List<Object> answer = new ArrayList<Object>(size);
111            while (size-- > 0) {
112                answer.add(unmarshalPrimitive(in));
113            }
114            return answer;
115        }
116    
117        public static void marshalPrimitive(DataOutputStream out, Object value) throws IOException {
118            if (value == null) {
119                marshalNull(out);
120            } else if (value.getClass() == Boolean.class) {
121                marshalBoolean(out, ((Boolean)value).booleanValue());
122            } else if (value.getClass() == Byte.class) {
123                marshalByte(out, ((Byte)value).byteValue());
124            } else if (value.getClass() == Character.class) {
125                marshalChar(out, ((Character)value).charValue());
126            } else if (value.getClass() == Short.class) {
127                marshalShort(out, ((Short)value).shortValue());
128            } else if (value.getClass() == Integer.class) {
129                marshalInt(out, ((Integer)value).intValue());
130            } else if (value.getClass() == Long.class) {
131                marshalLong(out, ((Long)value).longValue());
132            } else if (value.getClass() == Float.class) {
133                marshalFloat(out, ((Float)value).floatValue());
134            } else if (value.getClass() == Double.class) {
135                marshalDouble(out, ((Double)value).doubleValue());
136            } else if (value.getClass() == byte[].class) {
137                marshalByteArray(out, (byte[])value);
138            } else if (value.getClass() == String.class) {
139                marshalString(out, (String)value);
140            } else if (value instanceof Map) {
141                out.writeByte(MAP_TYPE);
142                marshalPrimitiveMap((Map)value, out);
143            } else if (value instanceof List) {
144                out.writeByte(LIST_TYPE);
145                marshalPrimitiveList((List)value, out);
146            } else {
147                throw new IOException("Object is not a primitive: " + value);
148            }
149        }
150    
151        public static Object unmarshalPrimitive(DataInputStream in) throws IOException {
152            Object value = null;
153            byte type = in.readByte();
154            switch (type) {
155            case BYTE_TYPE:
156                value = Byte.valueOf(in.readByte());
157                break;
158            case BOOLEAN_TYPE:
159                value = in.readBoolean() ? Boolean.TRUE : Boolean.FALSE;
160                break;
161            case CHAR_TYPE:
162                value = Character.valueOf(in.readChar());
163                break;
164            case SHORT_TYPE:
165                value = Short.valueOf(in.readShort());
166                break;
167            case INTEGER_TYPE:
168                value = Integer.valueOf(in.readInt());
169                break;
170            case LONG_TYPE:
171                value = Long.valueOf(in.readLong());
172                break;
173            case FLOAT_TYPE:
174                value = new Float(in.readFloat());
175                break;
176            case DOUBLE_TYPE:
177                value = new Double(in.readDouble());
178                break;
179            case BYTE_ARRAY_TYPE:
180                value = new byte[in.readInt()];
181                in.readFully((byte[])value);
182                break;
183            case STRING_TYPE:
184                value = in.readUTF();
185                break;
186            case BIG_STRING_TYPE:
187                value = readUTF8(in);
188                break;
189            case MAP_TYPE:
190                value = unmarshalPrimitiveMap(in);
191                break;
192            case LIST_TYPE:
193                value = unmarshalPrimitiveList(in);
194                break;
195            case NULL:
196                value = null;
197                break;
198            default:
199                throw new IOException("Unknown primitive type: " + type);
200            }
201            return value;
202        }
203    
204        public static void marshalNull(DataOutputStream out) throws IOException {
205            out.writeByte(NULL);
206        }
207    
208        public static void marshalBoolean(DataOutputStream out, boolean value) throws IOException {
209            out.writeByte(BOOLEAN_TYPE);
210            out.writeBoolean(value);
211        }
212    
213        public static void marshalByte(DataOutputStream out, byte value) throws IOException {
214            out.writeByte(BYTE_TYPE);
215            out.writeByte(value);
216        }
217    
218        public static void marshalChar(DataOutputStream out, char value) throws IOException {
219            out.writeByte(CHAR_TYPE);
220            out.writeChar(value);
221        }
222    
223        public static void marshalShort(DataOutputStream out, short value) throws IOException {
224            out.writeByte(SHORT_TYPE);
225            out.writeShort(value);
226        }
227    
228        public static void marshalInt(DataOutputStream out, int value) throws IOException {
229            out.writeByte(INTEGER_TYPE);
230            out.writeInt(value);
231        }
232    
233        public static void marshalLong(DataOutputStream out, long value) throws IOException {
234            out.writeByte(LONG_TYPE);
235            out.writeLong(value);
236        }
237    
238        public static void marshalFloat(DataOutputStream out, float value) throws IOException {
239            out.writeByte(FLOAT_TYPE);
240            out.writeFloat(value);
241        }
242    
243        public static void marshalDouble(DataOutputStream out, double value) throws IOException {
244            out.writeByte(DOUBLE_TYPE);
245            out.writeDouble(value);
246        }
247    
248        public static void marshalByteArray(DataOutputStream out, byte[] value) throws IOException {
249            marshalByteArray(out, value, 0, value.length);
250        }
251    
252        public static void marshalByteArray(DataOutputStream out, byte[] value, int offset, int length) throws IOException {
253            out.writeByte(BYTE_ARRAY_TYPE);
254            out.writeInt(length);
255            out.write(value, offset, length);
256        }
257    
258        public static void marshalString(DataOutputStream out, String s) throws IOException {
259            // If it's too big, out.writeUTF may not able able to write it out.
260            if (s.length() < Short.MAX_VALUE / 4) {
261                out.writeByte(STRING_TYPE);
262                out.writeUTF(s);
263            } else {
264                out.writeByte(BIG_STRING_TYPE);
265                writeUTF8(out, s);
266            }
267        }
268    
269        public static void writeUTF8(DataOutput dataOut, String text) throws IOException {
270            if (text != null) {
271                int strlen = text.length();
272                int utflen = 0;
273                char[] charr = new char[strlen];
274                int c = 0;
275                int count = 0;
276    
277                text.getChars(0, strlen, charr, 0);
278    
279                for (int i = 0; i < strlen; i++) {
280                    c = charr[i];
281                    if ((c >= 0x0001) && (c <= 0x007F)) {
282                        utflen++;
283                    } else if (c > 0x07FF) {
284                        utflen += 3;
285                    } else {
286                        utflen += 2;
287                    }
288                }
289                // TODO diff: Sun code - removed
290                byte[] bytearr = new byte[utflen + 4]; // TODO diff: Sun code
291                bytearr[count++] = (byte)((utflen >>> 24) & 0xFF); // TODO diff:
292                // Sun code
293                bytearr[count++] = (byte)((utflen >>> 16) & 0xFF); // TODO diff:
294                // Sun code
295                bytearr[count++] = (byte)((utflen >>> 8) & 0xFF);
296                bytearr[count++] = (byte)((utflen >>> 0) & 0xFF);
297                for (int i = 0; i < strlen; i++) {
298                    c = charr[i];
299                    if ((c >= 0x0001) && (c <= 0x007F)) {
300                        bytearr[count++] = (byte)c;
301                    } else if (c > 0x07FF) {
302                        bytearr[count++] = (byte)(0xE0 | ((c >> 12) & 0x0F));
303                        bytearr[count++] = (byte)(0x80 | ((c >> 6) & 0x3F));
304                        bytearr[count++] = (byte)(0x80 | ((c >> 0) & 0x3F));
305                    } else {
306                        bytearr[count++] = (byte)(0xC0 | ((c >> 6) & 0x1F));
307                        bytearr[count++] = (byte)(0x80 | ((c >> 0) & 0x3F));
308                    }
309                }
310                dataOut.write(bytearr);
311    
312            } else {
313                dataOut.writeInt(-1);
314            }
315        }
316    
317        public static String readUTF8(DataInput dataIn) throws IOException {
318            int utflen = dataIn.readInt(); // TODO diff: Sun code
319            if (utflen > -1) {
320                StringBuffer str = new StringBuffer(utflen);
321                byte bytearr[] = new byte[utflen];
322                int c;
323                int char2;
324                int char3;
325                int count = 0;
326    
327                dataIn.readFully(bytearr, 0, utflen);
328    
329                while (count < utflen) {
330                    c = bytearr[count] & 0xff;
331                    switch (c >> 4) {
332                    case 0:
333                    case 1:
334                    case 2:
335                    case 3:
336                    case 4:
337                    case 5:
338                    case 6:
339                    case 7:
340                        /* 0xxxxxxx */
341                        count++;
342                        str.append((char)c);
343                        break;
344                    case 12:
345                    case 13:
346                        /* 110x xxxx 10xx xxxx */
347                        count += 2;
348                        if (count > utflen) {
349                            throw new UTFDataFormatException();
350                        }
351                        char2 = bytearr[count - 1];
352                        if ((char2 & 0xC0) != 0x80) {
353                            throw new UTFDataFormatException();
354                        }
355                        str.append((char)(((c & 0x1F) << 6) | (char2 & 0x3F)));
356                        break;
357                    case 14:
358                        /* 1110 xxxx 10xx xxxx 10xx xxxx */
359                        count += 3;
360                        if (count > utflen) {
361                            throw new UTFDataFormatException();
362                        }
363                        char2 = bytearr[count - 2]; // TODO diff: Sun code
364                        char3 = bytearr[count - 1]; // TODO diff: Sun code
365                        if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) {
366                            throw new UTFDataFormatException();
367                        }
368                        str.append((char)(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)));
369                        break;
370                    default:
371                        /* 10xx xxxx, 1111 xxxx */
372                        throw new UTFDataFormatException();
373                    }
374                }
375                // The number of chars produced may be less than utflen
376                return new String(str);
377            } else {
378                return null;
379            }
380        }
381    
382        public static String propertiesToString(Properties props) throws IOException {
383            String result = "";
384            if (props != null) {
385                DataByteArrayOutputStream dataOut = new DataByteArrayOutputStream();
386                props.store(dataOut, "");
387                result = new String(dataOut.getData(), 0, dataOut.size());
388                dataOut.close();
389            }
390            return result;
391        }
392    
393        public static Properties stringToProperties(String str) throws IOException {
394            Properties result = new Properties();
395            if (str != null && str.length() > 0) {
396                DataByteArrayInputStream dataIn = new DataByteArrayInputStream(str.getBytes());
397                result.load(dataIn);
398                dataIn.close();
399            }
400            return result;
401        }
402    
403        public static String truncate64(String text) {
404            if (text.length() > 63) {
405                text = text.substring(0, 45) + "..." + text.substring(text.length() - 12);
406            }
407            return text;
408        }
409    }