Serialization via J2ME or BlackBerry APIs

后端 未结 4 2077
时光说笑
时光说笑 2020-12-21 07:29

Is it possible to serialize an object into a string or a byte array using either the J2ME or BlackBerry APIs?

Thanks.

4条回答
  •  我在风中等你
    2020-12-21 08:27

    The way I handle the object serialization case is by implementing my own infrastructure for handling everything. You don't have reflection in this API, but you do have "Class.forName()" which is better than nothing. So here's what I do...

    First, this is the interface that I have every serializable object implement:

    public interface Serializable {
        void serialize(DataOutput output) throws IOException;
        void deserialize(DataInput input) throws IOException;
    }
    

    The serialize() method writes the object's fields to the DataOutput instance, while the deserialize() method sets the object's fields from the DataInput instance. (these are both plain top-level interfaces used by the data-oriented I/O streams, which allows me to have more flexibility) Also, any class implementing this interface needs to have a default no-arguments constructor. Of course if you want your serialized class to be robust against change, you may want to choose your underlying data formats accordingly. (for example, I implemented a serializable hashtable as an underlying container to handle these cases)

    Now, to actually serialize a class implementing this interface, I have a method that looks something like this:

    public static byte[] serializeClass(Serializable input) {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        DataOutputStream output = new DataOutputStream(buffer);
        try {
            output.writeUTF(input.getClass().getName());
            input.serialize(output);
        } catch (IOException ex) {
            // do nothing
        }
        return buffer.toByteArray();
    }
    

    And to deserialize:

    public static Serializable deserializeClass(byte[] data) {
        DataInputStream input = new DataInputStream(new ByteArrayInputStream(data));
        Object deserializedObject;
        Serializable result = null;
        try {
            String classType = input.readUTF();
            deserializedObject = Class.forName(classType).newInstance();
            if(deserializedObject instanceof Serializable) {
                result = (Serializable)deserializedObject;
                result.deserialize(input);
            }
        } catch (IOException ex) {
            result = null;
        } catch (ClassNotFoundException ex) {
            result = null;
        } catch (InstantiationException ex) {
            result = null;
        } catch (IllegalAccessException ex) {
            result = null;
        }
        return result;
    }
    

提交回复
热议问题