This won't work if the second or last byte is a lead byte, because modifying `dataLen` here will cause the index calculation the next time around the loop to be wrong.

You could just break out of the loop here immediately if you detect a lead byte, since at that point you know that truncating the buffer before that byte is ok.