From 0f26608ebcfbe08aaf1ba2f3b0592cc5ce1feec1 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 5 Jan 2011 14:35:13 -0500 Subject: [PATCH] io: fix Copyn EOF handling Fixes #1383. R=r CC=golang-dev https://golang.org/cl/3821044 --- src/pkg/io/io.go | 7 ++++++- src/pkg/io/io_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/pkg/io/io.go b/src/pkg/io/io.go index fa1c0d2b064..1a6eca95a0d 100644 --- a/src/pkg/io/io.go +++ b/src/pkg/io/io.go @@ -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 { diff --git a/src/pkg/io/io_test.go b/src/pkg/io/io_test.go index 20f240a51a5..4fcd85e693e 100644 --- a/src/pkg/io/io_test.go +++ b/src/pkg/io/io_test.go @@ -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"))