I have a big CLOB (more than 32kB) that I want to read to a String, using StringBuilder. How do I do this in the most efficient way? I can not use the \"int length\" constru
A friendly helper method using apache commons.io
Reader reader = clob.getCharacterStream();
StringWriter writer = new StringWriter();
IOUtils.copy(reader, writer);
String clobContent = writer.toString();
CLOB are like Files, you can read parts of it easily like this
// read the first 1024 characters
String str = myClob.getSubString(0, 1024);
and you can overwrite to it like this
// overwrite first 1024 chars with first 1024 chars in str
myClob.setString(0, str,0,1024);
I don't suggest using StringBuilder and fill it until you get an Exception, almost like adding numbers blindly until you get an overflow. Clob is like a text file and the best way to read it is using a buffer, in case you need to process it, otherwise you can stream it into a local file like this
int s = 0;
File f = new File("out.txt");
FileWriter fw new FileWriter(f);
while (s < myClob.length())
{
fw.write(myClob.getSubString(0, 1024));
s += 1024;
}
fw.flush();
fw.close();
If using Mule, below are the steps.
Follow below steps.
Enable streaming in the connector i.e. progressiveStreaming=2
Typecast DB2 returned CLOB to java.sql.Clob (IBM Supports this type cast)
Convert that to character stream (ASCII stream sometimes may not support some special characters). So you may to use getCharacterStream()
That will return a "reader" object which can be converted to "String" using common-io (IOUtils).
So in short, use groovy component and add below code.
clobTest = (java.sql.Clob)payload.field1
bodyText = clobTest.getCharacterStream()
targetString = org.apache.commons.io.IOUtils.toString(bodyText)
payload.PAYLOADHEADERS=targetString return payload
Note: Here I'm assuming "payload.field1" is holding clob data.
That's it!
Regards Naveen
I can not use the "int length" constructor for
StringBuilder
since the length of my CLOB is longer than aint
and needs along
value.
If the CLOB length is greater than fits in an int, the CLOB data won't fit in a String either. You'll have to use a streaming approach to deal with this much XML data.
If the actual length of the CLOB is smaller than Integer.MAX_VALUE
, just force the long
to int
by putting (int)
in front of it.
Ok I will suppose a general use, first you have to download apache commons, there you will find an utility class named IOUtils which has a method named copy();
Now the solution is: get the input stream of your CLOB object using getAsciiStream() and pass it to the copy() method.
InputStream in = clobObject.getAsciiStream();
StringWriter w = new StringWriter();
IOUtils.copy(in, w);
String clobAsString = w.toString();