/// <summary>
/// Saves the changes made in the current instance to the
/// file it represents.
/// </summary>
public override void Save()
{
Mode = AccessMode.Write;
try {
long end;
List <Page> pages = new List <Page> ();
Dictionary <uint, Bitstream> streams =
ReadStreams(pages, out end);
Dictionary <uint, Paginator> paginators =
new Dictionary <uint, Paginator> ();
List <List <Page> > new_pages =
new List <List <Page> > ();
Dictionary <uint, int> shifts =
new Dictionary <uint, int> ();
foreach (Page page in pages)
{
uint id = page.Header.StreamSerialNumber;
if (!paginators.ContainsKey(id))
{
paginators.Add(id,
new Paginator(
streams [id].Codec));
}
paginators [id].AddPage(page);
}
foreach (uint id in paginators.Keys)
{
paginators [id].SetComment(
tag.GetComment(id));
int shift;
new_pages.Add(new List <Page> (
paginators [id]
.Paginate(out shift)));
shifts.Add(id, shift);
}
ByteVector output = new ByteVector();
bool empty;
do
{
empty = true;
foreach (List <Page> stream_pages in new_pages)
{
if (stream_pages.Count == 0)
{
continue;
}
output.Add(stream_pages [0].Render());
stream_pages.RemoveAt(0);
if (stream_pages.Count != 0)
{
empty = false;
}
}
} while (!empty);
Insert(output, 0, end);
InvariantStartPosition = output.Count;
InvariantEndPosition = Length;
TagTypesOnDisk = TagTypes;
Page.OverwriteSequenceNumbers(this,
output.Count, shifts);
} finally {
Mode = AccessMode.Closed;
}
}