public void WriteEntry(Entry entry) { int entrySize = entry.GetTotalSize(LastPosition); if (entrySize + TocEntrySize > _spaceLeft) { throw new Exception("Not enough space to write entry."); } if (EntryCount == 0) { FirstEntry = entry; } SpanWriter blockWriter = new SpanWriter(Buffer); blockWriter.Position = LastPosition; // Begin to write the entry's common information // Not actually BE, both are writen as indexing buffer int entryOffset = blockWriter.Position; blockWriter.Endian = Endian.Big; blockWriter.WriteInt32(entry.ParentNode); blockWriter.Endian = Endian.Little; blockWriter.WriteStringRaw(entry.Name); blockWriter.Align(0x04); // Entry is aligned // Write type specific int entryMetaOffset = blockWriter.Position; entry.SerializeTypeMeta(ref blockWriter); blockWriter.Align(0x04); // Whole entry is also aligned LastPosition = blockWriter.Position; _spaceLeft -= entrySize + TocEntrySize; // Include the block's toc entry // Write the lookup information at the end of the block blockWriter.Position = BlockSize - ((EntryCount + 1) * TocEntrySize); blockWriter.WriteUInt16((ushort)entryOffset); blockWriter.WriteUInt16((ushort)entry.GetEntryMetaSize()); blockWriter.WriteUInt16((ushort)entryMetaOffset); blockWriter.WriteUInt16(entry.GetTypeMetaSize()); // Move on to next. EntryCount++; }
public void WriteNextDataEntry(Entry lastPrevBlockEntry, Entry firstNextBlockEntry) { var blockWriter = new SpanWriter(Buffer); blockWriter.Position = LastPosition; byte[] indexer = CompareEntries(lastPrevBlockEntry, firstNextBlockEntry); int actualSpace = MeasureEntrySize(blockWriter.Position, indexer); int entrySize = indexer.Length; if (actualSpace > _spaceLeft) { throw new Exception("Not enough space to write index entry."); } // Begin to write the entry's common information int entryOffset = blockWriter.Position; blockWriter.WriteBytes(indexer); blockWriter.Align(0x04); // Entry is aligned int endPos = blockWriter.Position; _spaceLeft -= actualSpace + TocEntrySize; // Include the block's toc entry // Write the lookup information at the end of the block blockWriter.Position = BlockSize - ((EntryCount + 1) * TocEntrySize); blockWriter.WriteUInt16((ushort)entryOffset); blockWriter.WriteUInt16((ushort)entrySize); blockWriter.WriteUInt32(0); // We will write the block index later as we don't have it // Move on to next. EntryCount++; LastPosition = endPos; }