'Replace old_pages with new_pages within fileobj.
old_pages must have come from reading fileobj originally.
new_pages are assumed to have the \'same\' data as old_pages,
and so the serial and sequence numbers will be copied, as will
the flags for the first and last pages.
fileobj will be resized and pages renumbered as necessary. As
such, it must be opened r+b or w+b.'

@classmethod
def replace(cls, fileobj, old_pages, new_pages):

    if ((not len(old_pages)) or (not len(new_pages))):
        raise ValueError('empty    pages    list    not    allowed')
    first = old_pages[0].sequence
    for (page, seq) in izip(new_pages, xrange(first, (first + len(new_pages)))):
        page.sequence = seq
        page.serial = old_pages[0].serial
    new_pages[0].first = old_pages[0].first
    new_pages[0].last = old_pages[0].last
    new_pages[0].continued = old_pages[0].continued
    new_pages[(-1)].first = old_pages[(-1)].first
    new_pages[(-1)].last = old_pages[(-1)].last
    new_pages[(-1)].complete = old_pages[(-1)].complete
    if ((not new_pages[(-1)].complete) and (len(new_pages[(-1)].packets) == 1)):
        new_pages[(-1)].position = (-1)
    new_data = [cls.write(p) for p in new_pages]
    pages_diff = (len(old_pages) - len(new_data))
    if (pages_diff > 0):
        new_data.extend(([''] * pages_diff))
    elif (pages_diff < 0):
        new_data[(pages_diff - 1):] = [''.join(new_data[(pages_diff - 1):])]
    offset_adjust = 0
    new_data_end = None
    assert (len(old_pages) == len(new_data))
    for (old_page, data) in izip(old_pages, new_data):
        offset = (old_page.offset + offset_adjust)
        data_size = len(data)
        resize_bytes(fileobj, old_page.size, data_size, offset)
        fileobj.seek(offset, 0)
        fileobj.write(data)
        new_data_end = (offset + data_size)
        offset_adjust += (data_size - old_page.size)
    if (len(old_pages) != len(new_pages)):
        fileobj.seek(new_data_end, 0)
        serial = new_pages[(-1)].serial
        sequence = (new_pages[(-1)].sequence + 1)
        cls.renumber(fileobj, serial, sequence)
