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);
}
}
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);
}
}