1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00

refine handling for empty read

This commit is contained in:
Darien Raymond 2018-03-29 18:15:50 +02:00
parent babd0107cf
commit bb46a96f04
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

View File

@ -23,7 +23,7 @@ const xlSize = 128 * 1024
func (r *BytesToBufferReader) readSmall() (MultiBuffer, error) {
b := New()
for {
for i := 0; i < 64; i++ {
err := b.Reset(ReadFrom(r.Reader))
if b.IsFull() {
r.buffer = newBytes(Size + 1)
@ -36,6 +36,13 @@ func (r *BytesToBufferReader) readSmall() (MultiBuffer, error) {
return nil, err
}
}
return nil, newError("Reader returns too many empty payloads.")
}
func (r *BytesToBufferReader) freeBuffer() {
freeBytes(r.buffer)
r.buffer = nil
}
// ReadMultiBuffer implements Reader.
@ -44,26 +51,27 @@ func (r *BytesToBufferReader) ReadMultiBuffer() (MultiBuffer, error) {
return r.readSmall()
}
for {
nBytes, err := r.Reader.Read(r.buffer)
if nBytes > 0 {
mb := NewMultiBufferCap(nBytes/Size + 1)
mb.Write(r.buffer[:nBytes])
if nBytes == len(r.buffer) && nBytes < xlSize {
freeBytes(r.buffer)
r.buffer = newBytes(uint32(nBytes) + 1)
} else if nBytes < Size {
freeBytes(r.buffer)
r.buffer = nil
}
return mb, nil
}
if err != nil {
nBytes, err := r.Reader.Read(r.buffer)
if nBytes > 0 {
mb := NewMultiBufferCap(nBytes/Size + 1)
mb.Write(r.buffer[:nBytes])
if nBytes == len(r.buffer) && nBytes < xlSize {
freeBytes(r.buffer)
r.buffer = nil
return nil, err
r.buffer = newBytes(uint32(nBytes) + 1)
} else if nBytes < Size {
r.freeBuffer()
}
return mb, nil
}
r.freeBuffer()
if err != nil {
return nil, err
}
// Read() returns empty payload and nil err. We don't expect this to happen, but just in case.
return r.readSmall()
}
// BufferedReader is a Reader that keeps its internal buffer.