@SuppressWarnings("unchecked") private static void serialize(ByteArrayOutputStream stream, Object obj, String charset) { if (obj == null) { writeNull(stream); } else if (obj instanceof Boolean) { writeBoolean(stream, ((Boolean) obj).booleanValue() ? __1 : __0); } else if ((obj instanceof Byte) || (obj instanceof Short) || (obj instanceof Integer)) { writeInteger(stream, getAsciiBytes(obj)); } else if (obj instanceof Long) { writeDouble(stream, getAsciiBytes(obj)); } else if (obj instanceof Float) { Float f = (Float) obj; obj = f.isNaN() ? __NAN : (!f.isInfinite() ? obj : (f.floatValue() > 0 ? __INF : __NINF)); writeDouble(stream, getAsciiBytes(obj)); } else if (obj instanceof Double) { Double d = (Double) obj; obj = d.isNaN() ? __NAN : (!d.isInfinite() ? obj : (d.doubleValue() > 0 ? __INF : __NINF)); writeDouble(stream, getAsciiBytes(obj)); } else if ((obj instanceof Character) || (obj instanceof String) || (obj instanceof StringBuffer)) { writeString(stream, getBytes(obj, charset)); } else if (obj instanceof Map) { writeMap(stream, (Map) obj, charset); } else if (obj instanceof List) { writeList(stream, (List) obj, charset); } else { System.out.println("Serializer.serialize: The DataType is invalid"); } }
@SuppressWarnings("unchecked") private static void writeMap(ByteArrayOutputStream stream, Map h, String charset) { int len = h.size(); byte[] hlen = getAsciiBytes(new Integer(len)); stream.write(__a); stream.write(__Colon); stream.write(hlen, 0, hlen.length); stream.write(__Colon); stream.write(__LeftB); for (Iterator keys = h.keySet().iterator(); keys.hasNext(); ) { Object key = keys.next(); if ((key instanceof Byte) || (key instanceof Short) || (key instanceof Integer)) { writeInteger(stream, getAsciiBytes(key)); } else if (key instanceof Boolean) { writeInteger(stream, new byte[] {((Boolean) key).booleanValue() ? __1 : __0}); } else { writeString(stream, getBytes(key, charset)); } serialize(stream, h.get(key), charset); } stream.write(__RightB); }
@SuppressWarnings("unchecked") private static void writeList(ByteArrayOutputStream stream, List a, String charset) { int len = a.size(); byte[] alen = getAsciiBytes(new Integer(len)); stream.write(__a); stream.write(__Colon); stream.write(alen, 0, alen.length); stream.write(__Colon); stream.write(__LeftB); for (int i = 0; i < len; i++) { writeInteger(stream, getAsciiBytes(new Integer(i))); serialize(stream, a.get(i), charset); } stream.write(__RightB); }