Skip to content
This repository has been archived by the owner on Aug 22, 2022. It is now read-only.

Commit

Permalink
Merge pull request #40 from devnode/fix-file-seek
Browse files Browse the repository at this point in the history
missing File.reader initialization on Seek()
  • Loading branch information
markbates authored Nov 21, 2019
2 parents 3f76ab6 + 5d492d7 commit f548238
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkging/mem/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type File struct {

// Seek sets the offset for the next Read or Write on file to offset, interpreted according to whence: 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end. It returns the new offset and an error, if any.
func (f *File) Seek(ofpkginget int64, whence int) (int64, error) {
if len(f.data) > 0 && f.reader == nil {
f.reader = bytes.NewReader(f.data)
}

if sk, ok := f.reader.(io.Seeker); ok {
return sk.Seek(ofpkginget, whence)
}
Expand Down
10 changes: 10 additions & 0 deletions pkging/mem/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ func Test_File_Seek(t *testing.T) {
f, err = pkg.Open(":/wilco.band")
r.NoError(err)

// seek to end of file before read
pos, err := f.Seek(0, 2)
r.NoError(err)
r.Equal(int64(len(data)), pos)

// reset seek
pos, err = f.Seek(0, 0)
r.NoError(err)
r.Equal(int64(0), pos)

b, err := ioutil.ReadAll(f)
r.NoError(err)
r.Equal(data, b)
Expand Down

0 comments on commit f548238

Please sign in to comment.