File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes InputStream duplication, how to ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "InputStream duplication, how to ?" Watch "InputStream duplication, how to ?" New topic

InputStream duplication, how to ?

Srinu Nanduri
Ranch Hand

Joined: Mar 20, 2006
Posts: 42

I have a problem with InputStream (FilterInputStream) in my application.

I would like to start with reminding the point that "Once an input stream is read, we cannot read it again". Any comments ?

Now, my question is, Is there any possibility to duplicate the input stream i have such that i can use the original / duplicate and pass the other one as it is further to my application.

I will be very thankful if some one can help me with this.

Thanks & Regards,

Joe Ess

Joined: Oct 29, 2001
Posts: 9191

InputStream and Reader both have a reset() method which can be used to rewind a stream to a point identified with a call to mark(). These method are not supported with all streams/readers and care must be used in setting the size of data preserved with the mark() call.

[How To Ask Questions On JavaRanch]
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
You could read the InputStream completely to a byte[] and construct any number of streams from that source with ByteArrayInputStream. (Assuming the total is reasonable to keep in memory)
subject: InputStream duplication, how to ?
It's not a secret anymore!