tags:

views:

214

answers:

1

hello, i m developing an application in j2me which features a Bluetooth Communication of an object of a user-defined class. I received the byte array using following code:


public byte[] receiveBoard() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ins = conn.openInputStream();
            int n;

            while((n=ins.read(brd))>0) {
                baos.write(brd, 0, n);
            }
        } catch(Exception e) {
            System.err.println(e);
        }
        return baos.toByteArray();
    }

  • Correct me if this code is having mistakes.

Now my problem is,

  1. i m not able to convert this "byte array" back to the requird instance of user-defined class. I m not able to retrieve my 'initial object' from the byte array, so that i can use it in my application again after the reception.

i tried using ObjectInputStream, ObjectInput classes but it is not importing those packages!! Please Please help me fast!!!!

+1  A: 

No it won't work, you have to make your own object serialization in J2ME. That means figuring out what properties of your object you want serialized, and writing your own code to write the object to a byte array, and read it back again.

You'll probably find DataInputStream and DataOutputStream very useful for this.

funkybro
which means: don't serialize object more complicated than a String. dump ints, floats, Strings... into the stream, then read them back in the correct order to rebuild your object from its instance variables.
QuickRecipesOnSymbianOS
yep, pare your object down to Strings and primitives
funkybro