maddy/buffer/bytesreader.go
fox.cpp d07d656472 buffer: Move to a separate package
The module package is meant for interfaces implemented by modules and is
not a catch-all for all utilities.
2019-08-25 20:23:13 +03:00

48 lines
1.2 KiB
Go

package buffer
import (
"bytes"
)
// TODO: Remove this interface from here, it is no longer necessary.
type Byter interface {
Bytes() []byte
}
// BytesReader is a wrapper for bytes.Reader that stores the original []byte
// value and allows to retrieve it.
//
// It is meant for passing to libraries that expect a io.Reader
// but apply certain optimizations when the Reader implements
// Bytes() interface.
type BytesReader struct {
*bytes.Reader
value []byte
}
// Bytes returns the unread portion of underlying slice used to construct
// BytesReader.
func (br BytesReader) Bytes() []byte {
return br.value[int(br.Size())-br.Len():]
}
// Copy returns the BytesReader reading from the same slice as br at the same
// position.
func (br BytesReader) Copy() BytesReader {
return NewBytesReader(br.Bytes())
}
// Close is a dummy method for implementation of io.Closer so BytesReader can
// be used in MemoryBuffer directly.
func (br BytesReader) Close() error {
return nil
}
func NewBytesReader(b []byte) BytesReader {
// BytesReader and not *BytesReader because BytesReader already wraps two
// pointers and double indirection would be pointless.
return BytesReader{
Reader: bytes.NewReader(b),
value: b,
}
}