File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "InputStream duplication, how to ?" Watch "InputStream duplication, how to ?" New topic
Author

InputStream duplication, how to ?

Srinu Nanduri
Ranch Hand

Joined: Mar 20, 2006
Posts: 42
Hi

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,
Srinivasan.


SCJP, SCWCD
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 9024
    
    9

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
Rancher

Joined: Mar 22, 2000
Posts: 12868
    
    5
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)
Bill
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: InputStream duplication, how to ?