[Issue #1288] Adding loop around write to ensure entire length of data is written