Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang Read Bytes from net.TCPConn

Tags:

tcp

go

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.

like image 846
Frederick Rice Avatar asked Mar 23 '15 22:03

Frederick Rice


3 Answers

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.

like image 137
Dave C Avatar answered Oct 19 '22 04:10

Dave C


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.

like image 34
Pravin Mishra Avatar answered Oct 19 '22 03:10

Pravin Mishra


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.

like image 2
Bayta Darell Avatar answered Oct 19 '22 05:10

Bayta Darell