mirror of
https://github.com/mjl-/mox.git
synced 2024-12-26 16:33:47 +03:00
28 lines
558 B
Go
28 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
|
||
|
}
|