Commit 0f26608e authored by Russ Cox's avatar Russ Cox

io: fix Copyn EOF handling

Fixes #1383.

R=r
CC=golang-dev
https://golang.org/cl/3821044
parent 189d4ff7
......@@ -206,7 +206,12 @@ func Copyn(dst Writer, src Reader, n int64) (written int64, err os.Error) {
// If the writer has a ReadFrom method, use it to do the copy.
// Avoids a buffer allocation and a copy.
if rt, ok := dst.(ReaderFrom); ok {
return rt.ReadFrom(LimitReader(src, n))
written, err = rt.ReadFrom(LimitReader(src, n))
if written < n && err == nil {
// rt stopped early; must have been EOF.
err = os.EOF
}
return
}
buf := make([]byte, 32*1024)
for written < n {
......
......@@ -8,6 +8,7 @@ import (
"bytes"
. "io"
"os"
"strings"
"testing"
)
......@@ -80,6 +81,41 @@ func TestCopynWriteTo(t *testing.T) {
}
}
type noReadFrom struct {
w Writer
}
func (w *noReadFrom) Write(p []byte) (n int, err os.Error) {
return w.w.Write(p)
}
func TestCopynEOF(t *testing.T) {
// Test that EOF behavior is the same regardless of whether
// argument to Copyn has ReadFrom.
b := new(bytes.Buffer)
n, err := Copyn(&noReadFrom{b}, strings.NewReader("foo"), 3)
if n != 3 || err != nil {
t.Errorf("Copyn(noReadFrom, foo, 3) = %d, %v; want 3, nil", n, err)
}
n, err = Copyn(&noReadFrom{b}, strings.NewReader("foo"), 4)
if n != 3 || err != os.EOF {
t.Errorf("Copyn(noReadFrom, foo, 4) = %d, %v; want 3, EOF", n, err)
}
n, err = Copyn(b, strings.NewReader("foo"), 3) // b has read from
if n != 3 || err != nil {
t.Errorf("Copyn(bytes.Buffer, foo, 3) = %d, %v; want 3, nil", n, err)
}
n, err = Copyn(b, strings.NewReader("foo"), 4) // b has read from
if n != 3 || err != os.EOF {
t.Errorf("Copyn(bytes.Buffer, foo, 4) = %d, %v; want 3, EOF", n, err)
}
}
func TestReadAtLeast(t *testing.T) {
var rb bytes.Buffer
rb.Write([]byte("0123"))
......
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