1
0
mirror of https://github.com/golang/go synced 2024-11-22 04:14:42 -07:00

io: fix Copyn EOF handling

Fixes #1383.

R=r
CC=golang-dev
https://golang.org/cl/3821044
This commit is contained in:
Russ Cox 2011-01-05 14:35:13 -05:00
parent 189d4ff776
commit 0f26608ebc
2 changed files with 42 additions and 1 deletions

View File

@ -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. // If the writer has a ReadFrom method, use it to do the copy.
// Avoids a buffer allocation and a copy. // Avoids a buffer allocation and a copy.
if rt, ok := dst.(ReaderFrom); ok { 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) buf := make([]byte, 32*1024)
for written < n { for written < n {

View File

@ -8,6 +8,7 @@ import (
"bytes" "bytes"
. "io" . "io"
"os" "os"
"strings"
"testing" "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) { func TestReadAtLeast(t *testing.T) {
var rb bytes.Buffer var rb bytes.Buffer
rb.Write([]byte("0123")) rb.Write([]byte("0123"))