Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting Inputstream from BLOB problrm  RSS feed

 
Wai Meng Ng
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I need to save an InputStream read from an Oracle BLOB field into an object with an attribute of InputStream. How can i do this?
<The current code>
......
Blob blob = rs.getBlob("ATTACHMENT");
InputStream is = blob.getBinaryStream();
object.setAttachment(is);
However when i try to use available to check the size, it always returns a 0.
If so, then how may i know the size of the InputStream to loop through it and write the data out?
Basically, I have an web application that allows the user to save some info. including an uploadable binary file (pdf files). Upon saving, the user may come back some other time to view the contents . The ATTACHMENT Field is the BLOB that stores the .pdf file that I need to save. Afterwhich I need to use a servlet to display the pdf file back to the using response.getOutputStream(). Can someone please help me to solve this problem? or give me some sample code show how it should be done?
Many thanks in advance...
Cheers,
wmeng
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However when i try to use available to check the size, it always returns a 0.
Don't use available(), it's worthless. It only tells you how many bytes are available to be read at that instant. Well, at the instant you call it, maybe there aren't any bytes available yet, because the network hasn't sent them yet. But they're coming, you just have to wait for them. So really, just ignore the available() method and call read(), which will wait until theres actually something to read. Here's one way to do this:

Or more efficiently:
 
Wai Meng Ng
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the heads up,
But how i actually know when has the source stream has actually indeed finished 'sending'? I mean how long must I wait?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what the != -1 does. Look at the documentation for read() - it returns a -1 when there is nothing more to be read in the stream. So you just keep reading until you get a return value of -1.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the I/O and Streams forum...
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!