mox/moxio/limitreader.go

28 lines
558 B
Go
Raw Normal View History

2023-01-30 16:27:06 +03:00
package moxio
import (
"errors"
"io"
)
var ErrLimit = errors.New("input exceeds maximum size") // Returned by LimitReader.
// LimitReader reads up to Limit bytes, returning an error if more bytes are
// read. LimitReader can be used to enforce a maximum input length.
type LimitReader struct {
R io.Reader
Limit int64
}
// Read reads bytes from the underlying reader.
func (r *LimitReader) Read(buf []byte) (int, error) {
n, err := r.R.Read(buf)
if n > 0 {
r.Limit -= int64(n)
if r.Limit < 0 {
return 0, ErrLimit
}
}
return n, err
}