Files
litestream/internal/limit_read_closer.go
Ben Johnson c2e884d4f0
Some checks failed
Commit / Lint (push) Has been cancelled
Commit / Build Windows (push) Has been cancelled
Commit / Build & Unit Test (push) Has been cancelled
Commit / Run S3 Mock Tests (push) Has been cancelled
Commit / Run NATS Integration Tests (push) Has been cancelled
Commit / Run S3 Integration Tests (push) Has been cancelled
Commit / Run GCP Integration Tests (push) Has been cancelled
Commit / Run Azure Blob Store Integration Tests (push) Has been cancelled
Commit / Run SFTP Integration Tests (push) Has been cancelled
Litestream VFS MVP (#721)
2025-08-20 17:35:04 -06:00

30 lines
558 B
Go

package internal
import "io"
// Copied from the io package to implement io.Closer.
func LimitReadCloser(r io.ReadCloser, n int64) io.ReadCloser {
return &LimitedReadCloser{r, n}
}
type LimitedReadCloser struct {
R io.ReadCloser // underlying reader
N int64 // max bytes remaining
}
func (l *LimitedReadCloser) Close() error {
return l.R.Close()
}
func (l *LimitedReadCloser) Read(p []byte) (n int, err error) {
if l.N <= 0 {
return 0, io.EOF
}
if int64(len(p)) > l.N {
p = p[0:l.N]
}
n, err = l.R.Read(p)
l.N -= int64(n)
return
}