2015-08-01 23:58:14 +08:00
|
|
|
package cmux
|
|
|
|
|
2016-02-26 04:55:09 +08:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
)
|
2015-08-01 23:58:14 +08:00
|
|
|
|
2016-02-26 04:55:09 +08:00
|
|
|
// bufferedReader is an optimized implementation of io.Reader that behaves like
|
|
|
|
// ```
|
|
|
|
// io.MultiReader(bytes.NewReader(buffer.Bytes()), io.TeeReader(source, buffer))
|
|
|
|
// ```
|
|
|
|
// without allocating.
|
|
|
|
type bufferedReader struct {
|
|
|
|
source io.Reader
|
|
|
|
buffer bytes.Buffer
|
|
|
|
bufferRead int
|
|
|
|
bufferSize int
|
|
|
|
sniffing bool
|
2016-04-25 00:55:13 +08:00
|
|
|
lastErr error
|
2015-08-01 23:58:14 +08:00
|
|
|
}
|
|
|
|
|
2016-02-26 04:55:09 +08:00
|
|
|
func (s *bufferedReader) Read(p []byte) (int, error) {
|
2016-04-25 00:55:13 +08:00
|
|
|
if s.bufferSize > s.bufferRead {
|
|
|
|
// If we have already read something from the buffer before, we return the
|
|
|
|
// same data and the last error if any. We need to immediately return,
|
|
|
|
// otherwise we may block for ever, if we try to be smart and call
|
|
|
|
// source.Read() seeking a little bit of more data.
|
|
|
|
bn := copy(p, s.buffer.Bytes()[s.bufferRead:s.bufferSize])
|
|
|
|
s.bufferRead += bn
|
|
|
|
return bn, s.lastErr
|
|
|
|
}
|
2015-08-01 23:58:14 +08:00
|
|
|
|
2016-04-25 00:55:13 +08:00
|
|
|
// If there is nothing more to return in the sniffed buffer, read from the
|
|
|
|
// source.
|
2016-02-26 04:55:09 +08:00
|
|
|
sn, sErr := s.source.Read(p)
|
|
|
|
if sn > 0 && s.sniffing {
|
2016-04-25 00:55:13 +08:00
|
|
|
s.lastErr = sErr
|
2016-02-26 04:55:09 +08:00
|
|
|
if wn, wErr := s.buffer.Write(p[:sn]); wErr != nil {
|
2016-04-25 00:55:13 +08:00
|
|
|
return wn, wErr
|
2016-02-26 04:55:09 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-25 00:55:13 +08:00
|
|
|
return sn, sErr
|
2015-08-01 23:58:14 +08:00
|
|
|
}
|
|
|
|
|
2016-02-26 04:55:09 +08:00
|
|
|
func (s *bufferedReader) reset(snif bool) {
|
|
|
|
s.sniffing = snif
|
|
|
|
s.bufferRead = 0
|
|
|
|
s.bufferSize = s.buffer.Len()
|
2015-08-01 23:58:14 +08:00
|
|
|
}
|