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
|
|
|
|
}
|
|
|
|
|
2018-05-27 03:38:03 +00:00
|
|
|
type bytesReader struct {
|
2018-03-26 12:32:43 +00:00
|
|
|
dr *delimReader
|
|
|
|
dec io.Reader
|
|
|
|
}
|
|
|
|
|
2018-05-27 03:38:03 +00:00
|
|
|
func newBytesReader(r io.Reader) *bytesReader {
|
2018-03-26 12:32:43 +00:00
|
|
|
dr := &delimReader{r: r}
|
2018-05-27 03:38:03 +00:00
|
|
|
return &bytesReader{
|
2018-03-26 12:32:43 +00:00
|
|
|
dr: dr,
|
|
|
|
dec: base64.NewDecoder(base64.StdEncoding, dr),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 03:38:03 +00:00
|
|
|
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
|
2018-05-27 03:38:03 +00:00
|
|
|
} 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.
|
2018-05-27 03:38:03 +00:00
|
|
|
func (br *bytesReader) buffered() io.Reader {
|
|
|
|
return bytes.NewBuffer(br.dr.rest)
|
2018-03-26 12:32:43 +00:00
|
|
|
}
|