• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reuse of InputStreamReader

 
Erik Pragt
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
I have a very small problem, but I just can't figure out how to solve it.
I have created an InputStreamReader from a URLConnection, which I use to process some XML data (in other words: the InputStream is an XML response).
Now I want to do 2 things with the inputstream:
1) print it to screen
2) process it (as said before)
But the problem is: after I processed it, the internal pointer of the InputStreamReader is pointing to the end of the stream, causing the XML processing to fail.
Maybe I could first clone the inputstream, but as I have no previous experience with clone, I'd like to delay using it untill I know enough about it.
So if anyone could help me, please let me know. It's giving me a headache!!
Have a nice Christmas,
Erik Pragt
 
Nayanjyoti Talukdar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
During the process time, u can either write the data to a file or put these into a StringBuffer object and each time append the data.
That way, as soon as the process gets over, u'r data will be stored in a file or some other object. If the process takes place first, then the InputStream pointer will reach end of the data, and then it won't display anything.
I think this will help....Merry Christmas!!
----------
Nayan.
 
Erik Pragt
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer!
However, I really need to reuse the inputstream, because I use it for parsing XML, and the XML parser can only take an inputstream as input.
Maybe you (or someone else) has an other suggestion?
Greetings, Erik
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Printing to screen is easily done by a custom FilterReader.

Then just write something like
XMLParser.parse(new EchoReader(reader));
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also copy all the input to a CharArrayWriter to obtain the file as a char[] and create two seperate CharArrayReaders for reading purposes. That would require holding the entire file contents in memory, though.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey David,
i was able to understand the problem reading your answer
i liked the FilterReader thing. its cool. i have never used it but its a good idea for filtering...
thanks, it was helpful.
regards
maulin
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic