×

Discussion Board

Results 1 to 12 of 12
  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    31

    j2me byte array to object conversion

    I have a file that holds values, these values are then read by inputstream to create an object.
    I dont want everytime the app run to read the file all the time if it hasnt been changed so what Im trying to do is to get the byte array from the file using an inputstream and compare it to the byte array in the record store.
    I check if the bytes array are equals by converting them to string first. now if they're equal that means the file hasnt been changed so I dont need to read it again to make the object but I need a way to take this byte array (the one that is in the record store / the one that we obtained via the input stream, they're the same we just found out) and convert it into the object to make an object out of it instead of reading the file.
    how can I do it ? j2me doesnt have any kind of object streams ?

  2. #2
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    686

    Re: j2me byte array to object conversion

    J2ME does not support object serialization/deserialization native interface and thus you cannot simply save an object to stream or directly read a stream to an object.
    You would have to implement your own serialization for your own objects, for instance lets say you have an object with 2 members: 1 int and 1 string.
    then you would create a dataoutputstream with your outputstream and then write int using writeInt() and then your string using writeUTF()
    to read your object you would create a datainputstream with your inputstream and read the int using readInt() and your string using readUTF
    and assign those values to a new object instance of your class that you will create.
    Heard of DVLUP? Join here

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    31

    Re: j2me byte array to object conversion

    Quote Originally Posted by shai.i View Post
    J2ME does not support object serialization/deserialization native interface and thus you cannot simply save an object to stream or directly read a stream to an object.
    You would have to implement your own serialization for your own objects, for instance lets say you have an object with 2 members: 1 int and 1 string.
    then you would create a dataoutputstream with your outputstream and then write int using writeInt() and then your string using writeUTF()
    to read your object you would create a datainputstream with your inputstream and read the int using readInt() and your string using readUTF
    and assign those values to a new object instance of your class that you will create.
    hoped I wont have to write my own implementation , can you tell me how the readint readutf and all the other read methods know how to get from the byte array integers and string ? they know how to seperate the different types when they read the bytes ? I hope you understand my question .
    lets say I have object with the following fields :
    string str
    int num;

    so when I use readUTF ill get the str values for sure ?

    also what happens if I have a field which is an array of other class how do i read it ?

  4. #4
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,688

    Re: j2me byte array to object conversion

    The only real question is the String-related stuff: readUTF actually expects to read data which has been written with writeUTF: http://docs.oracle.com/javame/config...a.lang.String)
    Quote Originally Posted by docs
    First, two bytes are written to the output stream as if by the writeShort method giving the number of bytes to follow. This value is the number of bytes actually written out, not the length of the string. Following the length, each character of the string is output, in sequence, using the UTF-8 encoding for the character.
    So these UTF strings are actually prefixed with their length.
    For the rest of your question: unless you explicitly store type information, there is no type information. You have to know/remember what field follows what field in the serialized dataset. The simple datatypes have a fixed length, so it does not matter if you writeInt(0) or writeInt(1234567890), in both cases 4 bytes are written into the stream, so a readInt will read the value back properly.

  5. #5
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    686

    Re: j2me byte array to object conversion

    it will "know" to read the proper string value because you wrote the proper string value, you have to write the values and read the values in the same order ( so if you write int then string you have to read it the same way first int then string )
    if you have an array of something then I would suggest you first write the array length as int so something like (dataoutputstream.writeint(array.length) and then iterate over the array and write each of the array cells, if its simple type then just write it if its an object type then just call your implementation of the serialization method with the current outputstream).
    Heard of DVLUP? Join here

  6. #6
    Registered User
    Join Date
    Jan 2014
    Posts
    31

    Re: j2me byte array to object conversion

    ok ill try to implement a serializable interface ill see how its going on. by the way when I see for example changes were made to the file by comparing the byte arrays ill have to read the file again and then write the object to the recordstore so for this thing I wont have to implement a serialize method right ? I could just use datainputstream to read the file and write it to a byte array and pass it to the record store correct?

  7. #7
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    686

    Re: j2me byte array to object conversion

    In general that's correct (if you want to overwrite the changes) but I would also suggest you to implement a versioning system for your files so you wont have to read the entire "file" and compare the 2 byte arrays, so you can for instance before writing the file data as a bytearray write an int (or a long depanding on how many changes you expect) and increment that int/long with every change and compare that to an int/long of the file.
    Heard of DVLUP? Join here

  8. #8
    Registered User
    Join Date
    Jan 2014
    Posts
    31

    Re: j2me byte array to object conversion

    ok I have a little problem here, here are the fields of the class I want to deserializ :
    private Image backgroundImage;
    private Point point;
    private Point [] vertices;
    private String difficulty;
    private int speed;
    private Ball [] balls;

    now how can I read the bytes of the image / the object types Ball and Point ? class Point has two fields of type int so I guess for the first Point field I read 8 bytes and for the array of points I read 8 * array.length bytes but what about image and Ball ? Ball holds a Sprite and an int so how do I read a Sprite / picture how can I know their byte size ?

  9. #9
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    686

    Re: j2me byte array to object conversion

    As for the image you would have to write its RGB data and then read it and create the image from it, you can get the image RGB data using Image.getRGB() (read the documentation)
    Sprites are also basically images with position on screen (and current frame, reference point, etc) so just write all these values and read them and set them to the created sprite object.


    (to calculate the image byte array size you would use the following formula (width*height*4) however note that the getRGB method returns an int array and not byte array so you have to convert the int array manually to a byte array (every int = 4 bytes so you pass an int array of size width*height to the getRGB method).
    Heard of DVLUP? Join here

  10. #10
    Registered User
    Join Date
    Jan 2014
    Posts
    31

    Re: j2me byte array to object conversion

    Quote Originally Posted by shai.i View Post
    As for the image you would have to write its RGB data and then read it and create the image from it, you can get the image RGB data using Image.getRGB() (read the documentation)
    Sprites are also basically images with position on screen (and current frame, reference point, etc) so just write all these values and read them and set them to the created sprite object.


    (to calculate the image byte array size you would use the following formula (width*height*4) however note that the getRGB method returns an int array and not byte array so you have to convert the int array manually to a byte array (every int = 4 bytes so you pass an int array of size width*height to the getRGB method).
    im starting to think this all thing doesnt pay off too much things have to be done and I lose what I want to achieve, the class reads data from the txt files located in the resources folder and use those values to create objects, but then I wanted to make something so I wont have to read the files each time if they havnt been changed but doing this seems to cost more than just do it again, I want to hear your opinion what do you think ?

  11. #11
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,688

    Re: j2me byte array to object conversion

    You can always consider checking the documentation. Image has getRGB and createRGBImage. Of course it still needs some work, as you have to store width and height yourself, and create and re-create the RGB array, as it is made of integers and not bytes:
    Code:
    void SerializeImage(Image img, DataOutputStream stream) throws Exception
    {
        final int w=img.getWidth();
        final int h=img.getHeight();
        stream.writeInt(w);
        stream.writeInt(h);
        final int rgb[]=new int[w*h];
        img.getRGB(rgb,0,w,0,0,w,h);
        for(int i=0;i<rgb.length;i++)
            stream.writeInt(rgb[i]);
    }
    
    Image DeserializeImage(DataInputStream stream) throws Exception
    {
        final int w=stream.readInt();
        final int h=stream.readInt();
        final int rgb[]=new int[w*h];
        for(int i=0;i<rgb.length;i++)
            rgb[i]=stream.readInt();
        return Image.createRGBImage(rgb,w,h,true); // or false, as you wish
    }
    I am not aware of Point datatype, but it presumably has 2 integers inside, serialization could be done with 2 writeInt calls, deserialization with 2 readInt calls. Ball is certainly your custom type, but there you can apply the same approach.

    (Of course this is not a reply to the 2-minutes old query, but the previous part of the discussion)

  12. #12
    Registered User
    Join Date
    Jan 2014
    Posts
    31

    Re: j2me byte array to object conversion

    Quote Originally Posted by wizard_hu_ View Post
    You can always consider checking the documentation. Image has getRGB and createRGBImage. Of course it still needs some work, as you have to store width and height yourself, and create and re-create the RGB array, as it is made of integers and not bytes:
    Code:
    void SerializeImage(Image img, DataOutputStream stream) throws Exception
    {
        final int w=img.getWidth();
        final int h=img.getHeight();
        stream.writeInt(w);
        stream.writeInt(h);
        final int rgb[]=new int[w*h];
        img.getRGB(rgb,0,w,0,0,w,h);
        for(int i=0;i<rgb.length;i++)
            stream.writeInt(rgb[i]);
    }
    
    Image DeserializeImage(DataInputStream stream) throws Exception
    {
        final int w=stream.readInt();
        final int h=stream.readInt();
        final int rgb[]=new int[w*h];
        for(int i=0;i<rgb.length;i++)
            rgb[i]=stream.readInt();
        return Image.createRGBImage(rgb,w,h,true); // or false, as you wish
    }
    I am not aware of Point datatype, but it presumably has 2 integers inside, serialization could be done with 2 writeInt calls, deserialization with 2 readInt calls. Ball is certainly your custom type, but there you can apply the same approach.

    (Of course this is not a reply to the 2-minutes old query, but the previous part of the discussion)
    Point is my type aswell but I would like to know what you guys think this serialization and deserialization seems to costy because it my serializable class has an image in it and i think it'll be just as slow as reading the values all over again from the files, what should I do ?

Similar Threads

  1. byte array and array object!!
    By divi11 in forum Symbian
    Replies: 7
    Last Post: 2012-05-24, 17:46
  2. Converting image object into byte array
    By kamalinfo2k2@yahoo.com in forum Mobile Java General
    Replies: 7
    Last Post: 2011-02-26, 10:57
  3. How to get byte array of Image object.
    By swap842 in forum Mobile Java General
    Replies: 6
    Last Post: 2010-06-23, 18:35
  4. Byte Array to Image conversion
    By kumar_a in forum Symbian Tools & SDKs
    Replies: 11
    Last Post: 2008-10-16, 08:11
  5. can we publish an object as byte array
    By mearunsingh in forum Symbian
    Replies: 2
    Last Post: 2008-05-07, 09:02

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×