State of io.Reader after reading a surrounding bufio.Reader

213 views
Skip to first unread message

Scott Pakin

unread,
Feb 9, 2015, 5:12:37 PM2/9/15
to golan...@googlegroups.com
To implement a particular interface from the standard library (i.e., one I didn't define myself), a method I'm writing needs to accept an io.Reader.  However, I'd like to be able to exploit all the nice methods defined on bufio.Reader.  I just don't want the caller to know or care that my function does that.  What should I do?  I'm assuming that there are no guarantees about the state of the underlying io.Reader after reading from a bufio.Reader that wraps it, but I'd be happy to be wrong about that.

— Scott

Dave Cheney

unread,
Feb 9, 2015, 6:20:08 PM2/9/15
to golan...@googlegroups.com
There are no guarantees provided. bufio.Reader may consume some, none, or all of the contents of the underlying reader.

Marvin Renich

unread,
Feb 9, 2015, 8:05:53 PM2/9/15
to golan...@googlegroups.com
> On Tuesday, 10 February 2015 09:12:37 UTC+11, Scott Pakin wrote:
> > To implement a particular interface from the standard library (i.e., one I
> > didn't define myself), a method I'm writing needs to accept an io.Reader.
> > However, I'd like to be able to exploit all the nice methods defined on
> > bufio.Reader. I just don't want the caller to know or care that my
> > function does that. What should I do? I'm assuming that there are no
> > guarantees about the state of the underlying io.Reader after reading from
> > a bufio.Reader that wraps it, but I'd be happy to be wrong about that.

* Dave Cheney <da...@cheney.net> [150209 18:20]:
> There are no guarantees provided. bufio.Reader may consume some, none, or
> all of the contents of the underlying reader.

However, if the caller might pass in a bufio.Reader, and you are happy
to take advantage of that if so, but do without if not, you can always
check with something like:

var b, ok = r.(*bufio.Reader)

and then use b if ok is true.

...Marvin

Edward Muller

unread,
Feb 10, 2015, 1:41:53 AM2/10/15
to Scott Pakin, golan...@googlegroups.com
If you are passed in an io.Reader, you could just wrap it in a bufio.Reader with bufio.NewReader(passedInReader).

This way you always have a bufio.Reader to work with.

On Monday, February 9, 2015, Scott Pakin <scot...@pakin.org> wrote:
To implement a particular interface from the standard library (i.e., one I didn't define myself), a method I'm writing needs to accept an io.Reader.  However, I'd like to be able to exploit all the nice methods defined on bufio.Reader.  I just don't want the caller to know or care that my function does that.  What should I do?  I'm assuming that there are no guarantees about the state of the underlying io.Reader after reading from a bufio.Reader that wraps it, but I'd be happy to be wrong about that.

— Scott

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Edward Muller
@freeformz

Scott Pakin

unread,
Feb 10, 2015, 3:21:39 PM2/10/15
to golan...@googlegroups.com, mr...@renich.org
On Monday, February 9, 2015 at 6:05:53 PM UTC-7, Marvin Renich wrote:
However, if the caller might pass in a bufio.Reader, and you are happy
to take advantage of that if so, but do without if not, you can always
check with something like:

    var b, ok = r.(*bufio.Reader)

and then use b if ok is true.

Yeah, that's what I had.  I guess I'll just have to document that if the caller passes in a bufio.Reader, they'll be able to continue using it afterwards without issue, but if they pass in an ordinary io.Reader, all bets are off.

Thanks, guys,
— Scott

Brad Fitzpatrick

unread,
Feb 10, 2015, 4:05:41 PM2/10/15
to Scott Pakin, golang-nuts, mr...@renich.org
Yup. Or do something like http://golang.org/pkg/encoding/json/#Decoder.Buffered and then the caller can do io.MultiReader(yourThingy.Buffered(), theirReader) to concatenate the two readers together.
 

Scott Pakin

unread,
Feb 11, 2015, 3:21:09 PM2/11/15
to golan...@googlegroups.com, scot...@pakin.org, mr...@renich.org
On Tuesday, February 10, 2015 at 2:05:41 PM UTC-7, bradfitz wrote:
Yup. Or do something like http://golang.org/pkg/encoding/json/#Decoder.Buffered and then the caller can do io.MultiReader(yourThingy.Buffered(), theirReader) to concatenate the two readers together.

That's a nice, simple solution.  I like it.

Thanks,
— Scott 

Brad Fitzpatrick

unread,
Feb 11, 2015, 3:27:15 PM2/11/15
to Scott Pakin, golang-nuts, mr...@renich.org
That's how we roll. :)

Reply all
Reply to author
Forward
0 new messages