Search code examples
javahibernatedeserializationstringbuffer

deserialize stringBuffer


I have a db varchar field looks like the result of Java StringBuffer serialization:

íjava.lang.StringBuffer [many random characters here removed for this question]

how do I deserialize it into String?


Solution

  • Essentially you need to do this:

    byte[] varcharContents = ... // get the bytes of the field, not via a String
    ObjectInputStream ois = 
        new ObjectInputStream(new ByteArrayInputStream(varcharContents));
    StringBuffer sb = (StringBuffer)ois.readObject();
    String s = sb.toString();
    

    You'll have to hope that you can really get the original bytes produced by the serialisation back, and that they haven't been transformed on their way to and from the DB.