简体   繁体   中英

Golang Read Bytes from net.TCPConn

Is there a version of ioutil.ReadAll that will read until it reaches an EOF OR if it reads n bytes (whichever comes first)?

I cannot just take the first n bytes from an ioutil.ReadAll dump for DOS reasons.

There are two options. If n is the number of bytes you want to read and r is the connection.

Option 1:

p := make([]byte, n)
_, err := io.ReadFull(r, p)

Option 2:

p, err := io.ReadAll(&io.LimitedReader{R: r, N: n})

The first option is more efficient if the application typically fills the buffer.

If you are reading from an HTTP request body, then use http.MaxBytesReader .

There are a couple of ways to achieve your requirement. You can use either one.

func ReadFull

func ReadFull(r Reader, buf []byte) (n int, err error)

ReadFull reads exactly len(buf) bytes from r into buf. It returns the number of bytes copied and an error if fewer bytes were read. The error is EOF only if no bytes were read.

func LimitReader

func LimitReader(r Reader, n int64) Reader

LimitReader returns a Reader that reads from r but stops with EOF after n bytes. The underlying implementation is a *LimitedReader.

func CopyN

func CopyN(dst Writer, src Reader, n int64) (written int64, err error)

CopyN copies n bytes (or until an error) from src to dst. It returns the number of bytes copied and the earliest error encountered while copying. On return, written == n if and only if err == nil.

func ReadAtLeast

func ReadAtLeast(r Reader, buf []byte, min int) (n int, err error)

ReadAtLeast reads from r into buf until it has read at least min bytes. It returns the number of bytes copied and an error if fewer bytes were read. The error is EOF only if no bytes were read.

io.ReadFull or io.LimitedReader or http.MaxBytesReader .

If you need something different first look at how those are implemented, it'd be trivial to roll your own with tweaked behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM