Remove SD file available size saturation #3209
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
A problem with file streaming arose during some experiments with the SDWebServer example. It seems that the current version of the software has a problem with sending large files stored on the SD card to connected clients.
After trying to find the cause of this problem, I have found that large files are always streamed up to the first 32767 bytes, regardless of the value returned by the size() method of the File class. This can be easily seen in browser network diagnostic tools:

The issue is caused by the SD library's File class available() method, which causes its result to be saturated to 0x7FFF (32767), making the file streaming function behave incorrectly. After removing the value clamping code, the streaming works perfectly for files with sizes larger than 32kB:

I have also tested the fix with very large files (~40MB) and can confirm that it works (albeit quite slowly).
I suspect that the value saturation code was borrowed from the original Arduino SD libraries, which usually use 8-bit AVR compilers, where the int type (returned by the available() method) is a 16-bit integer - hence the clamping.