2020-04-27 16:46:23 +00:00
|
|
|
package iio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type readAtWrapper struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
|
|
|
|
io.ReadSeeker
|
|
|
|
io.ReaderAt
|
2020-09-27 19:23:47 +00:00
|
|
|
io.Closer
|
2020-04-27 16:46:23 +00:00
|
|
|
}
|
|
|
|
|
2020-09-27 19:23:47 +00:00
|
|
|
func NewReadAtWrapper(r io.ReadSeeker) Reader {
|
2020-04-27 16:46:23 +00:00
|
|
|
return &readAtWrapper{ReadSeeker: r}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *readAtWrapper) ReadAt(p []byte, off int64) (int, error) {
|
|
|
|
rw.mu.Lock()
|
|
|
|
defer rw.mu.Unlock()
|
|
|
|
_, err := rw.Seek(off, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2021-04-25 09:00:27 +00:00
|
|
|
return io.ReadAtLeast(rw, p, len(p))
|
2020-04-27 16:46:23 +00:00
|
|
|
}
|
2020-09-27 19:23:47 +00:00
|
|
|
|
|
|
|
func (rw *readAtWrapper) Close() error {
|
2021-04-25 09:00:27 +00:00
|
|
|
rw.mu.Lock()
|
|
|
|
defer rw.mu.Unlock()
|
2020-09-27 19:23:47 +00:00
|
|
|
c, ok := rw.ReadSeeker.(io.Closer)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Close()
|
|
|
|
}
|