Question

I have an InputStream that I know is a PDF that I need to use to update a Blob in Oracle. How can I transform this stream into a BufferedOutputStream, or otherwise update the Blob with this stream information?

Note that I am not allowed to wrap an Oracle class in my code due to the restrictions of the code.

What I've tried so far:

I have already tried using this answer: https://stackoverflow.com/a/1574857/2188082 to connect the streams, but the result is a NullPointerError, because the OutputStream is empty.

The current Query I am trying to run:

pstmt = dbConn.prepareStatement("UPDATE "+ qualTable("document")+" set document_blob ="
                   +" utl_raw.cast_to_raw('"+inStream+"') where document_id = " + documentId);

       pstmt.execute();
       pstmt.close();

I am aware that utl_raw.cast_to_raw may not be the correct oracle method to call for an inStream read, unfortunately I am not as well-versed in Oracle as I would like to be, so I am unaware of the correct cast I should be using.

Was it helpful?

Solution

PreparedStatement.setBlob(parameterIndex, inputStream, length).

Like setString and other setters. No OutputStream, but a length needed.

OTHER TIPS

A typical way to do this is to create a byte[] array and to use it to transfer the data:

byte[] buffer = new byte[4096]; // 4K buffer...
int len = 0;
while ((len = input.read(buffer)) >= 0) {
   output.write(buffer, 0, len);
}
output.flush();

This copies some number of bytes in each loop from the input to the output, until you run out of input.

The size of the buffer is a tunable parameter which yu should test in your environment to see what works best.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top