Extract several packages to form a public API

This commit is contained in:
fox.cpp 2020-07-14 20:36:18 +03:00
parent 7d497f88f0
commit bcceec4fe4
No known key found for this signature in database
GPG key ID: 5B991F6215D2FCC0
170 changed files with 385 additions and 392 deletions

View file

@ -0,0 +1,33 @@
package buffer
import (
"io"
"io/ioutil"
)
// MemoryBuffer implements Buffer interface using byte slice.
type MemoryBuffer struct {
Slice []byte
}
func (mb MemoryBuffer) Open() (io.ReadCloser, error) {
return NewBytesReader(mb.Slice), nil
}
func (mb MemoryBuffer) Len() int {
return len(mb.Slice)
}
func (mb MemoryBuffer) Remove() error {
return nil
}
// BufferInMemory is a convenience function which creates MemoryBuffer with
// contents of the passed io.Reader.
func BufferInMemory(r io.Reader) (Buffer, error) {
blob, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return MemoryBuffer{Slice: blob}, nil
}