Commit 24a78a02 authored by Roger Peppe's avatar Roger Peppe Committed by Russ Cox

bufio: make Reader.Read implement io.Reader semantics

R=rsc
CC=golang-dev
https://golang.org/cl/3395042
parent cf6c2121
...@@ -128,16 +128,17 @@ func (b *Reader) Peek(n int) ([]byte, os.Error) { ...@@ -128,16 +128,17 @@ func (b *Reader) Peek(n int) ([]byte, os.Error) {
// Read reads data into p. // Read reads data into p.
// It returns the number of bytes read into p. // It returns the number of bytes read into p.
// If nn < len(p), also returns an error explaining // It calls Read at most once on the underlying Reader,
// why the read is short. At EOF, the count will be // hence n may be less than len(p).
// zero and err will be os.EOF. // At EOF, the count will be zero and err will be os.EOF.
func (b *Reader) Read(p []byte) (nn int, err os.Error) { func (b *Reader) Read(p []byte) (n int, err os.Error) {
nn = 0 n = len(p)
for len(p) > 0 { if n == 0 {
n := len(p) return 0, b.err
}
if b.w == b.r { if b.w == b.r {
if b.err != nil { if b.err != nil {
return nn, b.err return 0, b.err
} }
if len(p) >= len(b.buf) { if len(p) >= len(b.buf) {
// Large read, empty buffer. // Large read, empty buffer.
...@@ -148,12 +149,14 @@ func (b *Reader) Read(p []byte) (nn int, err os.Error) { ...@@ -148,12 +149,14 @@ func (b *Reader) Read(p []byte) (nn int, err os.Error) {
b.lastRuneSize = -1 b.lastRuneSize = -1
} }
p = p[n:] p = p[n:]
nn += n return n, b.err
continue
} }
b.fill() b.fill()
continue if b.w == b.r {
return 0, b.err
} }
}
if n > b.w-b.r { if n > b.w-b.r {
n = b.w - b.r n = b.w - b.r
} }
...@@ -162,9 +165,7 @@ func (b *Reader) Read(p []byte) (nn int, err os.Error) { ...@@ -162,9 +165,7 @@ func (b *Reader) Read(p []byte) (nn int, err os.Error) {
b.r += n b.r += n
b.lastByte = int(b.buf[b.r-1]) b.lastByte = int(b.buf[b.r-1])
b.lastRuneSize = -1 b.lastRuneSize = -1
nn += n return n, nil
}
return nn, nil
} }
// ReadByte reads and returns a single byte. // ReadByte reads and returns a single byte.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment