diff --git a/reader.go b/reader.go index 9712fc8..e292b01 100644 --- a/reader.go +++ b/reader.go @@ -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 { @@ -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() @@ -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 } diff --git a/reader_test.go b/reader_test.go index b46d943..cc1a683 100644 --- a/reader_test.go +++ b/reader_test.go @@ -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) {