SslStream has a Read method that accepts an initialized array of bytes (let's call it a buffer), an offset, and the maximum number of bytes that can be written to the buffer, and may not be written if there are not enough of them in the incoming stream. In many data exchange protocols, we do not always know the exact size of the next packet or the reception and transmission architecture built on an abstract “pipeline” in which buffers are allocated immediately for the maximum possible packet size, it can be 12 bytes, or maybe 12,000 bytes. Therefore, it is necessary to initialize a larger buffer (with a margin that often exceeds the actual number of bytes in the input stream hundreds of times), which has a detrimental effect on memory consumption and application performance.
Question : how correctly and with a bias on performance, you can override or in some other way get the packet size first (for example, reading it into some smaller temporary buffer) or the temporary buffer itself (so that you can simply replace the reference to the main buffer)?