bufio.Reader.Buffered returns 0

444 views
Skip to first unread message

Archos

unread,
Aug 24, 2010, 5:50:04 PM8/24/10
to golang-nuts
Why bufio.Reader.Buffered() returns 0?

It should returns the number of bytes to read from buffer

Note that I've to read the first byte (ReadByte) for then to can use
Buffered()

* * *
package main

import (
"bufio"
"os"
"fmt"
)

func ChangeHeader(fname string) os.Error {
inputFile, err := os.Open(fname, os.O_RDWR, 0644)
if err != nil {
return err
}

defer inputFile.Close()

input := bufio.NewReader(inputFile)
println(input.Buffered()) // *** It returns 0

return nil
}


func main() {
var fname = "src"

ChangeHeader(fname)
}

Evan Shaw

unread,
Aug 24, 2010, 5:55:52 PM8/24/10
to Archos, golang-nuts
On Tue, Aug 24, 2010 at 4:50 PM, Archos <raul...@sent.com> wrote:
> Why bufio.Reader.Buffered() returns 0?
>
> It should returns the number of bytes to read from buffer
>
> Note that I've to read the first byte (ReadByte) for then to can use
> Buffered()

I think you're misunderstanding the documentation of the Buffered
method. It returns the number of bytes it's read into its buffer,
*not* the number of bytes that are in the underlying io.Reader. Since
you haven't read anything yet in your code, the bufio.Reader hasn't
read anything into its buffer.

- Evan

Archos

unread,
Aug 24, 2010, 6:17:08 PM8/24/10
to golang-nuts
Well, the doc. *on this case* didn't help me much:

"Buffered returns the number of bytes that can be read from the
current buffer."[1]

[1]: http://golang.org/pkg/bufio/#Reader.Buffered

On Aug 24, 9:55 pm, Evan Shaw <chicken...@gmail.com> wrote:

Evan Shaw

unread,
Aug 24, 2010, 6:21:20 PM8/24/10
to Archos, golang-nuts
On Tue, Aug 24, 2010 at 5:17 PM, Archos <raul...@sent.com> wrote:
> Well, the doc. *on this case* didn't help me much:
>
> "Buffered returns the number of bytes that can be read from the
> current buffer."[1]

I find that to be a pretty clear description. Consider that it could
be impossible or at least expensive to find the number of bytes in the
underlying io.Reader when all you have to go off of is the Read
method. How would you propose to do it with this type, for example:

type nullReader struct{}

func (r *nullReader) Read(p []byte) (int, os.Error) {
for i := range p {
p[i] = 0
}
return len(p), nil
}

- Evan

smog...@gmail.com

unread,
Dec 15, 2019, 10:57:06 PM12/15/19
to golang-nuts
I'm having a similar problem as the original poster. I expect Buffered() to return the number of bytes that can be read from the current buffer, like the documentation states. However a call to Buffered() returns 0 both before and after a call to any Read() that finds bytes in the buffer. In my case the Reader is a net.Conn. 

How do we use Buffered() properly? This is the only google result I could find that even mentions using the function.

I ended up solving my problem without using bufio.
   -Sam


On Tuesday, August 24, 2010 at 6:21:20 PM UTC-4, Evan Shaw wrote:

Ian Lance Taylor

unread,
Dec 15, 2019, 11:05:01 PM12/15/19
to smog...@gmail.com, golang-nuts
On Sun, Dec 15, 2019 at 7:56 PM <smog...@gmail.com> wrote:
>
> I'm having a similar problem as the original poster. I expect Buffered() to return the number of bytes that can be read from the current buffer, like the documentation states. However a call to Buffered() returns 0 both before and after a call to any Read() that finds bytes in the buffer. In my case the Reader is a net.Conn.
>
> How do we use Buffered() properly? This is the only google result I could find that even mentions using the function.
>
> I ended up solving my problem without using bufio.

Note that you are replying to an e-mail from over nine years ago.

The Buffered method is fairly special purpose. It just tells you how
many bytes you can Read without causing a call to the underlying
Reader. What are you really trying to do? Can you show us some code
that does not work as you expect?

Ian


> On Tuesday, August 24, 2010 at 6:21:20 PM UTC-4, Evan Shaw wrote:
>>
>> On Tue, Aug 24, 2010 at 5:17 PM, Archos <rau...@sent.com> wrote:
>> > Well, the doc. *on this case* didn't help me much:
>> >
>> > "Buffered returns the number of bytes that can be read from the
>> > current buffer."[1]
>>
>> I find that to be a pretty clear description. Consider that it could
>> be impossible or at least expensive to find the number of bytes in the
>> underlying io.Reader when all you have to go off of is the Read
>> method. How would you propose to do it with this type, for example:
>>
>> type nullReader struct{}
>>
>> func (r *nullReader) Read(p []byte) (int, os.Error) {
>> for i := range p {
>> p[i] = 0
>> }
>> return len(p), nil
>> }
>>
>> - Evan
>
> --
> 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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/3a062f9c-27ab-4065-bd86-fa4e189acc9b%40googlegroups.com.

Marvin Renich

unread,
Dec 16, 2019, 7:27:57 AM12/16/19
to golang-nuts
* Ian Lance Taylor <ia...@golang.org> [191215 23:05]:
> The Buffered method [snip] tells you how
> many bytes you can Read without causing a call to the underlying
> Reader.

I think this would be a significant improvement over the current
ambiguous documentation. It should adequately dispel any unrealistic
expectations on the part of programmers who are reading the
documentation.

...Marvin

Sam

unread,
Dec 16, 2019, 2:35:14 PM12/16/19
to golang-nuts
Thanks Marvin, I think I understand now.
Sorry Ian I didn't even notice, but still...
I was using a net.Conn as the underlying reader. What I really needed was the Peek() function but the documentation for Buffered() made it seem more appropriate. I only needed to know the number of bytes received over the connection before evicting and interpreting them. 

intBytes, err := reader.Peek(4)
if err == nil {
// Decode integer
reader.Discard(4)
}

   -Sam

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/1smBsPOdFT0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/20191216122707.4nx3gueuibk4dpua%40basil.wdw.
Reply all
Reply to author
Forward
0 new messages