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
|
|
|
|
}
|
|
|
|
|
|
|
|
return rw.Read(p)
|
|
|
|
}
|
2020-09-27 19:23:47 +00:00
|
|
|
|
|
|
|
func (rw *readAtWrapper) Close() error {
|
|
|
|
c, ok := rw.ReadSeeker.(io.Closer)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Close()
|
|
|
|
}
|