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

D

davidxiongcn

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.
 
T

Thomas Hawtin

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
 
D

davidxiongcn

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?
 
D

davidxiongcn

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?
 
T

Thomas Hawtin

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
 
W

Wesley Hall

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/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top