Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to design/implement Reader with peek functionality  RSS feed

Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to implement a Reader which will periodically need to peek ahead at some of the data while reading it. As I see it, there are two ways I could implement this:

1. Using a mark(), read(), and reset() paradigm
2. Using a PushbackReader in which I read the data, and then unread() it

I was wondering if people had some input (no pun indented) as to any significant pros or cons of the two paradigms. Obviously with the first I need an underlying reader that supports marking -- I would likely use a BufferedReader. With the second I need to know ahead of time the largest number of characters I need to peek at so I can create the PushbackReader with a large enough buffer. That's not a problem. Other than those differences, I am hard pressed to see any pros or cons either way. Just as some more information, the reader will need to peek at 2 or 3 characters sometimes, and blocks of up to 200 characters at other times.

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!