From ee2ecc4552d8fd2b29be29aed1fe81dca0df60f9 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 2 Jan 2015 10:02:15 -0800 Subject: bufio: add Reader.Discard Reader.Discard is the complement to Peek. It discards the next n bytes of input. We already have Reader.Buffered to see how many bytes of data are sitting available in memory, and Reader.Peek to get that that buffer directly. But once you're done with the Peek'd data, you can't get rid of it, other than Reading it. Both Read and io.CopyN(ioutil.Discard, bufReader, N) are relatively slow. People instead resort to multiple blind ReadByte calls, just to advance the internal b.r variable. I've wanted this previously, several people have asked for it in the past on golang-nuts/dev, and somebody just asked me for it again in a private email. There are a few places in the standard library we'd use it too. Change-Id: I85dfad47704a58bd42f6867adbc9e4e1792bc3b0 Reviewed-on: https://go-review.googlesource.com/2260 Reviewed-by: Russ Cox --- src/net/http/transfer.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/net/http') diff --git a/src/net/http/transfer.go b/src/net/http/transfer.go index fd9389adf0..c39d6cff67 100644 --- a/src/net/http/transfer.go +++ b/src/net/http/transfer.go @@ -638,8 +638,7 @@ func (b *body) readTrailer() error { // The common case, since nobody uses trailers. buf, err := b.r.Peek(2) if bytes.Equal(buf, singleCRLF) { - b.r.ReadByte() - b.r.ReadByte() + b.r.Discard(2) return nil } if len(buf) < 2 { -- cgit v1.3