return number of bytes written, including 0 when byte array is full