2

I need to “un-read” characters from an InputStreamReader. For that purpose I wanted to use mark and reset but markSupported returns false for the InputStreamReader class, since it doesn’t maintain an internal buffer and/or queue of characters.

I know about BufferedInputStream and PushbackInputStream but neither is appropriate here since they buffer on byte basis, while I need characters.

Does Java offer a buffered character reader which can un-read characters? Actually, let me constrain that further, I only ever need to un-read a single character (for lookahead purposes). Do I really need to maintain my own lookahead?

Community
  • 1
  • 1
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214

3 Answers3

7

The two byte-stream based classes java.io.BufferedInputStream and java.io.PushbackInputStream have their character-stream based counterparts in the same package:

java.io.PushbackReader
java.io.BufferedReader
mhaller
  • 14,122
  • 1
  • 42
  • 61
  • Thanks a lot. I’m still not at home in the stream class naming. Neither in .NET, for that matter, even though I’ve been using the .NET classes for well over seven years now. – Konrad Rudolph Nov 03 '09 at 17:27
3

Have you tried java.io.BufferedReader?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

You could wrap the input stream using a BufferedReader

Reader markedReader = new BufferedReader(inputStreamReader) ;

The buffered reader does support mark and reads characters.

rsp
  • 23,135
  • 6
  • 55
  • 69