mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-07 05:47:38 +03:00
Add buf ReadAllFrom
This commit is contained in:
parent
9cd9268a7e
commit
f00396c60e
1 changed files with 18 additions and 0 deletions
|
@ -2,6 +2,7 @@ package buf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -219,6 +220,23 @@ func (b *Buffer) ReadFullFrom(r io.Reader, size int) (n int, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Buffer) ReadAllFrom(reader io.Reader) (int, error) {
|
||||||
|
for {
|
||||||
|
if b.IsFull() {
|
||||||
|
return 0, io.ErrShortBuffer
|
||||||
|
}
|
||||||
|
readN, err := reader.Read(b.FreeBytes())
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, io.EOF) {
|
||||||
|
return b.Len(), nil
|
||||||
|
} else {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b.end += readN
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Buffer) WriteRune(s rune) (int, error) {
|
func (b *Buffer) WriteRune(s rune) (int, error) {
|
||||||
return b.Write([]byte{byte(s)})
|
return b.Write([]byte{byte(s)})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue