def write_bytes(fp, data):
    """
    Write bytes to the file object and returns bytes written.

    :return: written byte size
    """
    pos = fp.tell()
    fp.write(data)
    written = fp.tell() - pos
    assert written == len(data), 'written=%d, expected=%d' % (written, len(
        data))
    return written
