2019-01-09 03:15:23 +00:00
|
|
|
package streaming
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2019-01-13 02:31:41 +00:00
|
|
|
type Writer struct {
|
2019-01-09 03:15:23 +00:00
|
|
|
data []byte
|
|
|
|
offset int
|
|
|
|
}
|
|
|
|
|
2019-01-13 02:31:41 +00:00
|
|
|
func NewWriter(data []byte) *Writer {
|
|
|
|
return &Writer{data, 0}
|
2019-01-09 03:15:23 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 02:31:41 +00:00
|
|
|
func (w *Writer) Write(data []byte) (int, error) {
|
2019-01-09 03:15:23 +00:00
|
|
|
length := len(data)
|
|
|
|
if w.offset+length > len(w.data) {
|
|
|
|
length = len(w.data) - w.offset
|
|
|
|
}
|
|
|
|
|
|
|
|
if length == 0 {
|
|
|
|
return 0, errors.New("cannot write past end of buffer")
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(w.data[w.offset:], data[:length])
|
|
|
|
w.offset += length
|
|
|
|
|
|
|
|
return length, nil
|
|
|
|
}
|
|
|
|
|
2019-01-13 02:31:41 +00:00
|
|
|
func (w *Writer) Seek(offset int64, whence int) (int64, error) {
|
2019-01-09 03:15:23 +00:00
|
|
|
result := w.offset
|
|
|
|
switch whence {
|
|
|
|
case io.SeekStart:
|
|
|
|
result = int(offset)
|
|
|
|
case io.SeekCurrent:
|
|
|
|
result = w.offset + int(offset)
|
|
|
|
case io.SeekEnd:
|
|
|
|
result = len(w.data) - int(offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
if result < 0 {
|
|
|
|
return int64(w.offset), errors.New("cannot seek before beginning of buffer")
|
|
|
|
}
|
|
|
|
if result >= len(w.data) {
|
|
|
|
return int64(w.offset), errors.New("cannot seek past end of buffer")
|
|
|
|
}
|
|
|
|
|
|
|
|
w.offset = result
|
|
|
|
return int64(w.offset), nil
|
|
|
|
}
|