56 lines
837 B
Go
56 lines
837 B
Go
package cache
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
type Blob interface {
|
|
io.ReaderAt
|
|
io.Closer
|
|
Size() int64
|
|
}
|
|
|
|
type byteBlob struct {
|
|
buf *bytes.Reader
|
|
}
|
|
|
|
func NewByteBlob(b []byte) Blob {
|
|
return &byteBlob{buf: bytes.NewReader(b)}
|
|
}
|
|
|
|
func (blob *byteBlob) ReadAt(p []byte, off int64) (n int, err error) {
|
|
return blob.buf.ReadAt(p, off)
|
|
}
|
|
|
|
func (blob *byteBlob) Size() int64 {
|
|
return blob.buf.Size()
|
|
}
|
|
|
|
func (blob *byteBlob) Close() error {
|
|
return nil
|
|
}
|
|
|
|
type fileBlob struct {
|
|
buf *os.File
|
|
}
|
|
|
|
func NewFileBlob(f *os.File) Blob {
|
|
return &fileBlob{buf: f}
|
|
}
|
|
|
|
func (blob *fileBlob) ReadAt(p []byte, off int64) (n int, err error) {
|
|
return blob.buf.ReadAt(p, off)
|
|
}
|
|
|
|
func (blob *fileBlob) Size() int64 {
|
|
if i, err := blob.buf.Stat(); err != nil {
|
|
return i.Size()
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func (blob *fileBlob) Close() error {
|
|
return nil
|
|
}
|