Wednesday, February 1, 2012

problems with serialization with ServerSerializationStreamWriter

Hello!

I'm having some trouble getting the serialization with
ServerSerializationStreamWriter working.

My case is that I have a file upload. And as a result back to the
client I want to serialize a java class using the
ServerSerializationStreamWriter. On the server I have some code
looking like this:

public static byte[] serialize(Object o) throws SerializationException
{
ServerSerializationStreamWriter streamWriter = new
ServerSerializationStreamWriter(null);
streamWriter.prepareToWrite();
try {
streamWriter.writeObject(o);
} catch (Exception e) {
e.printStackTrace();
}
return streamWriter.toString().getBytes();
}

The bytes I then write to the outputstream in the response.

On the client I have something like this:

public static Object deSerialize(String s) {
SerializationStreamFactory factory =
(SerializationStreamFactory) GWT.create(CommandService.class);
SerializationStreamReader reader = null;
try {
reader = factory.createStreamReader(s);
} catch (SerializationException e) {
}

try {
return reader.readObject();
} catch (SerializationException e) {
}
return null;
}

This is not working and I'm having trouble finding any good
documentation on how to use the ServerSerializationStreamWriter. The
other way around (serializing on client and deserializing on the
server) is working fine with this code (I'm sending a serialized java
object along with the file upload):

Client;
public static String serialize(Object obj) {
SerializationStreamFactory factory =
(SerializationStreamFactory) GWT.create(CommandService.class);

// Creating stream writer
SerializationStreamWriter writer =
factory.createStreamWriter();

// Serializing object
try {
writer.writeObject(obj);
} catch (SerializationException e) {
e.printStackTrace();
}
return writer.toString();
}

Server:
public static Object deserialize(String content) throws
SerializationException {
ServerSerializationStreamReader streamReader = new
ServerSerializationStreamReader(
Thread.currentThread().getContextClassLoader(), null);
streamReader.prepareToRead(content);
return streamReader.readObject();
}

Any help would be appreciated!

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment