jldb/lib/testutil/limitwriter.go

34 lines
442 B
Go
Raw Permalink Normal View History

2023-10-13 09:43:27 +00:00
package testutil
import (
"io"
"os"
)
func NewLimitWriter(w io.Writer, limit int) *LimitWriter {
return &LimitWriter{
w: w,
limit: limit,
}
}
type LimitWriter struct {
w io.Writer
limit int
written int
}
func (lw *LimitWriter) Write(buf []byte) (int, error) {
n, err := lw.w.Write(buf)
if err != nil {
return n, err
}
lw.written += n
if lw.written > lw.limit {
return n, os.ErrClosed
}
return n, nil
}