2018-01-12 17:30:54 +01:00
|
|
|
package readers
|
2017-04-05 18:19:26 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2018-11-12 13:02:05 +01:00
|
|
|
"sync"
|
2017-04-05 18:19:26 +02:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A RepeatableReader implements the io.ReadSeeker it allow to seek cached data
|
|
|
|
// back and forth within the reader but will only read data from the internal Reader as necessary
|
|
|
|
// and will play nicely with the Account and io.LimitedReader to reflect current speed
|
|
|
|
type RepeatableReader struct {
|
2018-11-12 13:02:05 +01:00
|
|
|
mu sync.Mutex // protect against concurrent use
|
|
|
|
in io.Reader // Input reader
|
|
|
|
i int64 // current reading index
|
|
|
|
b []byte // internal cache buffer
|
2017-04-05 18:19:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ io.ReadSeeker = (*RepeatableReader)(nil)
|
|
|
|
|
|
|
|
// Seek implements the io.Seeker interface.
|
|
|
|
// If seek position is passed the cache buffer length the function will return
|
|
|
|
// the maximum offset that can be used and "fs.RepeatableReader.Seek: offset is unavailable" Error
|
|
|
|
func (r *RepeatableReader) Seek(offset int64, whence int) (int64, error) {
|
2018-11-12 13:02:05 +01:00
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
|
2017-04-05 18:19:26 +02:00
|
|
|
var abs int64
|
|
|
|
cacheLen := int64(len(r.b))
|
|
|
|
switch whence {
|
2018-04-06 20:53:06 +02:00
|
|
|
case io.SeekStart:
|
2017-04-05 18:19:26 +02:00
|
|
|
abs = offset
|
2018-04-06 20:53:06 +02:00
|
|
|
case io.SeekCurrent:
|
2017-04-05 18:19:26 +02:00
|
|
|
abs = r.i + offset
|
2018-04-06 20:53:06 +02:00
|
|
|
case io.SeekEnd:
|
2017-04-05 18:19:26 +02:00
|
|
|
abs = cacheLen + offset
|
|
|
|
default:
|
|
|
|
return 0, errors.New("fs.RepeatableReader.Seek: invalid whence")
|
|
|
|
}
|
|
|
|
if abs < 0 {
|
|
|
|
return 0, errors.New("fs.RepeatableReader.Seek: negative position")
|
|
|
|
}
|
|
|
|
if abs > cacheLen {
|
|
|
|
return offset - (abs - cacheLen), errors.New("fs.RepeatableReader.Seek: offset is unavailable")
|
|
|
|
}
|
|
|
|
r.i = abs
|
|
|
|
return abs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read data from original Reader into bytes
|
|
|
|
// Data is either served from the underlying Reader or from cache if was already read
|
|
|
|
func (r *RepeatableReader) Read(b []byte) (n int, err error) {
|
2018-11-12 13:02:05 +01:00
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
|
2017-04-05 18:19:26 +02:00
|
|
|
cacheLen := int64(len(r.b))
|
|
|
|
if r.i == cacheLen {
|
|
|
|
n, err = r.in.Read(b)
|
|
|
|
if n > 0 {
|
|
|
|
r.b = append(r.b, b[:n]...)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
n = copy(b, r.b[r.i:])
|
|
|
|
}
|
|
|
|
r.i += int64(n)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRepeatableReader create new repeatable reader from Reader r
|
|
|
|
func NewRepeatableReader(r io.Reader) *RepeatableReader {
|
|
|
|
return &RepeatableReader{in: r}
|
|
|
|
}
|
2017-11-23 11:59:28 +01:00
|
|
|
|
|
|
|
// NewRepeatableReaderSized create new repeatable reader from Reader r
|
|
|
|
// with an initial buffer of size.
|
|
|
|
func NewRepeatableReaderSized(r io.Reader, size int) *RepeatableReader {
|
|
|
|
return &RepeatableReader{
|
|
|
|
in: r,
|
|
|
|
b: make([]byte, 0, size),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRepeatableLimitReader create new repeatable reader from Reader r
|
2020-05-20 12:39:20 +02:00
|
|
|
// with an initial buffer of size wrapped in an io.LimitReader to read
|
2017-11-23 11:59:28 +01:00
|
|
|
// only size.
|
|
|
|
func NewRepeatableLimitReader(r io.Reader, size int) *RepeatableReader {
|
|
|
|
return NewRepeatableReaderSized(io.LimitReader(r, int64(size)), size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRepeatableReaderBuffer create new repeatable reader from Reader r
|
|
|
|
// using the buffer passed in.
|
|
|
|
func NewRepeatableReaderBuffer(r io.Reader, buf []byte) *RepeatableReader {
|
|
|
|
return &RepeatableReader{
|
|
|
|
in: r,
|
|
|
|
b: buf[:0],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRepeatableLimitReaderBuffer create new repeatable reader from
|
2020-05-20 12:39:20 +02:00
|
|
|
// Reader r and buf wrapped in an io.LimitReader to read only size.
|
2017-11-23 11:59:28 +01:00
|
|
|
func NewRepeatableLimitReaderBuffer(r io.Reader, buf []byte, size int64) *RepeatableReader {
|
2019-01-11 18:17:46 +01:00
|
|
|
return NewRepeatableReaderBuffer(io.LimitReader(r, size), buf)
|
2017-11-23 11:59:28 +01:00
|
|
|
}
|