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() > 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() > 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 <= 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.
memset()it's a waste of CPU cycles in this situation – Andrew Oct 27 '16 at 13:28