1
0
mirror of https://github.com/Luzifer/badge-gen.git synced 2024-09-20 07:42:55 +00:00
badge-gen/vendor/github.com/tdewolff/buffer/reader.go

40 lines
785 B
Go
Raw Normal View History

2016-06-28 17:38:58 +00:00
package buffer
import "io"
// Reader implements an io.Reader over a byte slice.
type Reader struct {
buf []byte
pos int
}
// NewReader returns a new Reader for a given byte slice.
func NewReader(buf []byte) *Reader {
return &Reader{
buf: buf,
}
}
// Read reads bytes into the given byte slice and returns the number of bytes read and an error if occurred.
func (r *Reader) Read(b []byte) (n int, err error) {
if len(b) == 0 {
return 0, nil
}
if r.pos >= len(r.buf) {
return 0, io.EOF
}
n = copy(b, r.buf[r.pos:])
r.pos += n
return
}
// Bytes returns the underlying byte slice.
func (r *Reader) Bytes() []byte {
return r.buf
}
// Reset resets the position of the read pointer to the beginning of the underlying byte slice
func (r *Reader) Reset() {
r.pos = 0
}