Search in sources :

Example 1 with Externalizable

use of com.codename1.io.Externalizable in project CodenameOne by codenameone.

the class Util method readObject.

/**
 * <p>Reads an object from the stream, notice that this is the inverse of the
 * {@link #writeObject(java.lang.Object, java.io.DataOutputStream)}.</p>
 *
 * <p>
 * The sample below demonstrates the usage and registration of the {@link com.codename1.io.Externalizable} interface:
 * </p>
 * <script src="https://gist.github.com/codenameone/858d8634e3cf1a82a1eb.js"></script>
 *
 * @param input the source input stream
 * @throws IOException thrown by the stream
 */
public static Object readObject(DataInputStream input) throws IOException {
    try {
        if (!input.readBoolean()) {
            return null;
        }
        String type = input.readUTF();
        if ("int".equals(type)) {
            return new Integer(input.readInt());
        }
        if ("byte".equals(type)) {
            return new Byte(input.readByte());
        }
        if ("short".equals(type)) {
            return new Short(input.readShort());
        }
        if ("long".equals(type)) {
            return new Long(input.readLong());
        }
        if ("float".equals(type)) {
            return new Float(input.readFloat());
        }
        if ("double".equals(type)) {
            return new Double(input.readDouble());
        }
        if ("bool".equals(type)) {
            return new Boolean(input.readBoolean());
        }
        if ("String".equals(type)) {
            return input.readUTF();
        }
        if ("Date".equals(type)) {
            return new Date(input.readLong());
        }
        if ("ObjectArray".equals(type)) {
            Object[] v = new Object[input.readInt()];
            int vlen = v.length;
            for (int iter = 0; iter < vlen; iter++) {
                v[iter] = readObject(input);
            }
            return v;
        }
        if ("ByteArray".equals(type)) {
            byte[] v = new byte[input.readInt()];
            input.readFully(v);
            return v;
        }
        if ("LongArray".equals(type)) {
            long[] v = new long[input.readInt()];
            int vlen = v.length;
            for (int iter = 0; iter < vlen; iter++) {
                v[iter] = input.readLong();
            }
            return v;
        }
        if ("ShortArray".equals(type)) {
            short[] v = new short[input.readInt()];
            int vlen = v.length;
            for (int iter = 0; iter < vlen; iter++) {
                v[iter] = input.readShort();
            }
            return v;
        }
        if ("DoubleArray".equals(type)) {
            double[] v = new double[input.readInt()];
            int vlen = v.length;
            for (int iter = 0; iter < vlen; iter++) {
                v[iter] = input.readDouble();
            }
            return v;
        }
        if ("FloatArray".equals(type)) {
            float[] v = new float[input.readInt()];
            int vlen = v.length;
            for (int iter = 0; iter < vlen; iter++) {
                v[iter] = input.readFloat();
            }
            return v;
        }
        if ("IntArray".equals(type)) {
            int[] v = new int[input.readInt()];
            int vlen = v.length;
            for (int iter = 0; iter < vlen; iter++) {
                v[iter] = input.readInt();
            }
            return v;
        }
        if ("java.util.Vector".equals(type)) {
            Vector v = new Vector();
            int size = input.readInt();
            for (int iter = 0; iter < size; iter++) {
                v.addElement(readObject(input));
            }
            return v;
        }
        if ("java.util.Hashtable".equals(type)) {
            Hashtable v = new Hashtable();
            int size = input.readInt();
            for (int iter = 0; iter < size; iter++) {
                v.put(readObject(input), readObject(input));
            }
            return v;
        }
        if ("java.util.Collection".equals(type)) {
            Collection v = new ArrayList();
            int size = input.readInt();
            for (int iter = 0; iter < size; iter++) {
                v.add(readObject(input));
            }
            return v;
        }
        if ("java.util.Map".equals(type)) {
            Map v = new HashMap();
            int size = input.readInt();
            for (int iter = 0; iter < size; iter++) {
                v.put(readObject(input), readObject(input));
            }
            return v;
        }
        if ("EncodedImage".equals(type)) {
            int width = input.readInt();
            int height = input.readInt();
            boolean op = input.readBoolean();
            byte[] data = new byte[input.readInt()];
            input.readFully(data);
            return EncodedImage.create(data, width, height, op);
        }
        Class cls = (Class) externalizables.get(type);
        if (cls != null) {
            Object o = cls.newInstance();
            if (o instanceof Externalizable) {
                Externalizable ex = (Externalizable) o;
                ex.internalize(input.readInt(), input);
                return ex;
            } else {
                PropertyBusinessObject pb = (PropertyBusinessObject) o;
                pb.getPropertyIndex().asExternalizable().internalize(input.readInt(), input);
                return pb;
            }
        }
        throw new IOException("Object type not supported: " + type);
    } catch (InstantiationException ex1) {
        Log.e(ex1);
        throw new IOException(ex1.getClass().getName() + ": " + ex1.getMessage());
    } catch (IllegalAccessException ex1) {
        Log.e(ex1);
        throw new IOException(ex1.getClass().getName() + ": " + ex1.getMessage());
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Vector(java.util.Vector) PropertyBusinessObject(com.codename1.properties.PropertyBusinessObject) Hashtable(java.util.Hashtable) IOException(java.io.IOException) Date(java.util.Date) Collection(java.util.Collection) PropertyBusinessObject(com.codename1.properties.PropertyBusinessObject) Externalizable(com.codename1.io.Externalizable) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Externalizable

use of com.codename1.io.Externalizable in project CodenameOne by codenameone.

the class Util method writeObject.

/**
 * <p>Writes an object to the given output stream, notice that it should be externalizable or one of
 * the supported types.</p>
 *
 * <p>
 * The sample below demonstrates the usage and registration of the {@link com.codename1.io.Externalizable} interface:
 * </p>
 * <script src="https://gist.github.com/codenameone/858d8634e3cf1a82a1eb.js"></script>
 *
 * @param o the object to write which can be null
 * @param out the destination output stream
 * @throws IOException thrown by the stream
 */
public static void writeObject(Object o, DataOutputStream out) throws IOException {
    if (o == null) {
        out.writeBoolean(false);
        return;
    }
    out.writeBoolean(true);
    if (o instanceof Externalizable) {
        Externalizable e = (Externalizable) o;
        out.writeUTF(e.getObjectId());
        out.writeInt(e.getVersion());
        e.externalize(out);
        return;
    }
    if (o instanceof PropertyBusinessObject) {
        Externalizable e = ((PropertyBusinessObject) o).getPropertyIndex().asExternalizable();
        out.writeUTF(e.getObjectId());
        out.writeInt(e.getVersion());
        e.externalize(out);
        return;
    }
    if (o instanceof Vector) {
        Vector v = (Vector) o;
        out.writeUTF("java.util.Vector");
        int size = v.size();
        out.writeInt(size);
        for (int iter = 0; iter < size; iter++) {
            writeObject(v.elementAt(iter), out);
        }
        return;
    }
    if (o instanceof Collection) {
        Collection v = (Collection) o;
        out.writeUTF("java.util.Collection");
        int size = v.size();
        out.writeInt(size);
        for (Object cur : v) {
            writeObject(cur, out);
        }
        return;
    }
    if (o instanceof Hashtable) {
        Hashtable v = (Hashtable) o;
        out.writeUTF("java.util.Hashtable");
        out.writeInt(v.size());
        Enumeration k = v.keys();
        while (k.hasMoreElements()) {
            Object key = k.nextElement();
            writeObject(key, out);
            writeObject(v.get(key), out);
        }
        return;
    }
    if (o instanceof Map) {
        Map v = (Map) o;
        out.writeUTF("java.util.Map");
        out.writeInt(v.size());
        for (Object key : v.keySet()) {
            writeObject(key, out);
            writeObject(v.get(key), out);
        }
        return;
    }
    if (o instanceof String) {
        String v = (String) o;
        out.writeUTF("String");
        out.writeUTF(v);
        return;
    }
    if (o instanceof Date) {
        Date v = (Date) o;
        out.writeUTF("Date");
        out.writeLong(v.getTime());
        return;
    }
    if (o instanceof Integer) {
        Integer v = (Integer) o;
        out.writeUTF("int");
        out.writeInt(v.intValue());
        return;
    }
    if (o instanceof Long) {
        Long v = (Long) o;
        out.writeUTF("long");
        out.writeLong(v.longValue());
        return;
    }
    if (o instanceof Byte) {
        Byte v = (Byte) o;
        out.writeUTF("byte");
        out.writeByte(v.byteValue());
        return;
    }
    if (o instanceof Short) {
        Short v = (Short) o;
        out.writeUTF("short");
        out.writeShort(v.shortValue());
        return;
    }
    if (o instanceof Float) {
        Float v = (Float) o;
        out.writeUTF("float");
        out.writeFloat(v.floatValue());
        return;
    }
    if (o instanceof Double) {
        Double v = (Double) o;
        out.writeUTF("double");
        out.writeDouble(v.doubleValue());
        return;
    }
    if (o instanceof Boolean) {
        Boolean v = (Boolean) o;
        out.writeUTF("bool");
        out.writeBoolean(v.booleanValue());
        return;
    }
    if (o instanceof EncodedImage) {
        out.writeUTF("EncodedImage");
        EncodedImage e = (EncodedImage) o;
        out.writeInt(e.getWidth());
        out.writeInt(e.getHeight());
        out.writeBoolean(e.isOpaque());
        byte[] b = e.getImageData();
        out.writeInt(b.length);
        out.write(b);
        return;
    }
    if (instanceofObjArray(o)) {
        Object[] v = (Object[]) o;
        out.writeUTF("ObjectArray");
        int size = v.length;
        out.writeInt(size);
        for (int iter = 0; iter < size; iter++) {
            writeObject(v[iter], out);
        }
        return;
    }
    if (instanceofByteArray(o)) {
        byte[] v = (byte[]) o;
        out.writeUTF("ByteArray");
        int size = v.length;
        out.writeInt(size);
        out.write(v);
        return;
    }
    if (instanceofShortArray(o)) {
        short[] v = (short[]) o;
        out.writeUTF("ShortArray");
        int size = v.length;
        out.writeInt(size);
        for (int iter = 0; iter < size; iter++) {
            out.writeShort(v[iter]);
        }
        return;
    }
    if (instanceofDoubleArray(o)) {
        double[] v = (double[]) o;
        out.writeUTF("DoubleArray");
        int size = v.length;
        out.writeInt(size);
        for (int iter = 0; iter < size; iter++) {
            out.writeDouble(v[iter]);
        }
        return;
    }
    if (instanceofFloatArray(o)) {
        float[] v = (float[]) o;
        out.writeUTF("FloatArray");
        int size = v.length;
        out.writeInt(size);
        for (int iter = 0; iter < size; iter++) {
            out.writeFloat(v[iter]);
        }
        return;
    }
    if (instanceofIntArray(o)) {
        int[] v = (int[]) o;
        out.writeUTF("IntArray");
        int size = v.length;
        out.writeInt(size);
        for (int iter = 0; iter < size; iter++) {
            out.writeInt(v[iter]);
        }
        return;
    }
    if (instanceofLongArray(o)) {
        long[] v = (long[]) o;
        out.writeUTF("LongArray");
        int size = v.length;
        out.writeInt(size);
        for (int iter = 0; iter < size; iter++) {
            out.writeLong(v[iter]);
        }
        return;
    }
    throw new IOException("Object type not supported: " + o.getClass().getName() + " value: " + o);
}
Also used : Vector(java.util.Vector) Enumeration(java.util.Enumeration) PropertyBusinessObject(com.codename1.properties.PropertyBusinessObject) Hashtable(java.util.Hashtable) IOException(java.io.IOException) EncodedImage(com.codename1.ui.EncodedImage) Date(java.util.Date) Collection(java.util.Collection) Externalizable(com.codename1.io.Externalizable) PropertyBusinessObject(com.codename1.properties.PropertyBusinessObject) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Externalizable (com.codename1.io.Externalizable)2 PropertyBusinessObject (com.codename1.properties.PropertyBusinessObject)2 IOException (java.io.IOException)2 Collection (java.util.Collection)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 Hashtable (java.util.Hashtable)2 Map (java.util.Map)2 Vector (java.util.Vector)2 EncodedImage (com.codename1.ui.EncodedImage)1 ArrayList (java.util.ArrayList)1 Enumeration (java.util.Enumeration)1