2018-01-23 03:44:55 +08:00
|
|
|
package readers
|
|
|
|
|
|
|
|
import "io"
|
|
|
|
|
|
|
|
// LimitedReadCloser adds io.Closer to io.LimitedReader. Create one with NewLimitedReadCloser
|
|
|
|
type LimitedReadCloser struct {
|
|
|
|
*io.LimitedReader
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLimitedReadCloser returns a LimitedReadCloser wrapping rc to
|
2018-01-27 18:07:17 +08:00
|
|
|
// limit it to reading limit bytes. If limit < 0 then it does not
|
2018-01-23 03:44:55 +08:00
|
|
|
// wrap rc, it just returns it.
|
|
|
|
func NewLimitedReadCloser(rc io.ReadCloser, limit int64) (lrc io.ReadCloser) {
|
2018-01-27 18:07:17 +08:00
|
|
|
if limit < 0 {
|
2018-01-23 03:44:55 +08:00
|
|
|
return rc
|
|
|
|
}
|
|
|
|
return &LimitedReadCloser{
|
|
|
|
LimitedReader: &io.LimitedReader{R: rc, N: limit},
|
|
|
|
Closer: rc,
|
|
|
|
}
|
|
|
|
}
|