2

I have a blob object which I am trying to convert to String or XML format. I am not able to properly serialize it as I don't know the format. How can I convert this blob?

 Blob blob = rs.getBlob(4);
 StringBuffer strOut = new StringBuffer();
 String aux = null;
 BufferedReader br = new BufferedReader(new Inputblob.getBinaryStream()));
try {
    while ((aux = br.readLine()) != null) {
        strOut.append(aux);
    }
    } 
catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

System.out.println(aux);

The output comes in some strange character format:

enter image description here

8
  • By definition you have to know the binary structure of your blob. Commented Sep 2, 2015 at 7:03
  • Any clue ? about it's origin ? Commented Sep 2, 2015 at 7:04
  • What's the sample data from your blob? Can you post some words/characters of it? Commented Sep 2, 2015 at 7:05
  • I couldnt copy paste it as it was changing so added image of it, hope it helps Commented Sep 2, 2015 at 7:11
  • Is your blob data in your database an xml? Commented Sep 2, 2015 at 7:28

2 Answers 2

2

The Blob was in Portable Object Format it means that the data has been serialized so I must deserialise it to a java object.

                        Blob blob = rs.getBlob(4);

                            int blobLength = (int) blob.length();  
                            byte[] blobAsBytes = blob.getBytes(1, blobLength);
                            //release the blob and free up memory. (since JDBC 4.0)
                            blob.free();

                            ByteBuffer buff = ByteBuffer.wrap(blobAsBytes);

                            Object obj = PofSerializerHelper.deserialize1(buff); 

This is was as desired, so issue solved. Thanks

Sign up to request clarification or add additional context in comments.

Comments

0

As you do not know the structure, the de-serializing of the object into string would not make too much sense.

So if you data set is small. You can use this.

java.sql.Blob ablob = rs.getBlob(1);
String str = new String(ablob.getBytes(1l, (int) ablob.length()));

if the data size exceeds then there are chances to get OutOfMemoryExceptions.

You can try the code above, if that fails you could try this.

StringBuffer strOut = new StringBuffer();
String aux;
// We access to stream, as this way we don't have to use the CLOB.length() which is slower...
// assuming blob = resultSet.getBlob("somefield");
BufferedReader br = new BufferedReader(new InputStreamReader(blob.getBinaryStream()));
while ((aux=br.readLine())!=null) {
strOut.append(aux);
}
return strOut.toString();

Do try this and let me know if these work.

3 Comments

Damn, ANY chance, any chance at all that you can get the resultset/bloc structure?
There are few conversions i.e. base64 for normal String, in XML should use CDATA but this is "unstructured data", use of XML hasn't too much sense. Never mix "character" and "byte" solution, for example if You run into unicode character etc will go wrong.
Portable Object Format , data in the blob is in the format of POF_Binary

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.