[2317] in java-interest

home help back first fref pref prev next nref lref last post

Re: peek() into a stream lately?

daemon@ATHENA.MIT.EDU (Jim Graham)
Fri Sep 29 08:33:30 1995

Date: Wed, 27 Sep 1995 15:44:42 -0700
From: flar@bendenweyr.Eng.Sun.COM (Jim Graham)
To: java-interest@java.Eng.Sun.COM, rethingt@lehman.com


> I've been digging through the various input stream classes looking
> for a way to peek() at my NetworkClient's serverInput stream without
> taking bytes out of it. PushbackInputStream looked promising until I
> saw the source (in 1.0Alpha3).

The way this is traditionally done is to wrap the stream in a
BufferedInputStream, as in:

	if (!input.markSupported()) {
		input = new BufferedInputStream(input);
	}
	input.mark(100);
	/* Read up to 100 bytes to peek at them. */
	input.reset();

Note that you can no longer access the original "input" object since
100 bytes were read out of it and stored in the BufferedInputStream
object and won't be available if you try to read from the original
object.  You should replace any occurance of the original input stream
with the new object so that the byte stream will appear untouched.

This technique is used to sniff streams for content type when there
is no MIME header.

				...jim
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post