I try read file from TLS connect, but I can read only 4096 bytes (n = 4096). How I can read full file?
reader := bufio.NewReader(pc.conn)
msg := make([]byte, 10*1024*1024)
n,err:=reader.Read(msg)
I try read file from TLS connect, but I can read only 4096 bytes (n = 4096). How I can read full file?
reader := bufio.NewReader(pc.conn)
msg := make([]byte, 10*1024*1024)
n,err:=reader.Read(msg)
io.Reader.Read(p []byte)
— if succeeds, — is free to return any number of bytes between 1 and len(p)
; this is by
its contract:
Read reads up to
len(p)
bytes intop
. It returns the number of bytes read (0 <= n <= len(p)
) and any error encountered. Even ifRead
returnsn < len(p)
, it may use all of p as scratch space during the call. If some data is available but notlen(p)
bytes,Read
conventionally returns what is available instead of waiting for more.
(Emphasis mine.)
The "magic number" 4096 you're observing is likely the size of the cache of some piece of software under your TLS connection.
This actually matches the contract of the read(2)
POSIX syscall
(for sockets, it actually will be recv(2)
— from "BSD sockets" which every platform supported by Go implements, including Windows; its counterpart from Winsock has the same semantics, FWIW).
If you know exactly how many bytes you need to read from the source, use the io.ReadFull
helper.
This is usually the simplest approach to deal with data
encoded in a TLV-like format (and encoding/binary
helps as well).