Skip to content

Commit

Permalink
Merge pull request #16 from jarxorg/fix/without-reset-tails
Browse files Browse the repository at this point in the history
Introduce resetTails
  • Loading branch information
mojatter authored Apr 1, 2022
2 parents b899494 + 3cf4d3f commit 326f3a2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
17 changes: 17 additions & 0 deletions multireader.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ func (mr *multiReader) Seek(offset int64, whence int) (int64, error) {
return 0, errors.New("invalid whence")
}

func (mr *multiReader) resetTails() error {
for i := mr.off + 1; i < len(mr.rs); i++ {
r := mr.rs[i]
if _, err := r.Seek(0, io.SeekStart); err != nil {
return err
}
r.off = 0
}
return nil
}

func (mr *multiReader) seekStart(offset int64, start int) (int64, error) {
off := int64(0)
for i := 0; i < start; i++ {
Expand All @@ -156,6 +167,9 @@ func (mr *multiReader) seekStart(offset int64, start int) (int64, error) {
if err != nil {
return 0, err
}
if err := mr.resetTails(); err != nil {
return 0, err
}
return off, nil
}

Expand Down Expand Up @@ -187,6 +201,9 @@ func (mr *multiReader) seekCurrent(offset int64) (int64, error) {
}
return mr.seekEnd(diffset, mr.off-1)
}
if err := mr.resetTails(); err != nil {
return 0, err
}
return off + n, nil
}

Expand Down
44 changes: 44 additions & 0 deletions multireader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,33 @@ func TestMultiSeek(t *testing.T) {
whence: io.SeekStart,
n: int64(4),
after: "ef",
}, {
reader: func() io.ReadSeekCloser {
r := testMultiReaderStrings(t, "abc", "def")
ioutil.ReadAll(r)
return r
},
offset: int64(0),
whence: io.SeekStart,
n: int64(0),
after: "abcdef",
}, {
reader: func() io.ReadSeekCloser {
r0 := NopReadSeekCloser(strings.NewReader("abc"))
r1 := strings.NewReader("def")
d1 := Delegate(r1)
r, err := MultiReadSeekCloser(r0, d1)
if err != nil {
t.Fatal(err)
}
d1.SeekFunc = func(offset int64, whence int) (int64, error) {
return 0, errors.New("failed to reset tails")
}
return r
},
offset: int64(0),
whence: io.SeekStart,
errstr: "failed to reset tails",
}, {
reader: func() io.ReadSeekCloser {
// NOTE: Check single reader.
Expand Down Expand Up @@ -204,6 +231,23 @@ func TestMultiSeek(t *testing.T) {
whence: io.SeekCurrent,
n: int64(0),
after: "abcdefghi",
}, {
reader: func() io.ReadSeekCloser {
r0 := NopReadSeekCloser(strings.NewReader("abc"))
r1 := strings.NewReader("def")
d1 := Delegate(r1)
r, err := MultiReadSeekCloser(r0, d1)
if err != nil {
t.Fatal(err)
}
d1.SeekFunc = func(offset int64, whence int) (int64, error) {
return 0, errors.New("failed to reset tails")
}
return r
},
offset: int64(0),
whence: io.SeekCurrent,
errstr: "failed to reset tails",
}, {
reader: func() io.ReadSeekCloser {
// NOTE: Check single reader.
Expand Down

0 comments on commit 326f3a2

Please sign in to comment.