tstor/pkg/ioutils/readerat.go
royalcat bd75492b02
Some checks failed
docker / build-docker (linux/386) (push) Failing after 18s
docker / build-docker (linux/amd64) (push) Failing after 17s
docker / build-docker (linux/arm/v7) (push) Failing after 17s
docker / build-docker (linux/arm64) (push) Failing after 17s
docker / build-docker (linux/arm64/v8) (push) Failing after 15s
refactor
2024-06-02 22:53:33 +03:00

49 lines
1 KiB
Go

package ioutils
import (
"context"
"sync"
"github.com/royalcat/ctxio"
)
type ReaderReaderAtWrapper struct {
mu sync.Mutex
rat ctxio.ReaderAt
offset int64
}
func NewReaderReaderAtWrapper(rat ctxio.ReaderAt) *ReaderReaderAtWrapper {
return &ReaderReaderAtWrapper{
rat: rat,
}
}
var _ ctxio.Reader = (*ReaderReaderAtWrapper)(nil)
var _ ctxio.ReaderAt = (*ReaderReaderAtWrapper)(nil)
var _ ctxio.Closer = (*ReaderReaderAtWrapper)(nil)
// Read implements Reader.
func (r *ReaderReaderAtWrapper) Read(ctx context.Context, p []byte) (n int, err error) {
r.mu.Lock()
defer r.mu.Unlock()
n, err = r.rat.ReadAt(ctx, p, r.offset)
r.offset += int64(n)
return n, err
}
// ReadAt implements ReaderAt.
func (r *ReaderReaderAtWrapper) ReadAt(ctx context.Context, p []byte, off int64) (n int, err error) {
return r.rat.ReadAt(ctx, p, off)
}
// Close implements Closer.
func (r *ReaderReaderAtWrapper) Close(ctx context.Context) (err error) {
if c, ok := r.rat.(ctxio.Closer); ok {
err = c.Close(ctx)
if err != nil {
return err
}
}
return nil
}