Hmm. It has something to do with your UTF-8 validation in apl_read().
My guess is that read() returns in the middle of a multibyte UTF-8 character, causing the validation to fail.
I don't think this is strictly related to the size of the buffer; I tried changing BUFFER_SIZE to 6256 with no improvement.
It's interesting to note that APL prints the ]KEYB output all in one chunk of about a thousand characters with embedded \n, then does one C++ endl. In a spot check, I couldn't find any other instances in APL where \n was sent instead of endl.