FAQ
Hi all,

Brief summary to my situation: I'm designing a new web protocol for my
university dissertation, and my experimental implementation is in Go. I
have mimicked the net/http API, since it's such a joy to use. The problem
I've encountered is in the file net/http/request.go, in the function
ReadRequest(r *bufio.Reader).

My problem is that if I use r.Peek(10), it returns 10 bytes as expected,
and this is the data which was sent. However, r.Buffered() returns 0 (both
before and after the call to r.Peek()), and calls to r.Read() and
r.ReadByte() block indefinitely.

I can appreciate that this has probably been caused by my wrecking the rest
of the library code, but I'm bemused at how r.Peek() can return data, while
the reader appears otherwise to contain no data.

Any ideas would be very welcome.

Many thanks

--

Search Discussions

  • Russ Cox at Nov 25, 2012 at 10:21 pm

    My problem is that if I use r.Peek(10), it returns 10 bytes as expected, and
    this is the data which was sent. However, r.Buffered() returns 0 (both
    before and after the call to r.Peek()),
    Can you post code demonstrating this? I suspect something else is going on.
    In this sequence:

    b, err := r.Peek(10)
    m := r.Buffered()

    I believe it is impossible to have m < len(b). However, it is
    certainly possible to have m < 10, such as if len(b) == 0.

    Russ

    --
  • Jamie Hall at Nov 25, 2012 at 10:36 pm
    The following pseudocode gives the results I described. Because I'm doing
    some processing on the data, that all happens in a function which contains
    the call to r.Peek. However, it gives this effect.

    fmt.Println("Buffered:", r.Buffered()) // 0
    b, err := r.Peek(1) // len(b) == 1
    fmt.Println(b) // Prints the data that was sent
    fmt.Println("Buffered:", r.Buffered()) // 0

    If it makes any difference, the data being sent is raw binary data.

    Thanks
    On Sunday, 25 November 2012 22:15:50 UTC, Russ Cox wrote:

    My problem is that if I use r.Peek(10), it returns 10 bytes as expected, and
    this is the data which was sent. However, r.Buffered() returns 0 (both
    before and after the call to r.Peek()),
    Can you post code demonstrating this? I suspect something else is going
    on.
    In this sequence:

    b, err := r.Peek(10)
    m := r.Buffered()

    I believe it is impossible to have m < len(b). However, it is
    certainly possible to have m < 10, such as if len(b) == 0.

    Russ
    --
  • Rémy Oudompheng at Nov 25, 2012 at 10:52 pm
    Le 25 nov. 2012 23:36, "Jamie Hall" <the.sly.marbo@googlemail.com> a écrit :
    The following pseudocode gives the results I described. Because I'm doing
    some processing on the data, that all happens in a function which contains
    the call to r.Peek. However, it gives this effect.
    fmt.Println("Buffered:", r.Buffered()) // 0
    b, err := r.Peek(1) // len(b) == 1
    fmt.Println(b) // Prints the data that was sent
    fmt.Println("Buffered:", r.Buffered()) // 0

    If it makes any difference, the data being sent is raw binary data.

    Thanks
    Can you give the actual code sequence and not only pseudocode?

    Rémy.

    --
  • Jamie Hall at Nov 25, 2012 at 10:58 pm
    The full code is as follows:

    // Type declarations
    type FirstByte byte

    func (reader DataStream) FirstByte() (FirstByte, error) {
    var first FirstByte
    if b, err := reader.Peek(1); err != nil {
    return 0, err
    } else {
    first = FirstByte(b[0])
    }

    return first, nil
    }

    type DataStream struct {
    bufio.Reader
    }

    func NewDataStream(r *bufio.Reader) DataStream {
    return DataStream{*r}
    }


    // Functional code
    fmt.Println("Buffered:", b.Buffered()) // 0

    data := NewDataStream(b)
    var firstByte FirstByte
    if f, err := data.FirstByte(); err != nil {
    return nil, err
    } else {
    firstByte = f
    }

    fmt.Println(firstByte) // | 0 1 0 0 0 0 0 1 | (the data I sent)
    fmt.Println("Buffered:", b.Buffered()) // 0
    On Sunday, 25 November 2012 22:52:57 UTC, Rémy Oudompheng wrote:


    Can you give the actual code sequence and not only pseudocode?

    Rémy.
    --
  • Rémy Oudompheng at Nov 25, 2012 at 11:07 pm
    Le 25 nov. 2012 23:58, "Jamie Hall" <the.sly.marbo@googlemail.com> a écrit :
    The full code is as follows:

    // Type declarations
    type FirstByte byte

    func (reader DataStream) FirstByte() (FirstByte, error) {
    var first FirstByte
    if b, err := reader.Peek(1); err != nil {
    return 0, err
    } else {
    first = FirstByte(b[0])
    }

    return first, nil
    }

    type DataStream struct {
    bufio.Reader
    }

    func NewDataStream(r *bufio.Reader) DataStream {
    return DataStream{*r}
    }
    You are using the bufio.Reader by value here. Any action you will take on
    it won't affect its state at all.

    Rémy.

    --
  • Jamie Hall at Nov 25, 2012 at 11:12 pm

    You are using the bufio.Reader by value here. Any action you will take on
    it won't affect its state at all.

    Rémy.
    This was exactly it; it's now working fine. Very silly mistake of mine.

    Many thanks =)

    (Absolutely *loving* Go, by the way)

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 24, '12 at 1:30a
activeNov 25, '12 at 11:12p
posts7
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase