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