public static void writeObject(DataOutput out, Object obj) throws IOException {
        if (obj == null) {
            out.writeByte(0);
        } else if (obj instanceof Long) {
            out.writeByte(1);
            out.writeLong((Long) obj);
        } else if (obj instanceof Integer) {
            out.writeByte(2);
            out.writeInt((Integer) obj);
        } else if (obj instanceof String) {
            out.writeByte(3);
            out.writeUTF((String) obj);
        } else if (obj instanceof Double) {
            out.writeByte(4);
            out.writeDouble((Double) obj);
        } else if (obj instanceof Float) {
            out.writeByte(5);
            out.writeFloat((Float) obj);
        } else if (obj instanceof Boolean) {
            out.writeByte(6);
            out.writeBoolean((Boolean) obj);
        } else if (obj instanceof Date) {
            out.writeByte(8);
            out.writeLong(((Date) obj).getTime());
        } else if(obj instanceof byte[]){
            out.writeByte(9);
            // write length
            out.writeInt(((byte[]) obj).length);
            // write byte array
            out.write((byte[]) obj);
        } else {
            out.writeByte(10);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(obj);
            oos.close();
            byte[] buf = bos.toByteArray();
            out.writeInt(buf.length);
            out.write(buf);
        }
    }
Reads a Map of Serializable key and value pairs. This method will return
<tt>null</tt> if the Map written to the stream was <tt>null</tt>.

@param in the input stream.
@param map a Map of Serializable key and value pairs.
@param loader class loader to use to build elements inside of the serialized collection.
@throws IOException if an error occurs.
@return the number of elements added to the collection.
xmppserver/src/main/java/org/jivesoftware/util/cache/DefaultExternalizableUtil.java