mirror of https://github.com/golang/go.git
io: revert: add an Err field to LimitedReader
We are having a hard time deciding the exact semantics of the Err field, and we need to ship the beta. So revert the Err field change; it can wait for Go 1.20. For #51115. This reverts CL 396215. Change-Id: I7719386567d3da10a614058a11f19dbccf304b4d Reviewed-on: https://go-review.googlesource.com/c/go/+/410133 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com> Auto-Submit: Russ Cox <rsc@golang.org>
This commit is contained in:
parent
21f05284c7
commit
f8a53df314
|
|
@ -1 +0,0 @@
|
||||||
pkg io, type LimitedReader struct, Err error #51115
|
|
||||||
|
|
@ -6,7 +6,6 @@ package io_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
|
@ -284,16 +283,3 @@ func ExampleReadAll() {
|
||||||
// Output:
|
// Output:
|
||||||
// Go is a general-purpose language designed with systems programming in mind.
|
// Go is a general-purpose language designed with systems programming in mind.
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleLimitedReader() {
|
|
||||||
r := strings.NewReader("some io.Reader stream to be read\n")
|
|
||||||
sentinel := errors.New("reached read limit")
|
|
||||||
lr := &io.LimitedReader{R: r, N: 4, Err: sentinel}
|
|
||||||
|
|
||||||
if _, err := io.Copy(os.Stdout, lr); err != sentinel {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Output:
|
|
||||||
// some
|
|
||||||
}
|
|
||||||
|
|
|
||||||
16
src/io/io.go
16
src/io/io.go
|
|
@ -455,26 +455,20 @@ func copyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error) {
|
||||||
// LimitReader returns a Reader that reads from r
|
// LimitReader returns a Reader that reads from r
|
||||||
// but stops with EOF after n bytes.
|
// but stops with EOF after n bytes.
|
||||||
// The underlying implementation is a *LimitedReader.
|
// The underlying implementation is a *LimitedReader.
|
||||||
func LimitReader(r Reader, n int64) Reader { return &LimitedReader{r, n, nil} }
|
func LimitReader(r Reader, n int64) Reader { return &LimitedReader{r, n} }
|
||||||
|
|
||||||
// A LimitedReader reads from R but limits the amount of
|
// A LimitedReader reads from R but limits the amount of
|
||||||
// data returned to just N bytes. Each call to Read
|
// data returned to just N bytes. Each call to Read
|
||||||
// updates N to reflect the new amount remaining.
|
// updates N to reflect the new amount remaining.
|
||||||
// Read returns Err when N <= 0.
|
// Read returns EOF when N <= 0 or when the underlying R returns EOF.
|
||||||
// If Err is nil, it returns EOF instead.
|
|
||||||
type LimitedReader struct {
|
type LimitedReader struct {
|
||||||
R Reader // underlying reader
|
R Reader // underlying reader
|
||||||
N int64 // max bytes remaining
|
N int64 // max bytes remaining
|
||||||
Err error // error to return on reaching the limit
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LimitedReader) Read(p []byte) (n int, err error) {
|
func (l *LimitedReader) Read(p []byte) (n int, err error) {
|
||||||
if l.N <= 0 {
|
if l.N <= 0 {
|
||||||
err := l.Err
|
return 0, EOF
|
||||||
if err == nil {
|
|
||||||
err = EOF
|
|
||||||
}
|
|
||||||
return 0, err
|
|
||||||
}
|
}
|
||||||
if int64(len(p)) > l.N {
|
if int64(len(p)) > l.N {
|
||||||
p = p[0:l.N]
|
p = p[0:l.N]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue