views:

637

answers:

2

I intend to use the markSupported feature for checking first byte to check the format then reset it, please tell me which stream should be used for tcp based communication in java. For most of the streams like datainputstream does not provide this feature. Please can give me some pointers to use mark and reset.

+4  A: 

You could use a BufferedInputStream around whatever other stream you've got. That's guaranteed to support mark/reset, so long as you don't try to read beyond the buffer size after marking.

Jon Skeet
+1 Why do I need a google search and a tangentially related question to solve a problem whose solution is intuitively named and documented? Ah the mysteries of the human mind....
David Berger
+6  A: 

Take a look at PushbackInputStream, which looks like it's designed to do what you want.

It'll wrap an existing input stream and allow you to read a byte, and then put it back. From the Javadoc:

A PushbackInputStream adds functionality to another input stream, namely the ability to "push back" or "unread" one byte. This is useful in situations where it is convenient for a fragment of code to read an indefinite number of data bytes that are delimited by a particular byte value; after reading the terminating byte, the code fragment can "unread" it, so that the next read operation on the input stream will reread the byte that was pushed back. For example, bytes representing the characters constituting an identifier might be terminated by a byte representing an operator character; a method whose job is to read just an identifier can read until it sees the operator and then push the operator back to be re-read.

Brian Agnew
never noticed this class!!! I like to learn!
dfa