| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- package io
- import (
- "io"
- "sync"
- "v2ray.com/core/common/buf"
- )
- type ChanReader struct {
- sync.Mutex
- stream Reader
- current *buf.Buffer
- eof bool
- }
- func NewChanReader(stream Reader) *ChanReader {
- return &ChanReader{
- stream: stream,
- }
- }
- // Private: Visible for testing.
- func (v *ChanReader) Fill() {
- b, err := v.stream.Read()
- v.current = b
- if err != nil {
- v.eof = true
- v.current = nil
- }
- }
- func (v *ChanReader) Read(b []byte) (int, error) {
- if v.eof {
- return 0, io.EOF
- }
- v.Lock()
- defer v.Unlock()
- if v.current == nil {
- v.Fill()
- if v.eof {
- return 0, io.EOF
- }
- }
- nBytes, err := v.current.Read(b)
- if v.current.IsEmpty() {
- v.current.Release()
- v.current = nil
- }
- return nBytes, err
- }
- func (v *ChanReader) Release() {
- v.Lock()
- defer v.Unlock()
- v.eof = true
- v.current.Release()
- v.current = nil
- v.stream = nil
- }
|