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?
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).