Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
0f26608e
Commit
0f26608e
authored
Jan 05, 2011
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
io: fix Copyn EOF handling
Fixes #1383. R=r CC=golang-dev
https://golang.org/cl/3821044
parent
189d4ff7
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
1 deletion
+42
-1
io.go
src/pkg/io/io.go
+6
-1
io_test.go
src/pkg/io/io_test.go
+36
-0
No files found.
src/pkg/io/io.go
View file @
0f26608e
...
...
@@ -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
{
...
...
src/pkg/io/io_test.go
View file @
0f26608e
...
...
@@ -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"
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment