edge/pkg/bundle/zim/uncompressed_blob_reader.go

64 lines
1.3 KiB
Go
Raw Normal View History

2023-10-11 11:18:32 +02:00
package zim
import (
"io"
"github.com/pkg/errors"
)
type UncompressedBlobReader struct {
reader *Reader
blobStartOffset uint64
blobEndOffset uint64
blobSize int
readOffset uint64
}
// Size implements BlobReader.
func (r *UncompressedBlobReader) Size() (int64, error) {
return int64(r.blobEndOffset - r.blobStartOffset), nil
}
// Close implements io.ReadCloser.
func (*UncompressedBlobReader) Close() error {
return nil
}
// Read implements io.ReadCloser.
func (r *UncompressedBlobReader) Read(p []byte) (n int, err error) {
len := len(p)
remaining := int(r.blobEndOffset - (r.blobStartOffset + r.readOffset))
if len > remaining {
len = remaining
}
data := make([]byte, len)
err = r.reader.readRange(int64(r.blobStartOffset+r.readOffset), data)
if err != nil {
return len, errors.WithStack(err)
}
r.readOffset += uint64(len)
copy(p, data)
if len == remaining {
return len, io.EOF
}
return len, nil
}
func NewUncompressedBlobReader(reader *Reader, blobStartOffset, blobEndOffset uint64, blobSize int) *UncompressedBlobReader {
return &UncompressedBlobReader{
reader: reader,
blobStartOffset: blobStartOffset,
blobEndOffset: blobEndOffset,
blobSize: blobSize,
readOffset: 0,
}
}
var _ BlobReader = &UncompressedBlobReader{}