1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-03 07:56:42 -05:00

Merge pull request #2005 from mellow-io/fix-read

fix ReadBuffer()
This commit is contained in:
Kslr 2019-11-05 18:35:58 +08:00 committed by GitHub
commit 9a06ff8f91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,20 +24,14 @@ func readOneUDP(r io.Reader) (*Buffer, error) {
return nil, newError("Reader returns too many empty payloads.")
}
// ReadBuffer reads a Buffer from the given reader, without allocating large buffer in advance.
// ReadBuffer reads a Buffer from the given reader.
func ReadBuffer(r io.Reader) (*Buffer, error) {
// Use an one-byte buffer to wait for incoming payload.
var firstByte [1]byte
nBytes, err := r.Read(firstByte[:])
b := New()
_, err := b.ReadFrom(r)
if err != nil {
b.Release()
return nil, err
}
b := New()
if nBytes > 0 {
common.Must(b.WriteByte(firstByte[0]))
}
b.ReadFrom(r)
return b, nil
}