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 */
017package org.apache.activemq.util;
018
019import java.io.DataInput;
020import java.io.DataInputStream;
021import java.io.DataOutput;
022import java.io.DataOutputStream;
023import java.io.IOException;
024import java.io.UTFDataFormatException;
025import java.util.ArrayList;
026import java.util.HashMap;
027import java.util.List;
028import java.util.Map;
029import java.util.Properties;
030
031import 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 */
037public 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            long utfCount = countUTFBytes(text);
300            dataOut.writeInt((int)utfCount);
301
302            byte[] buffer = new byte[(int)utfCount];
303            int len = writeUTFBytesToBuffer(text, (int) utfCount, buffer, 0);
304            dataOut.write(buffer, 0, len);
305
306            assert utfCount==len;
307        } else {
308            dataOut.writeInt(-1);
309        }
310    }
311
312    /**
313     * From: http://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/DataOutputStream.java
314     */
315    public static long countUTFBytes(String str) {
316        int utfCount = 0, length = str.length();
317        for (int i = 0; i < length; i++) {
318            int charValue = str.charAt(i);
319            if (charValue > 0 && charValue <= 127) {
320                utfCount++;
321            } else if (charValue <= 2047) {
322                utfCount += 2;
323            } else {
324                utfCount += 3;
325            }
326        }
327        return utfCount;
328    }
329
330    /**
331     * From: http://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/DataOutputStream.java
332     */
333    public static int writeUTFBytesToBuffer(String str, long count,
334                                     byte[] buffer, int offset) throws IOException {
335        int length = str.length();
336        for (int i = 0; i < length; i++) {
337            int charValue = str.charAt(i);
338            if (charValue > 0 && charValue <= 127) {
339                buffer[offset++] = (byte) charValue;
340            } else if (charValue <= 2047) {
341                buffer[offset++] = (byte) (0xc0 | (0x1f & (charValue >> 6)));
342                buffer[offset++] = (byte) (0x80 | (0x3f & charValue));
343            } else {
344                buffer[offset++] = (byte) (0xe0 | (0x0f & (charValue >> 12)));
345                buffer[offset++] = (byte) (0x80 | (0x3f & (charValue >> 6)));
346                buffer[offset++] = (byte) (0x80 | (0x3f & charValue));
347            }
348        }
349        return offset;
350    }
351
352    public static String readUTF8(DataInput dataIn) throws IOException {
353        int utflen = dataIn.readInt();
354        if (utflen > -1) {
355            byte bytearr[] = new byte[utflen];
356            char chararr[] = new char[utflen];
357            dataIn.readFully(bytearr, 0, utflen);
358            return convertUTF8WithBuf(bytearr, chararr, 0, utflen);
359        } else {
360            return null;
361        }
362    }
363
364    /**
365     * From: http://svn.apache.org/repos/asf/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/util/Util.java
366     */
367    public static String convertUTF8WithBuf(byte[] buf, char[] out, int offset,
368                                            int utfSize) throws UTFDataFormatException {
369        int count = 0, s = 0, a;
370        while (count < utfSize) {
371            if ((out[s] = (char) buf[offset + count++]) < '\u0080')
372                s++;
373            else if (((a = out[s]) & 0xe0) == 0xc0) {
374                if (count >= utfSize)
375                    throw new UTFDataFormatException();
376                int b = buf[offset + count++];
377                if ((b & 0xC0) != 0x80)
378                    throw new UTFDataFormatException();
379                out[s++] = (char) (((a & 0x1F) << 6) | (b & 0x3F));
380            } else if ((a & 0xf0) == 0xe0) {
381                if (count + 1 >= utfSize)
382                    throw new UTFDataFormatException();
383                int b = buf[offset + count++];
384                int c = buf[offset + count++];
385                if (((b & 0xC0) != 0x80) || ((c & 0xC0) != 0x80))
386                    throw new UTFDataFormatException();
387                out[s++] = (char) (((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F));
388            } else {
389                throw new UTFDataFormatException();
390            }
391        }
392        return new String(out, 0, s);
393    }
394
395    public static String propertiesToString(Properties props) throws IOException {
396        String result = "";
397        if (props != null) {
398            DataByteArrayOutputStream dataOut = new DataByteArrayOutputStream();
399            props.store(dataOut, "");
400            result = new String(dataOut.getData(), 0, dataOut.size());
401            dataOut.close();
402        }
403        return result;
404    }
405
406    public static Properties stringToProperties(String str) throws IOException {
407        Properties result = new Properties();
408        if (str != null && str.length() > 0) {
409            DataByteArrayInputStream dataIn = new DataByteArrayInputStream(str.getBytes());
410            result.load(dataIn);
411            dataIn.close();
412        }
413        return result;
414    }
415
416    public static String truncate64(String text) {
417        if (text.length() > 63) {
418            text = text.substring(0, 45) + "..." + text.substring(text.length() - 12);
419        }
420        return text;
421    }
422}