mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-02 23:47:07 -05:00
fix ReadBuffer()
This commit is contained in:
parent
5e1d8f8e69
commit
f7abe353df
@ -24,20 +24,14 @@ func readOneUDP(r io.Reader) (*Buffer, error) {
|
|||||||
return nil, newError("Reader returns too many empty payloads.")
|
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) {
|
func ReadBuffer(r io.Reader) (*Buffer, error) {
|
||||||
// Use an one-byte buffer to wait for incoming payload.
|
b := New()
|
||||||
var firstByte [1]byte
|
_, err := b.ReadFrom(r)
|
||||||
nBytes, err := r.Read(firstByte[:])
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
b.Release()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
b := New()
|
|
||||||
if nBytes > 0 {
|
|
||||||
common.Must(b.WriteByte(firstByte[0]))
|
|
||||||
}
|
|
||||||
b.ReadFrom(r)
|
|
||||||
return b, nil
|
return b, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user