Problem with URL encode serialized object

  • Thread starter Francois Gagnon
  • Start date
F

Francois Gagnon

Hi,
would anyone know what the problem with this code is? the error I get
is: StreamCorruptedException: invalid stream header

URL encode/decode must be causing the problem, but I don't see why...

thanks in advance...


fg



class test implements Serializable{

public int value = 1234;

public static void main(String [] a){

try{
test t = new test();
System.out.println("value start:" + t.value);


ByteArrayOutputStream s = new ByteArrayOutputStream();
ObjectOutputStream o = new ObjectOutputStream(s);

o.writeObject(t);

String str = URLEncoder.encode(o.toString(), "UTF-8");
System.out.println("encoded:" + str);


ByteArrayInputStream stream = new
ByteArrayInputStream(URLDecoder.decode(str, "UTF-8").getBytes("UTF-8"));
ObjectInputStream i = new ObjectInputStream(stream);

t = (test) i.readObject();

System.out.println("value decoded:" + t.value);
}
catch(IOException e)
{
System.err.println("IO error:" + e.getMessage());
e.printStackTrace(System.err);
}

catch (java.lang.ClassNotFoundException c)
{
System.err.println("error:" + c.getMessage());
c.printStackTrace(System.err);
}
}
 
J

John C. Bollinger

Francois said:
Hi,
would anyone know what the problem with this code is? the error I get
is: StreamCorruptedException: invalid stream header

URL encode/decode must be causing the problem, but I don't see why...

I agree that that general part of the program is likely to be the
problem. You could check that assumption by removing the encode /
decode part and testing again, but see below.
class test implements Serializable{

public int value = 1234;

public static void main(String [] a){

try{
test t = new test();
System.out.println("value start:" + t.value);


ByteArrayOutputStream s = new ByteArrayOutputStream();
ObjectOutputStream o = new ObjectOutputStream(s);

o.writeObject(t);

String str = URLEncoder.encode(o.toString(), "UTF-8");

You have a fundamental design problem here. You are taking a random
sequence of bytes and treating it as if it were a UTF-8 encoded sequence
of characters. It isn't (unless you get very lucky), and the result of
the conversion is unlikely to be amenable to the reverse transformation.
System.out.println("encoded:" + str);

Do you, by any chance, see very many encoded question mark characters in
the string? That would be a hint.
ByteArrayInputStream stream = new
ByteArrayInputStream(URLDecoder.decode(str, "UTF-8").getBytes("UTF-8"));

Very likely the URL decoding is perfectly reversible, but the UTF-8
encoding doesn't give you back what you started with. Garbage in,
garbage out.

[...]

So why do you want to URL encode the data anyway? If you can avoid the
encoding step altogether then you would be better off. If you can't
avoid encoding then use an encoding scheme that is designed for byte
streams -- a base64 encoding would be a typical example. You could in
fact write your own URL encoder that worked on a byte stream instead of
on a String, but URL encoding is very inefficient for raw binary data.

If you are sending serialized objects over HTTP, then consider just
setting the content type to application/octet-stream and sending the
serialized object in the message body without further encoding.
Depending on exactly what you are trying to do, you might also find that
it is easier to use RMI than to build your own object transport.


John Bollinger
(e-mail address removed)
 

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

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top