2018-08-24 04:56:38 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package streams
|
|
|
|
|
|
|
|
import "io"
|
|
|
|
|
|
|
|
// EOFAwareLimitReader holds reader and status of EOF
|
|
|
|
type EOFAwareLimitReader struct {
|
|
|
|
reader io.Reader
|
|
|
|
eof bool
|
2018-09-08 16:41:40 +01:00
|
|
|
err error
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// EOFAwareReader keeps track of the state, has the internal reader reached EOF
|
|
|
|
func EOFAwareReader(r io.Reader) *EOFAwareLimitReader {
|
2018-09-08 16:41:40 +01:00
|
|
|
return &EOFAwareLimitReader{reader: r}
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *EOFAwareLimitReader) Read(p []byte) (n int, err error) {
|
|
|
|
n, err = r.reader.Read(p)
|
|
|
|
if err == io.EOF {
|
|
|
|
r.eof = true
|
2018-09-08 16:41:40 +01:00
|
|
|
} else if err != nil && r.err == nil {
|
|
|
|
r.err = err
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *EOFAwareLimitReader) isEOF() bool {
|
|
|
|
return r.eof
|
|
|
|
}
|
2018-09-08 16:41:40 +01:00
|
|
|
|
|
|
|
func (r *EOFAwareLimitReader) hasError() bool {
|
|
|
|
return r.err != nil
|
|
|
|
}
|