2018-01-22 20:44:55 +01:00
|
|
|
package readers
|
|
|
|
|
2024-01-03 13:21:08 +01:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
)
|
2018-01-22 20:44:55 +01:00
|
|
|
|
|
|
|
// LimitedReadCloser adds io.Closer to io.LimitedReader. Create one with NewLimitedReadCloser
|
|
|
|
type LimitedReadCloser struct {
|
|
|
|
*io.LimitedReader
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
2024-01-03 13:21:08 +01:00
|
|
|
// Close closes the underlying io.Closer. The error, if any, will be ignored if data is read completely
|
|
|
|
func (lrc *LimitedReadCloser) Close() error {
|
|
|
|
err := lrc.Closer.Close()
|
|
|
|
if err != nil && lrc.N == 0 {
|
|
|
|
fs.Debugf(nil, "ignoring close error because we already got all the data")
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-22 20:44:55 +01:00
|
|
|
// NewLimitedReadCloser returns a LimitedReadCloser wrapping rc to
|
2018-01-27 11:07:17 +01:00
|
|
|
// limit it to reading limit bytes. If limit < 0 then it does not
|
2018-01-22 20:44:55 +01:00
|
|
|
// wrap rc, it just returns it.
|
|
|
|
func NewLimitedReadCloser(rc io.ReadCloser, limit int64) (lrc io.ReadCloser) {
|
2018-01-27 11:07:17 +01:00
|
|
|
if limit < 0 {
|
2018-01-22 20:44:55 +01:00
|
|
|
return rc
|
|
|
|
}
|
|
|
|
return &LimitedReadCloser{
|
|
|
|
LimitedReader: &io.LimitedReader{R: rc, N: limit},
|
|
|
|
Closer: rc,
|
|
|
|
}
|
|
|
|
}
|