1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-18 07:17:32 -05:00
v2fly/common/buf/merge_reader.go

35 lines
535 B
Go
Raw Normal View History

2017-03-27 05:12:34 -04:00
package buf
type MergingReader struct {
2017-03-27 05:14:55 -04:00
reader Reader
timeoutReader TimeoutReader
2017-03-27 05:12:34 -04:00
}
func NewMergingReader(reader Reader) Reader {
return &MergingReader{
2017-03-27 05:14:55 -04:00
reader: reader,
timeoutReader: reader.(TimeoutReader),
2017-03-27 05:12:34 -04:00
}
}
2017-04-15 15:07:23 -04:00
func (r *MergingReader) Read() (MultiBuffer, error) {
mb, err := r.reader.Read()
2017-03-27 05:12:34 -04:00
if err != nil {
return nil, err
}
2017-03-27 05:14:55 -04:00
if r.timeoutReader == nil {
2017-04-15 15:07:23 -04:00
return mb, nil
2017-03-27 05:14:55 -04:00
}
2017-03-27 05:26:44 -04:00
for {
2017-04-15 15:07:23 -04:00
mb2, err := r.timeoutReader.ReadTimeout(0)
2017-03-27 05:26:44 -04:00
if err != nil {
break
}
2017-04-15 15:07:23 -04:00
mb.AppendMulti(mb2)
2017-03-27 05:12:34 -04:00
}
2017-04-15 15:07:23 -04:00
return mb, nil
2017-03-27 05:12:34 -04:00
}