`(buffer.length - 1)` is incorrect, nSize parameter includes the NULL terminator.
Should be just `buffer.length`.