Qestion about convert Object to byte[] and convert it back

Discussion in 'Java' started by davidxiongcn@gmail.com, Nov 4, 2006.

  1. Guest

    I need to convert an object to String to store it into structure in our
    application, so I write some function to convert Object into byte[] and
    convert it back. The following is a test program:

    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io_ObjectInputStream;
    import java.io_ObjectOutputStream;

    public class test {
    private static Object getObjectFromByteArr(byte [] byteArr) throws
    java.io.IOException, ClassNotFoundException {
    ObjectInputStream in = new ObjectInputStream(new
    ByteArrayInputStream(byteArr));
    Object obj = in.readObject();
    in.close();
    return obj;
    }

    private static byte [] getBlobFromObject(Object obj) throws
    java.io.IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ObjectOutputStream objOut = new ObjectOutputStream(out);
    objOut.writeObject(obj);
    byte [] bytes = out.toByteArray();
    objOut.close();
    return bytes;
    }

    public static void main(String [] args) {
    try {
    Integer i = new Integer(0);
    // it's OK here
    Integer j = (Integer)getObjectFromByteArr(getBlobFromObject(i));
    System.out.println(j);
    // try to convert byte[] to String
    String str = new String(getBlobFromObject(i));
    // and convert it back, exception occurs!!!
    Integer k = (Integer)getObjectFromByteArr(str.getBytes());
    System.out.println(k);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

    But it always report a java.io.InvalidClassException, as it seems the
    serialVersionUID has been changed when I convert it to String. I have
    compared the convert result and the result of String.getBytes(), they
    are different. But I have no idea about how to fix it.
    What's wrong with it when I convert a byte[] to String? I tried to use
    a characterset in creating new String, the problem remains.
     
    , Nov 4, 2006
    #1
    1. Advertising

  2. wrote:
    > I need to convert an object to String to store it into structure in our
    > application, so I write some function to convert Object into byte[] and
    > convert it back. The following is a test program:


    Why do you want to convert it to a String? That's nuts.

    > ByteArrayOutputStream out = new ByteArrayOutputStream();
    > ObjectOutputStream objOut = new ObjectOutputStream(out);
    > objOut.writeObject(obj);
    > byte [] bytes = out.toByteArray();


    This converts the *current* received output to a byte[].

    > objOut.close();


    This *flushes* unwritten data to the output stream.

    > // try to convert byte[] to String
    > String str = new String(getBlobFromObject(i));
    > // and convert it back, exception occurs!!!
    > Integer k = (Integer)getObjectFromByteArr(str.getBytes());


    > But it always report a java.io.InvalidClassException, as it seems the
    > serialVersionUID has been changed when I convert it to String. I have
    > compared the convert result and the result of String.getBytes(), they
    > are different. But I have no idea about how to fix it.
    > What's wrong with it when I convert a byte[] to String? I tried to use
    > a characterset in creating new String, the problem remains.


    Works for me, but then perhaps you have a different default character
    set. I'm using ISO8859-15; perhaps you are using a Microsoft proprietary
    character set. Look at the API docs for the methods you have used. (I
    think methods implicitly using the default character set or locale
    should be deprecated.)

    You could use String(byte[],int) and
    String.getBytes(int,int,byte[],int), but they are deprecated for good
    reason.

    Tom hawtin
     
    Thomas Hawtin, Nov 4, 2006
    #2
    1. Advertising

  3. Guest

    I think I have solved the problem by using characterset: 8859_1. But
    what's the default characterset? Why it can cause the problem above?
     
    , Nov 4, 2006
    #3
  4. Guest

    Another question: if I use 8859_1, when there is \0 in byte[], will it
    cause problem? Will the bytes after the byte \0 be lost?
     
    , Nov 4, 2006
    #4
  5. wrote:
    > I think I have solved the problem by using characterset: 8859_1. But
    > what's the default characterset? Why it can cause the problem above?


    Latin 1 should work, by coincidence.

    The default character set is whatever happens to be configured. So not
    something you want to rely upon.

    Anyway, attempting to store binary data in Strings is a really bad idea.

    Tom Hawtin
     
    Thomas Hawtin, Nov 4, 2006
    #5
  6. Wesley Hall Guest

    wrote:
    > I need to convert an object to String to store it into structure in our
    > application, so I write some function to convert Object into byte[] and
    > convert it back. The following is a test program:
    >
    > import java.io.ByteArrayInputStream;
    > import java.io.ByteArrayOutputStream;
    > import java.io_ObjectInputStream;
    > import java.io_ObjectOutputStream;
    >
    > public class test {
    > private static Object getObjectFromByteArr(byte [] byteArr) throws
    > java.io.IOException, ClassNotFoundException {
    > ObjectInputStream in = new ObjectInputStream(new
    > ByteArrayInputStream(byteArr));
    > Object obj = in.readObject();
    > in.close();
    > return obj;
    > }
    >
    > private static byte [] getBlobFromObject(Object obj) throws
    > java.io.IOException {
    > ByteArrayOutputStream out = new ByteArrayOutputStream();
    > ObjectOutputStream objOut = new ObjectOutputStream(out);
    > objOut.writeObject(obj);
    > byte [] bytes = out.toByteArray();
    > objOut.close();
    > return bytes;
    > }
    >
    > public static void main(String [] args) {
    > try {
    > Integer i = new Integer(0);
    > // it's OK here
    > Integer j = (Integer)getObjectFromByteArr(getBlobFromObject(i));
    > System.out.println(j);
    > // try to convert byte[] to String
    > String str = new String(getBlobFromObject(i));
    > // and convert it back, exception occurs!!!
    > Integer k = (Integer)getObjectFromByteArr(str.getBytes());
    > System.out.println(k);
    > } catch (Exception e) {
    > e.printStackTrace();
    > }
    > }
    > }
    >
    > But it always report a java.io.InvalidClassException, as it seems the
    > serialVersionUID has been changed when I convert it to String. I have
    > compared the convert result and the result of String.getBytes(), they
    > are different. But I have no idea about how to fix it.
    > What's wrong with it when I convert a byte[] to String? I tried to use
    > a characterset in creating new String, the problem remains.
    >


    If you really must do this, then you should run the object byte array
    through a Base64 encoder first. The result will be a byte[] that can be
    safely converted to 7-bit ASCII for storage as text. Email systems use
    this basic mechanism for sending attachments. Jakarta commons codec
    contains a Base64 encoder/decoder class, you can find it at
    http://jakarta.apache.org/commons/codec/
     
    Wesley Hall, Nov 4, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. the idiot

    align center qestion

    the idiot, May 6, 2005, in forum: HTML
    Replies:
    3
    Views:
    449
    the idiot
    May 6, 2005
  2. DaBeef
    Replies:
    1
    Views:
    635
    Matt Humphrey
    Jul 21, 2006
  3. a n00b regex qestion

    , Dec 3, 2007, in forum: Python
    Replies:
    2
    Views:
    312
    Tim Chase
    Dec 3, 2007
  4. smart

    qestion on combobox

    smart, Mar 17, 2008, in forum: Java
    Replies:
    1
    Views:
    349
    GArlington
    Mar 17, 2008
  5. Nicole

    asp qestion

    Nicole, Oct 2, 2003, in forum: ASP General
    Replies:
    2
    Views:
    128
    Dan Brussee
    Oct 2, 2003
Loading...

Share This Page