Serialization via J2ME or BlackBerry APIs

岁酱吖の 提交于 2019-11-28 09:05:40

问题


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

Thanks.


回答1:


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;
}



回答2:


Java ME, unfortunately, doesn't have any built-in APIs for serialization, so you'll have to invent something yourself.




回答3:


If your goal is to serialize an object or object graph for persisting to flash memory, you can use the PersistentStore class. Many of the native object types such as Boolean, Byte, Character, Integer, Long, Object, Short, String, Vector, Hashtable are implicitly persistable.




回答4:


You are stuck with creating your own serialization process for your classes. It wouldn't be too difficult to create your own base class and then use somesort of reflection to automatically serialize your properties.

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    DataOutputStream outputStream = new DataOutputStream(baos);
    try {
        // serialize your object - 
        outputStream.writeInt(this.name);
        // Then push the player name.
        outputStream.writeUTF(this.timestamp);
    }
    catch (IOException ioe) {
        System.out.println(ioe);
        ioe.printStackTrace();
    }


// Extract the byte array
byte[] b = baos.toByteArray();


来源:https://stackoverflow.com/questions/2449005/serialization-via-j2me-or-blackberry-apis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!