Skip to content

Commit

Permalink
Make read after close stateful.
Browse files Browse the repository at this point in the history
  • Loading branch information
klauspost committed Nov 5, 2019
1 parent d8c4cd0 commit 183268a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
7 changes: 7 additions & 0 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ func (a *reader) fill() (err error) {

// Read will return the next available data.
func (a *reader) Read(p []byte) (n int, err error) {
if a.err != nil {
return 0, a.err
}
// Swap buffer and maybe return error
err = a.fill()
if err != nil {
Expand Down Expand Up @@ -300,6 +303,9 @@ func (a *seekable) Seek(offset int64, whence int) (res int64, err error) {
// The return value n is the number of bytes written.
// Any error encountered during the write is also returned.
func (a *reader) WriteTo(w io.Writer) (n int64, err error) {
if a.err != nil {
return 0, a.err
}
n = 0
for {
err = a.fill()
Expand Down Expand Up @@ -338,6 +344,7 @@ func (a *reader) Close() (err error) {
a.closer = nil
return c.Close()
}
a.err = errors.New("readahead: read after Close")
return nil
}

Expand Down
5 changes: 5 additions & 0 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,11 @@ func TestWriteTo(t *testing.T) {
if err != nil {
t.Fatal("error when closing:", err)
}
// Test Read after close
_, err = io.Copy(dst, ar)
if err == nil {
t.Fatal("want error when closing, got:", err)
}
}

func TestNilReader(t *testing.T) {
Expand Down

0 comments on commit 183268a

Please sign in to comment.