Can I do sequential label operations in an InputStream in Java

I am trying to create a simple parser, and since the InputStream does not have any method similar to snooping, I use the sign and reset. But I suspect that consistent calls to celebrate, to cancel the previous ones. This is true? Is it possible to do something like

foo.mark(1); ... foo.mark(2); ... foo.reset(); ... foo.reset(); 

If not, is there any other way to simulate this or peek method?

thanks.

+4
source share
2 answers

Signs do not nest.

If you want to re-read the stream several times, you may need to copy (part) of the stream into a byte array and make it ByteArrayInputStream . You still cannot have multiple tags, but you can have multiple ByteArrayInputStream s. (Or just forget about ByteArrayInputStream and select bytes directly from the array.)

+1
source

Your suspicion is true, the InputStream.mark (int readlimit) method will allow you to move the stream only to the last marked position if you read fewer readlimit bytes. If you want a "peekable" InputStream, you might consider PushbackInputStream . It clearly does not offer the possibility of peek, but it will allow you to "discard" the bytes you read.

+4
source

All Articles