1
0
mirror of https://github.com/golang/go synced 2024-11-26 17:56:55 -07:00

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:
Russ Cox 2022-06-03 16:00:16 -04:00 committed by Gopher Robot
parent 21f05284c7
commit f8a53df314
3 changed files with 5 additions and 26 deletions

View File

@ -1 +0,0 @@
pkg io, type LimitedReader struct, Err error #51115

View File

@ -6,7 +6,6 @@ package io_test
import (
"bytes"
"errors"
"fmt"
"io"
"log"
@ -284,16 +283,3 @@ func ExampleReadAll() {
// Output:
// 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
}

View File

@ -455,26 +455,20 @@ func copyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error) {
// LimitReader returns a Reader that reads from r
// but stops with EOF after n bytes.
// 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
// data returned to just N bytes. Each call to Read
// updates N to reflect the new amount remaining.
// Read returns Err when N <= 0.
// If Err is nil, it returns EOF instead.
// Read returns EOF when N <= 0 or when the underlying R returns EOF.
type LimitedReader struct {
R Reader // underlying reader
N int64 // max bytes remaining
Err error // error to return on reaching the limit
R Reader // underlying reader
N int64 // max bytes remaining
}
func (l *LimitedReader) Read(p []byte) (n int, err error) {
if l.N <= 0 {
err := l.Err
if err == nil {
err = EOF
}
return 0, err
return 0, EOF
}
if int64(len(p)) > l.N {
p = p[0:l.N]