Why do i get additional characters when reading a file through a buffer if buffer size > 15? This is a follow up question to How to speed up writing a file to a WifiClient?

I modified the old code to read from one Stream and write to another which looked like this and worked fine albite being a bit slow

while (file.available() &gt; 0)
{
    client.print((char)file.read());
}
client.flush();


to use a buffer, like so

char buffer[300];
size_t bufferSize = sizeof(buffer);

// ...

while (file.available() &gt; 0)
{   
    memset(buffer, 0, bufferSize);  
    file.read(buffer, bufferSize);

    client.print(buffer);
}
client.flush();


Now, if the buffer size is > 15 (in the above case, it's 300), two additional characters Øb are introduced after the buffer size amount of characters. If the buffer size is &lt;= 15, this does not happen.

If the file to be read contains abcdefg repeated several times, I get the following result for a buffer size of 32:

|-------- 32 characters -------|Øb|-------- 32 characters -------|Øb|-----
abcdefgabcdefgabcdefgabcdefgabcdØbefgabcdefgabcdefgabcdefgabcdefgaØbbcdefg


What's causing those additional characters to appear?

The above output is produced in the browser If I do not specify a charset. if I do, I get � instead of Øb.
Try recording the actual number of bytes read and only write that number of bytes:

size_t nr = file.read(buffer, bufferSize);
client.write(buffer, nr);