input stream to CLOB using java service

I have used the following code in java service to write input stream to CLOB.

CLOB clob = null;
clob.open(CLOB.MODE_READWRITE);
Writer out = clob.getCharacterOutputStream();
out.write(java.io.InputStream stream);
out.flush();
out.close();

As the above code was not compiled ,I tried with one solution suggested by reamon in previous thread Writer out = clob.getAsciiOutputStream(); and got the same result.

Anyone has suggestion to write stream to CLOB?

Is the error that the compilation fails? Or that the code does not work?

CLOB clob = null;
clob.open(CLOB.MODE_READWRITE);

If this is the code in your service, you’re going to get a null pointer exception. clob is null so clob.open will fail.

You need to instantiate a clob object. The other thread you referenced shows how to get one by reading a record from the DB.

Thanks Reamon, It was compilation error pointing out.write() method.
Even after null assignment removed, it is giving compilation error

Can you post all the code to the service (I’m assuming the above is just a snippet)? Can you post the compiler error too?

out.write(java.io.InputStream stream); has two problems.

  1. The type declaration of java.io.InputStream isn’t legal here unless you are trying to cast the stream object. In that case the right syntax would be
    out.write((java.io.InputStream) stream);

  2. The stream object isn’t defined anywhere (I was assuming you posted just a snippet and omitted pipeline related code but perhaps that is not the case?).

Removing the null assignment didn’t really change anything. Variables without an initial assignment are null. At run-time the code will still fail if you don’t create/get a CLOB object. It will compile but it won’t run.

Reamon, you are right. It is just snippet and complete code which I used is given below.

IDataCursor idcPipeline = pipeline.getCursor();

String dbServer = null;
String dbUserId = null;
String dbPasswd = null;
String dbName = null;
String dbPort = null;
String proName = null;
String xmldata = null;
String table_name = null;
InputStream stream = null;
boolean cat_flag = true;
CLOB clob = null;

if (idcPipeline.first(“stream”))
{
stream = (InputStream)idcPipeline.getValue();
}
else
{ throw new ServiceException("No Input ‘stream’ ");

}
try
{
conn=getConnection123(dbServer , dbUserId, dbPasswd ,dbPort,dbName);

clob = CLOB.createTemporary(conn, true, CLOB.DURATION_SESSION);
clob.open(CLOB.MODE_READWRITE);
Writer out = clob.getCharacterOutputStream();
out.write(stream);
out.flush();
out.close();
}
catch(Exception e)
{
throw new ServiceException(“Error occured”);
}

---------------------------------------------------------------

and the following compilation error is found with the above code.
----------------------------------------------------------------

[FONT=Times New Roman][SIZE=3]…/IntegrationServer/packages/myTest /code/source/test/util.java :424: cannot find symbol
symbol : method write(java.io.InputStream)
location: class java.io.Writer
out.write(stream);
^
Note:
…/IntegrationServer/packages/myTest /code/source/test/util.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error


From the error message I understand that the method write() is used incorrectly. [/size][/font]

Sorry I missed this earlier. java.io.Writer does not have a write() method. You’ll need create a loop that reads from the input stream and writes to the output stream.