These are chat archives for anacrolix/torrent

26th
Oct 2017
Matt Joiner
@anacrolix
Oct 26 2017 03:07
@boramalper I'll do a quick test
Matt Joiner
@anacrolix
Oct 26 2017 03:15
@boramalper it seems to be working correctly, i think perhaps you're different behaviour from bytes.Buffer
it's worth noting that the bencode Decoder has a bufio.Reader internally, so it will read more than it needs, but will still return the correct results
Matt Joiner
@anacrolix
Oct 26 2017 03:22
it might need some work if that's undesirable for some circumstance
911423307d2ae0ffa555b323fb7414b7f427e915
anacrolix/torrent@9114233
Bora M. Alper
@boramalper
Oct 26 2017 09:17
@anacrolix "it might need some work" What would you suggest? Is there a way to bypass bufio.Reader?
Matt Joiner
@anacrolix
Oct 26 2017 11:46
is there some reason you can't afford the bencode decoder to consume ahead?
you might tee what's read into another buffer for reuse
i'm not sure how much of bufio.Reader is actually needed in the bencode package, it might get away without buffering internally
Bora M. Alper
@boramalper
Oct 26 2017 12:52
@anacrolix I need to get the exact size of the (bencoded form of the) first dictionary, so that I can slice it away and get the message I'm interested in. tee, unfortunately, doesn't give me that information.
Bora M. Alper
@boramalper
Oct 26 2017 12:58

I created this function in bencode/api.go:

func NewDecoder2(r interface {io.ByteScanner; io.Reader}) *Decoder {
    return &Decoder{r: r}
}

and I use it like this:

msgBuf := bytes.NewBuffer(msg)  // msg is []byte
rExtDict := new(extDict)  // extDict is a struct 
err := bencode.NewDecoder2(msgBuf).Decode(rExtDict)

and finally, when I need to get the unconsumed bytes, I can use msgBuf.Bytes() which returns the unconsumed bytes in the buffer.


I'd love to send a PR if you are also fine with that, though I think we need to come with a better name for the function, and the type of its single argument. =)