Add buf ReadAllFrom

This commit is contained in:
世界 2022-07-06 18:37:16 +08:00
parent 9cd9268a7e
commit f00396c60e
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -2,6 +2,7 @@ package buf
import (
"crypto/rand"
"errors"
"io"
"net"
"strconv"
@ -219,6 +220,23 @@ func (b *Buffer) ReadFullFrom(r io.Reader, size int) (n int, err error) {
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) {
return b.Write([]byte{byte(s)})
}