51 lines
947 B
Go
51 lines
947 B
Go
|
package seekbuf
|
||
|
|
||
|
import "io"
|
||
|
|
||
|
// Buffer is a ReadWriteCloser that supports seeking. It's intended to
|
||
|
// replicate the functionality of bytes.Buffer that I use in my projects.
|
||
|
//
|
||
|
// Note that the seeking is limited to the read marker; all writes are
|
||
|
// append-only.
|
||
|
type Buffer struct {
|
||
|
data []byte
|
||
|
pos int
|
||
|
}
|
||
|
|
||
|
func New(data []byte) *Buffer {
|
||
|
return &Buffer{
|
||
|
data: data,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (b *Buffer) Read(p []byte) (int, error) {
|
||
|
if b.pos >= len(b.data) {
|
||
|
return 0, io.EOF
|
||
|
}
|
||
|
|
||
|
n := copy(p, b.data[b.pos:])
|
||
|
b.pos += n
|
||
|
return n, nil
|
||
|
}
|
||
|
|
||
|
func (b *Buffer) Write(p []byte) (int, error) {
|
||
|
b.data = append(b.data, p...)
|
||
|
return len(p), nil
|
||
|
}
|
||
|
|
||
|
// Seek sets the read pointer to pos.
|
||
|
func (b *Buffer) Seek(pos int) {
|
||
|
b.pos = pos
|
||
|
}
|
||
|
|
||
|
// Rewind resets the read pointer to 0.
|
||
|
func (b *Buffer) Rewind() {
|
||
|
b.pos = 0
|
||
|
}
|
||
|
|
||
|
// Close clears all the data out of the buffer and sets the read position to 0.
|
||
|
func (b *Buffer) Close() error {
|
||
|
b.Clear()
|
||
|
return nil
|
||
|
}
|