• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading from same InputStream twice  RSS feed

 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!!

Good to be back after a long, long time...and with a new issue!!!

The actual problem is with xml parsing. But The Stream related issue is this:

Is there any way in which I can read from an InputStream twice???

I am reading the contents of a file using a FileInputStream once and then want to read the contents once again.
I have already tried mark(), reset() etc which are not working.

I have to use jdk 1.3.X

Is there any way in which i can read the contents of the file twice, using the same InputStream or by cloning the InputStream or something like that???
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Streams are pretty much one-way conduits. One could use PushbackInputStream or the corresponding reader to push what you've read back into the stream.
If the file were small enough one could read it into a stream and pass it to other entities requiring a Reader by wrapping it with StringReader.
If the file were too large to do that, I'd probably just read it twice using different reader/input streams.
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe,

The problem actually is this:

I am passing the InputStream(which is a FileInputStream) to a SAX XML Parser. The parser parses the file, breaking it into smaller fragments and passing it on to a JMS publisher. Now, the issue is that if the file is not valid XML(say ending root tag is missing), the parser still parses the file and throws an Exception only when it comes to the point where the XML is actually invalid, which means that i have already sent some messages to the publisher before realising that the file is invalid. So, to prevent that, we need to know before hand whether the file is invalid.

Now, I could always break up the file and store the fragments in a List of something and send them all at once after parsing the file, but here are the issues with it :

1. The file is normally HUGE(a Gig or more). And hence the choice of using a SAX parser to a DOM parser.

2. For the above reason, i cant really store all the fragments before sending them out. I run out of memory.

So, the only solution was to parse the file twice, time consuming, but not as hard on the memory. And hence the need to read from the same stream twice.

I have to use either SAX or DOM...preferably SAX coz I have the entire code in it. I cant use any other parser, though I would have loved to use jdom.

Any workaround to this?? Hmmm..and now this post probably doesnt sound right in this forum, does it???
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe in the past I would read it all into a ByteArrayInputStream so that I can read multiple times from it. However, if memory is an issue, and you're reading such a huge file a second time, it doesn't seem like it would be a bad idea to just start over with another InputStream to read the file in again (if the file is already huge, then maybe the performance hit would be negligible).
 
kotesh rao
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if u can get java cook book.
u can find this...may be use ful for you.
Topic : Duplicating a Stream as It Is Written
check it out.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!