I am using this static class as a way to serialize objects into strings before sending / using them in various ways. However the objects being serialized only seem to be serialized correctly once. A variable that starts at 20 and increments every update only gets to 21 even though if I print it right before its serialized , the value is correct ( somewhere around 1500 ).
Before you answer , note that I am using reset() and writeUnshared(). I tried pretty much everything I could find online and nothing seems to work.
public class InformationInterface {
static ObjectInputStream ois;
static ObjectOutputStream oos;
static ByteArrayOutputStream baos;
public static Object fromString( String s ) throws IOException ,
ClassNotFoundException {
byte [] data = Base64.getDecoder().decode( s );
ois = new ObjectInputStream(
new ByteArrayInputStream( data ) );
Object o = ois.readUnshared();
ois.close();
return o;
}
public static String toString( Serializable o ) throws IOException {
if(baos!=null){
baos.flush();
}
baos = new ByteArrayOutputStream();
baos.reset();
oos = new ObjectOutputStream( baos );
oos.reset();
oos.writeUnshared(o);
oos.flush();
oos.close();
return Base64.getEncoder().encodeToString(baos.toByteArray());
}
}
I have read many posts here stating that ObjectInputStream keeps a reference of sent objects ( Even though im creating a new one every time and only keeping a reference in between serializations ) using writeUnshared() or reset() works at sending multiple objects but I cant seem to get it to work on my part.
I dont understand why this isnt working. Im not even sending multiple objects simultaneously and simply want a way to serialize the same object throughout the lifetime of my server. However , the received serialized based64 String does not change at all after the first update is received.
Note that I tried multiple other ways to do it , including sticking a direct try/catch block creating an entirely new objectoutputstream and calling reset right where I would usually call these static functions. I also tried using ObjectOutput and ObjectInput and got the same result.
Everyone seems to have solved their problem using reset() , well this isnt my case. Could I be doing something wrong somewhere else in my code ? Since I read the data before its serialized and can confirm that it should be in fact a new object right before its serialized I doubt the problem is very far.
Edit , Just tried a different way using apacheutils and I get the exact same results. Here is the new code with the output :
public static Object fromString( String s ) throws IOException ,
ClassNotFoundException {
return convertFromBytes(Base64.getDecoder().decode(s));
}
private static byte[] convertToBytes(Serializable object) throws IOException {
ClientInformation client = (ClientInformation)object;
System.out.println(client.hp);
return SerializationUtils.serialize(object);
}
private static Object convertFromBytes(byte[] bytes) throws IOException, ClassNotFoundException {
return SerializationUtils.deserialize(bytes);
}
public static String toString( Serializable o ) throws IOException {
String s = Base64.getEncoder().encodeToString(convertToBytes(o));
System.out.println(s);
return s;
}
794
rO0ABXNyACBjb20ucjMuZW5kZ2FtZS5DbGllbnRJbmZvcm1hdGlvbj8t7NEouSNIAgAISQACaHBTAAdzbmFrZWlkSQAIc3Bkc2NhbGVMAANhYmN0ABJMamF2YS9sYW5nL1N0cmluZztMAAJpcHEAfgABTAAEbmFtZXEAfgABTAAIcG9zaXRpb250AB9MY29tL2JhZGxvZ2ljL2dkeC9tYXRoL1ZlY3RvcjI7TAAFc3BlZWRxAH4AAnhwAAADGgGkAAAAAXB0ABAvMTI3LjAuMC4xOjQ1Mzk0dAAHYXMzMmRkZHNyAB1jb20uYmFkbG9naWMuZ2R4Lm1hdGguVmVjdG9yMgyu1b5sQZuzAgACRgABeEYAAXl4cAAAAAAAAAAAc3EAfgAGPczMzQAAAAA=
795
rO0ABXNyACBjb20ucjMuZW5kZ2FtZS5DbGllbnRJbmZvcm1hdGlvbj8t7NEouSNIAgAISQACaHBTAAdzbmFrZWlkSQAIc3Bkc2NhbGVMAANhYmN0ABJMamF2YS9sYW5nL1N0cmluZztMAAJpcHEAfgABTAAEbmFtZXEAfgABTAAIcG9zaXRpb250AB9MY29tL2JhZGxvZ2ljL2dkeC9tYXRoL1ZlY3RvcjI7TAAFc3BlZWRxAH4AAnhwAAADGwGkAAAAAXB0ABAvMTI3LjAuMC4xOjQ1Mzk0dAAHYXMzMmRkZHNyAB1jb20uYmFkbG9naWMuZ2R4Lm1hdGguVmVjdG9yMgyu1b5sQZuzAgACRgABeEYAAXl4cAAAAAAAAAAAc3EAfgAGPczMzQAAAAA=
796
rO0ABXNyACBjb20ucjMuZW5kZ2FtZS5DbGllbnRJbmZvcm1hdGlvbj8t7NEouSNIAgAISQACaHBTAAdzbmFrZWlkSQAIc3Bkc2NhbGVMAANhYmN0ABJMamF2YS9sYW5nL1N0cmluZztMAAJpcHEAfgABTAAEbmFtZXEAfgABTAAIcG9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