]> Cypherpunks.ru repositories - gostls13.git/commitdiff
io: add (*SectionReader).Outer()
authorCarlo Alberto Ferraris <cafxx@strayorange.com>
Thu, 7 Sep 2023 23:21:58 +0000 (08:21 +0900)
committerGopher Robot <gobot@golang.org>
Mon, 9 Oct 2023 22:04:41 +0000 (22:04 +0000)
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>

api/next/61870.txt [new file with mode: 0644]
src/io/io.go
src/io/io_test.go

diff --git a/api/next/61870.txt b/api/next/61870.txt
new file mode 100644 (file)
index 0000000..27bb9f6
--- /dev/null
@@ -0,0 +1 @@
+pkg io, method (*SectionReader) Outer() (ReaderAt, int64, int64) #61870
index c2e1fa0cb0544d767b9a3fd6e6076491c8e804d9..a383f2f309d6f2f1a420dde362a49ec2613274f6 100644 (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
index c09b5e34d28613d43f43db937b765fd7c6e6f856..9491ffae614c61603a1c914aa1ec15123a59b007 100644 (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