Going back in an input stream

Hi there,

I know this thread topic seems a bit non-sense, but I just can't figure how to do what I need... And what I need is to make decisions in the current input stream bytes based on future bytes.

I know all about mark() and reset() methods, but they just don't work one any InputStream, and support in BufferedInputStream in limited (the maked position is lost each time the internal buffer is filled...).

So, my question is: how can I return to a given input stream position, from a subsequent position, when I only have an InputStream as an argument?

Thanks a lot in advance!

A. Pinto

[630 byte] By [apintoa] at [2007-11-27 6:16:21]
# 1
http://java.sun.com/j2se/1.4.2/docs/api/java/io/PushbackInputStream.html
dannyyatesa at 2007-7-12 17:27:56 > top of Java-index,Core,Core APIs...
# 2
Hi dannyyates, thanks for your reply.Unfortunately, PushbackInputStream isn't a viable option, because we can be talking about several MB of data (I'm processing large TIFF files, and the IFD can be, and often is, at the end of the file, after the image data).A. Pinto
apintoa at 2007-7-12 17:27:56 > top of Java-index,Core,Core APIs...
# 3

Then you have a number of other options...

Use a RandomAccessFile if the thing is on a disk.

Or use an NIO MappedByteBuffer (again, assumes it's on a disk).

If it's not on a disk (e.g. it's a stream coming back from a web server) then you're a bit screwed because it could be sending any amount of data and your memory is finite. In this case, you may need to place an artificial limit on the amount of data you can receive or else write the stream out to disk first and use one of the techniques above.

dannyyatesa at 2007-7-12 17:27:56 > top of Java-index,Core,Core APIs...