I’m getting a strange bug while trying to use the Java Object Serialization and moving data back and forth between my client and server, via the sendToServer() method.
The code works when i run a test case in a single class, but then fails when i move the data over through the server. The first thing I noticed is that sgs appends two bytes of data to the array that backs the bytebuffer. I managed to get around that, but now when I try to read an object from the ObjectInputStream i created from a ByteArrayInputStream i created from a byte array i created from the bytebuffer that the UserDataRecieved callback method hands me breathe now, it blocks and doesn’t finish the method.
Firstly, can anyone explain why this may be happening, and secondly, what all happens to the bytebuffer after I send it to the server?
here’s the code for my abstract event class that handles both the serialization and deserialization for the events.
Please excuse the attempted problem solving code in there.
Thanks for your help in advance
-Alex
package events;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.ByteBuffer;
public abstract class AbstractEvent implements Serializable{
public ByteBuffer dematerialize() {
byte[] bytes = null;
ObjectOutputStream oos =null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(this);
oos.flush();
baos.flush();
bytes = baos.toByteArray();
for (byte b : bytes){
System.out.print(b+" ");
}
System.out.println("");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
baos.close();
assert oos != null;
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
assert bytes != null;
ByteBuffer bb= ByteBuffer.allocate(bytes.length);
bb.put(bytes);
for (byte b : bb.array()){
System.out.print(b+" ");
}
System.out.println("");
System.out.println(bb.array().length+"long");
return bb;
}
public static Event rematerialize(ByteBuffer bb){
System.out.println("Byte buffer at pos "+bb.position());
System.out.println(bb.capacity()+" total bytes");
byte[] byteArray = new byte[bb.remaining()];
System.out.println(bb.remaining()+"bytes remaining");
bb.get(byteArray);
//System.out.println(bb.arrayOffset());
//bb.get();
//bb.get();
for (byte b : byteArray){
System.out.print(b+" ");
}
System.out.println("");
ObjectInputStream ois = null;
System.out.println(1);
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray, 0, byteArray.length);
System.out.println(bais.available()+" bytes available in bais");
Event e = null;
try {
ois = new ObjectInputStream(bais);
System.out.println(bais.available()+" bytes available in bais");
} catch (IOException e1) {
System.out.println("Couldn't make ois");
// TODO Auto-generated catch block
e1.printStackTrace();
return null;
}
try {
System.out.println(2);
System.out.println(ois.available() + " bytes can be read");
e = (Event)ois.readObject();
System.out.println(3);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("Recieved "+e.toString());
return e;
}
}