1
0
mirror of https://github.com/golang/go synced 2024-09-23 15:30:17 -06:00

io: add (*SectionReader).Outer()

Fixes #61870
Updates #61727

Change-Id: Iaef9b59c402d68f6bf64be212db2b6746abe8900
Reviewed-on: https://go-review.googlesource.com/c/go/+/526855
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@google.com>
This commit is contained in:
Carlo Alberto Ferraris 2023-09-08 08:21:58 +09:00 committed by Gopher Robot
parent 5955a03e9c
commit 1ce8a3f081
3 changed files with 20 additions and 4 deletions

1
api/next/61870.txt Normal file
View File

@ -0,0 +1 @@
pkg io, method (*SectionReader) Outer() (ReaderAt, int64, int64) #61870

View File

@ -493,16 +493,17 @@ func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader {
// Assume we can read up to an offset of 1<<63 - 1.
remaining = maxint64
}
return &SectionReader{r, off, off, remaining}
return &SectionReader{r, off, off, remaining, n}
}
// SectionReader implements Read, Seek, and ReadAt on a section
// of an underlying [ReaderAt].
type SectionReader struct {
r ReaderAt
base int64
r ReaderAt // constant after creation
base int64 // constant after creation
off int64
limit int64
limit int64 // constant after creation
n int64 // constant after creation
}
func (s *SectionReader) Read(p []byte) (n int, err error) {
@ -557,6 +558,14 @@ func (s *SectionReader) ReadAt(p []byte, off int64) (n int, err error) {
// Size returns the size of the section in bytes.
func (s *SectionReader) Size() int64 { return s.limit - s.base }
// Outer returns the underlying ReaderAt and offsets for the section.
//
// The returned values are the same that were passed to NewSectionReader
// when the SectionReader was created.
func (s *SectionReader) Outer() (r ReaderAt, off int64, n int64) {
return s.r, s.base, s.n
}
// An OffsetWriter maps writes at offset base to offset base+off in the underlying writer.
type OffsetWriter struct {
w WriterAt

View File

@ -384,6 +384,9 @@ func TestSectionReader_ReadAt(t *testing.T) {
if n, err := s.ReadAt(buf, int64(tt.at)); n != len(tt.exp) || string(buf[:n]) != tt.exp || err != tt.err {
t.Fatalf("%d: ReadAt(%d) = %q, %v; expected %q, %v", i, tt.at, buf[:n], err, tt.exp, tt.err)
}
if _r, off, n := s.Outer(); _r != r || off != int64(tt.off) || n != int64(tt.n) {
t.Fatalf("%d: Outer() = %v, %d, %d; expected %v, %d, %d", i, _r, off, n, r, tt.off, tt.n)
}
}
}
@ -445,6 +448,9 @@ func TestSectionReader_Max(t *testing.T) {
if n != 0 || err != EOF {
t.Errorf("Read = %v, %v, want 0, EOF", n, err)
}
if _r, off, n := sr.Outer(); _r != r || off != 3 || n != maxint64 {
t.Fatalf("Outer = %v, %d, %d; expected %v, %d, %d", _r, off, n, r, 3, int64(maxint64))
}
}
// largeWriter returns an invalid count that is larger than the number