mox/moxio/limitreader.go
Mechiel Lukkien cb229cb6cf
mox!
2023-01-30 14:27:06 +01:00

27 lines
558 B
Go

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
}