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()
|
|
|
|
}
|
2021-11-23 12:05:49 +00:00
|
|
|
|
|
|
|
type seekerWrapper struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
pos int64
|
|
|
|
size int64
|
|
|
|
|
|
|
|
io.Seeker
|
|
|
|
Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSeekerWrapper(r Reader, size int64) *seekerWrapper {
|
|
|
|
return &seekerWrapper{Reader: r}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *seekerWrapper) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
|
|
|
|
switch whence {
|
|
|
|
case io.SeekStart:
|
|
|
|
r.pos = offset
|
|
|
|
case io.SeekCurrent:
|
|
|
|
r.pos = r.pos + offset
|
|
|
|
case io.SeekEnd:
|
|
|
|
r.pos = r.size + offset
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.pos, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *seekerWrapper) Read(p []byte) (int, error) {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
|
|
|
|
n, err := r.ReadAt(p, r.pos)
|
|
|
|
r.pos += int64(n)
|
|
|
|
|
|
|
|
return n, err
|
|
|
|
}
|