mediocre-go-lib/jstream/bytes_reader.go

56 lines
1.0 KiB
Go
Raw Permalink Normal View History

2018-03-26 12:32:43 +00:00
package jstream
import (
"bytes"
"encoding/base64"
"io"
)
type delimReader struct {
r io.Reader
delim byte
rest []byte
}
func (dr *delimReader) Read(b []byte) (int, error) {
if dr.delim != 0 {
return 0, io.EOF
}
n, err := dr.r.Read(b)
if i := bytes.IndexAny(b[:n], bbDelims); i >= 0 {
dr.delim = b[i]
dr.rest = append([]byte(nil), b[i+1:n]...)
return i, err
}
return n, err
}
type bytesReader struct {
2018-03-26 12:32:43 +00:00
dr *delimReader
dec io.Reader
}
func newBytesReader(r io.Reader) *bytesReader {
2018-03-26 12:32:43 +00:00
dr := &delimReader{r: r}
return &bytesReader{
2018-03-26 12:32:43 +00:00
dr: dr,
dec: base64.NewDecoder(base64.StdEncoding, dr),
}
}
func (br *bytesReader) Read(into []byte) (int, error) {
n, err := br.dec.Read(into)
if br.dr.delim == bbEnd {
2018-03-26 12:32:43 +00:00
return n, io.EOF
} else if br.dr.delim == bbCancel {
2018-03-26 12:32:43 +00:00
return n, ErrCanceled
}
return n, err
}
// returns the bytes which were read off the underlying io.Reader but which
// haven't been consumed yet.
func (br *bytesReader) buffered() io.Reader {
return bytes.NewBuffer(br.dr.rest)
2018-03-26 12:32:43 +00:00
}