views:

1058

answers:

1

Hello

I'm reading from a Socket's InputStream. Because I'm parsing the incoming data on the fly, I'm required to read character by character.

Does BufferedReader.read() the same thing as InputStream.read() does ? (assuming that BufferedReader has been constructed with the InputStream as base)

Is it more efficient to use InputStream.read() when reading each character separately? Or is there any better way?

+4  A: 

BufferedReader will read multiple characters from an underlying Reader. An InputStream is providing bytes. So they're working on 2 distinct datatypes. How are you wrapping a Reader around a Stream ? Presumably you've go something like:

 BufferedReader in
   = new BufferedReader(new InputStreamReader(socket));

in which case I'd be careful to specify your character encoding.

From the perspective of optimisation, it would be better to use the BufferedReader, since it'll read several kilobytes at once, and you can take each character when you want (not necessarily forcing a new IO read).

Brian Agnew
thanks. then you advise me to use InputStream? I'm reading the stream until some special characters appear. Then I parse that piece and await the next "package". Is there any better way to do this?
Atmocreations
Note that a Stream will give you bytes. A Reader will give you characters. So you need to be clearer in terms of what you're receiving and what you need.
Brian Agnew