aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/memory.go
diff options
context:
space:
mode:
authorKane York <kanepyork@gmail.com>2020-07-04 19:58:38 -0700
committerKane York <kanepyork@gmail.com>2020-07-06 15:34:45 -0700
commitd0d47c3781674db571b18df0544f66d93decf342 (patch)
treeaf4ca137def644d02a1ad6183980c403a782a7c7 /plumbing/memory.go
parent8019144b6534ff58ad234a355e5b143f1c99b45e (diff)
downloadgo-git-d0d47c3781674db571b18df0544f66d93decf342.tar.gz
memoryobject: make blob reader seekable
Replace the bytes.Buffer with a bytes.Reader wrapped in a custom NopCloser, so that the extra reading methods are sill accessible.
Diffstat (limited to 'plumbing/memory.go')
-rw-r--r--plumbing/memory.go17
1 files changed, 14 insertions, 3 deletions
diff --git a/plumbing/memory.go b/plumbing/memory.go
index b8e1e1b..21337cc 100644
--- a/plumbing/memory.go
+++ b/plumbing/memory.go
@@ -3,7 +3,6 @@ package plumbing
import (
"bytes"
"io"
- "io/ioutil"
)
// MemoryObject on memory Object implementation
@@ -39,9 +38,11 @@ func (o *MemoryObject) Size() int64 { return o.sz }
// afterwards
func (o *MemoryObject) SetSize(s int64) { o.sz = s }
-// Reader returns a ObjectReader used to read the object's content.
+// Reader returns an io.ReadCloser used to read the object's content.
+//
+// For a MemoryObject, this reader is seekable.
func (o *MemoryObject) Reader() (io.ReadCloser, error) {
- return ioutil.NopCloser(bytes.NewBuffer(o.cont)), nil
+ return nopCloser{bytes.NewReader(o.cont)}, nil
}
// Writer returns a ObjectWriter used to write the object's content.
@@ -59,3 +60,13 @@ func (o *MemoryObject) Write(p []byte) (n int, err error) {
// Close releases any resources consumed by the object when it is acting as a
// ObjectWriter.
func (o *MemoryObject) Close() error { return nil }
+
+// nopCloser exposes the extra methods of bytes.Reader while nopping Close().
+//
+// This allows clients to attempt seeking in a cached Blob's Reader.
+type nopCloser struct {
+ *bytes.Reader
+}
+
+// Close does nothing.
+func (nc nopCloser) Close() error { return nil }