'Implements flushing changes in update mode when parts of one or more HDU
need to be resized.'

def _flush_resize(self):

    old_name = self._file.name
    old_memmap = self._file.memmap
    name = _tmp_name(old_name)
    if (not self._file.file_like):
        old_mode = os.stat(old_name).st_mode
        if (self._file.compression == 'gzip'):
            new_file = gzip.GzipFile(name, mode='ab+')
        elif (self._file.compression == 'bzip2'):
            new_file = bz2.BZ2File(name, mode='w')
        else:
            new_file = name
        with self.fromfile(new_file, mode='append') as hdulist:
            for hdu in self:
                hdu._writeto(hdulist._file, inplace=True, copy=True)
            if sys.platform.startswith('win'):
                mmaps = [(idx, _get_array_mmap(hdu.data), hdu.data) for (idx, hdu) in enumerate(self) if hdu._has_data]
            hdulist._file.close()
            self._file.close()
        if sys.platform.startswith('win'):
            for (idx, mmap, arr) in mmaps:
                if (mmap is not None):
                    mmap.close()
        os.remove(self._file.name)
        os.rename(name, old_name)
        os.chmod(old_name, old_mode)
        if isinstance(new_file, gzip.GzipFile):
            old_file = gzip.GzipFile(old_name, mode='rb+')
        else:
            old_file = old_name
        ffo = _File(old_file, mode='update', memmap=old_memmap)
        self._file = ffo
        for hdu in self:
            if (hdu._has_data and (_get_array_mmap(hdu.data) is not None)):
                del hdu.data
            hdu._file = ffo
        if sys.platform.startswith('win'):
            for (idx, mmap, arr) in mmaps:
                if (mmap is not None):
                    arr.data = self[idx].data.data
            del mmaps
    else:
        self.writeto(name)
        hdulist = self.fromfile(name)
        ffo = self._file
        ffo.truncate(0)
        ffo.seek(0)
        for hdu in hdulist:
            hdu._writeto(ffo, inplace=True, copy=True)
        hdulist.close()
        os.remove(hdulist._file.name)
    self._resize = False
    self._truncate = False
    for hdu in self:
        hdu._header._modified = False
        hdu._new = False
        hdu._file = ffo
