1
0
mirror of https://github.com/golang/go synced 2024-11-25 09:57:57 -07:00

bytes.Buffer: return error in WriteTo if buffer is not drained

R=rsc
CC=golang-dev
https://golang.org/cl/5642065
This commit is contained in:
Rob Pike 2012-02-09 08:58:40 +11:00
parent 3fce00d99e
commit c59dc485cd

View File

@ -182,14 +182,21 @@ func makeSlice(n int) []byte {
func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) {
b.lastRead = opInvalid
if b.off < len(b.buf) {
nBytes := b.Len()
m, e := w.Write(b.buf[b.off:])
if m > nBytes {
panic("bytes.Buffer.WriteTo: invalid Write count")
}
b.off += m
n = int64(m)
if e != nil {
return n, e
}
// otherwise all bytes were written, by definition of
// all bytes should have been written, by definition of
// Write method in io.Writer
if m != nBytes {
return n, io.ErrShortWrite
}
}
// Buffer is now empty; reset.
b.Truncate(0)