1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 17:46:58 -05:00

handle data first and then error

This commit is contained in:
Darien Raymond 2017-11-26 15:08:37 +01:00
parent 9a2a19509c
commit 519eb344db
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 19 additions and 16 deletions

View File

@ -91,8 +91,7 @@ func copyInternal(reader Reader, writer Writer, handler *copyHandler) error {
buffer.Release()
return werr
}
}
if err != nil {
} else if err != nil {
return err
}
}

View File

@ -25,14 +25,15 @@ func NewBytesToBufferReader(reader io.Reader) Reader {
func (r *BytesToBufferReader) readSmall() (MultiBuffer, error) {
b := New()
if err := b.Reset(ReadFrom(r.Reader)); err != nil {
b.Release()
return nil, err
}
err := b.Reset(ReadFrom(r.Reader))
if b.IsFull() {
r.buffer = make([]byte, 32*1024)
}
return NewMultiBufferValue(b), nil
if !b.IsEmpty() {
return NewMultiBufferValue(b), nil
}
b.Release()
return nil, err
}
// ReadMultiBuffer implements Reader.
@ -45,7 +46,7 @@ func (r *BytesToBufferReader) ReadMultiBuffer() (MultiBuffer, error) {
if nBytes > 0 {
mb := NewMultiBufferCap(nBytes/Size + 1)
mb.Write(r.buffer[:nBytes])
return mb, err
return mb, nil
}
return nil, err
}
@ -122,17 +123,20 @@ func (r *BufferedReader) ReadMultiBuffer() (MultiBuffer, error) {
}
// ReadAtMost returns a MultiBuffer with at most size.
func (r *BufferedReader) ReadAtMost(size int) (mb MultiBuffer, err error) {
func (r *BufferedReader) ReadAtMost(size int) (MultiBuffer, error) {
if r.leftOver == nil {
r.leftOver, err = r.stream.ReadMultiBuffer()
}
if r.leftOver != nil {
mb = r.leftOver.SliceBySize(size)
if r.leftOver.IsEmpty() {
r.leftOver = nil
mb, err := r.stream.ReadMultiBuffer()
if mb.IsEmpty() && err != nil {
return nil, err
}
r.leftOver = mb
}
return
mb := r.leftOver.SliceBySize(size)
if r.leftOver.IsEmpty() {
r.leftOver = nil
}
return mb, nil
}
func (r *BufferedReader) writeToInternal(writer io.Writer) (int64, error) {