Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce resetTails #16

Merged
merged 1 commit into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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