• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Getting Inputstream from BLOB problrm

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the I/O and Streams forum...
 
Sasparilla and fresh horses for all my men! You will see to it, won't you tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic