-1

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
rO0ABXNyACBjb20ucjMuZW5kZ2FtZS5DbGllbnRJbmZvcm1hdGlvbj8t7NEouSNIAgAISQACaHBTAAdzbmFrZWlkSQAIc3Bkc2NhbGVMAANhYmN0ABJMamF2YS9sYW5nL1N0cmluZztMAAJpcHEAfgABTAAEbmFtZXEAfgABTAAIcG9zaXRpb250AB9MY29tL2JhZGxvZ2ljL2dkeC9tYXRoL1ZlY3RvcjI7TAAFc3BlZWRxAH4AAnhwAAADHAGkAAAAAXB0ABAvMTI3LjAuMC4xOjQ1Mzk0dAAHYXMzMmRkZHNyAB1jb20uYmFkbG9naWMuZ2R4Lm1hdGguVmVjdG9yMgyu1b5sQZuzAgACRgABeEYAAXl4cAAAAAAAAAAAc3EAfgAGPczMzQAAAAA=
797
rO0ABXNyACBjb20ucjMuZW5kZ2FtZS5DbGllbnRJbmZvcm1hdGlvbj8t7NEouSNIAgAISQACaHBTAAdzbmFrZWlkSQAIc3Bkc2NhbGVMAANhYmN0ABJMamF2YS9sYW5nL1N0cmluZztMAAJpcHEAfgABTAAEbmFtZXEAfgABTAAIcG9zaXRpb250AB9MY29tL2JhZGxvZ2ljL2dkeC9tYXRoL1ZlY3RvcjI7TAAFc3BlZWRxAH4AAnhwAAADHQGkAAAAAXB0ABAvMTI3LjAuMC4xOjQ1Mzk0dAAHYXMzMmRkZHNyAB1jb20uYmFkbG9naWMuZ2R4Lm1hdGguVmVjdG9yMgyu1b5sQZuzAgACRgABeEYAAXl4cAAAAAAAAAAAc3EAfgAGPczMzQAAAAA=

As you can see , the serialized string is identical.

r3eckon
  • 14
  • 3
  • Do really have to implement on object-to-text serialisation solution by yourself? If not, try [xstream](http://x-stream.github.io/tutorial.html). It is really easy to use and you can choose between conversion Object to/from XML or Object to/from [JSON](https://en.wikipedia.org/wiki/JSON). – Dangermouse Sep 28 '16 at 12:15
  • All I really want is to turn my objects into base64 strings. I dont need fancy or feature-complete serialization at all as long as a string can contain my object. What I dont understand is why every serialization attempt after the first one returns the same object even though the data before serialization is clearly different. – r3eckon Sep 28 '16 at 12:23
  • 2
    Post the class you're serializing and the code that calls what you posted. – user207421 Sep 28 '16 at 12:46
  • Im serializing a simple container class that has a couple strings , 2 serializable vectors and a couple integers. The problem isnt where this code is being called either since when I read the data right after its been serialized its immediately invalid. – r3eckon Sep 28 '16 at 12:59
  • 1
    You have to post the code that actually calls your methods as most probably error is there. Dont tell us what you do, just **show us** what you do. But if you know better where error is, then why bothering asking questions about it here? – Antoniossss Sep 28 '16 at 13:13
  • You getting the same serialized output although the state of your object changes indicates that you have a bug where you *call* your serialization routines. You might be serializing a stale copy of the object over and over again. – JimmyB Sep 28 '16 at 13:18
  • Look at my code here , I print a variable from the object im trying to serialize literally right before its being serialized , then I print the serialized string and always get that same exact string. I have basically narrowed down the error to these lines of code , I dont think dumping my entire program here will change the fact that what goes wrong happens within the code I posted. – r3eckon Sep 28 '16 at 13:19
  • 1
    @r3eckon dude, I dont want to be rude, but either show us the code you are telling magical stories about, or seek help somewhere else. Nobody wants your entire program because it is most likely painfull to watch @ it, just couple of test lines where you actually tests that "something is not working ppl help me" You could as well write small `main` method that proofs that you are NOT WRONG in your assumptions, run even from IDE.. – Antoniossss Sep 28 '16 at 13:21
  • I did test it and posted a result, why dont you read my entire post before assuming I just dont want to post my code here ? I dont see what more you could possibly want from this code : the object is fine before its serialized and then after its serialized it isnt. I dont get how in your mind the culprit is just magically somewhere else even though the entire bug happens within these lines ? – r3eckon Sep 28 '16 at 13:33
  • 1
    @r3eckon what I assume is that you tested it wrong. Period. I did test the "relevant" in your opinion pieces of code and guess what... it works. – Antoniossss Sep 28 '16 at 13:34

1 Answers1

1

This is something you should do not me FYI.

From context of code I did:

public static void main(String[] args) throws IOException {
    ClientInformation ci = new ClientInformation();
    //      System.out.println(toString(ci));
    toString(ci);
    ci.hp = 3;
    toString(ci);
    //      System.out.println(toString(ci));
}

@SuppressWarnings("serial")
private static class ClientInformation implements Serializable {
    public int hp = 2;
}

I ran it from IDE using your code and this is the result:

2
rO0ABXNyADduZXRjcmF3bGVyLmZhY3Rvcmllcy5BcHBsaWNhbnRGYWN0b3J5JENsaWVudEluZm9ybWF0aW9uu7p3ca7TTqkCAAFJAAJocHhwAAAAAg==
3
rO0ABXNyADduZXRjcmF3bGVyLmZhY3Rvcmllcy5BcHBsaWNhbnRGYWN0b3J5JENsaWVudEluZm9ybWF0aW9uu7p3ca7TTqkCAAFJAAJocHhwAAAAAw==

Strings DOES NOT MATCH . The problem is not here.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99