Frederick Rice
Frederick Rice

Reputation: 71

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.

Upvotes: 6

Views: 12828

Answers (3)

Thundercat
Thundercat

Reputation: 120951

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.

Upvotes: 2

Pravin Mishra
Pravin Mishra

Reputation: 8434

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.

Upvotes: 3

Dave C
Dave C

Reputation: 7878

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.

Upvotes: 3

Related Questions