public static HeaderRec ReadFrom(BinaryReader r, int startByteIndex) { var rec = new HeaderRec { TableId = (BibleTableId) r.ReadByte(), Flags = (HeaderFlags) r.ReadByte(), Size = r.ReadInt32(), CompressedSize = r.ReadInt32(), StartByteIndex = startByteIndex }; if(rec.CompressedSize < 0 || rec.Size < 0) throw new BibleFormatException("Invalid size in header record."); return rec; }
private static void WriteRecord(BinaryWriter wr, ref HeaderRec rec) { wr.Write((byte) rec.TableId); wr.Write((byte) rec.Flags); wr.Write(rec.Size); wr.Write(rec.CompressedSize); }
public void File_with_invalid_header_record_should_throw() { var rec = new HeaderRec { TableId = BibleTableId.Books, Flags = HeaderFlags.None, Size = -5, CompressedSize = 20, StartByteIndex = 23 }; var wr = new BinaryWriter(new MemoryStream()); var rd = new BinaryReader(wr.BaseStream); WriteAscii(wr, Header.ExpectedHeaderString); WriteVersion(wr, ExpectedVersion); wr.Write((byte)1); WriteRecord(wr, ref rec); wr.Seek(0, SeekOrigin.Begin); Assert.Throws<BibleFormatException>(() => Header.ReadFrom(rd)); }
public void File_with_2_header_records_should_parse() { var rec1 = new HeaderRec { TableId = BibleTableId.Books, Flags = HeaderFlags.None, Size = 25, CompressedSize = 20, StartByteIndex = 33 }; var rec2 = new HeaderRec { TableId = BibleTableId.Verses, Flags = HeaderFlags.None, Size = 50, CompressedSize = 50, StartByteIndex = 53 }; var wr = new BinaryWriter(new MemoryStream()); var rd = new BinaryReader(wr.BaseStream); WriteAscii(wr, Header.ExpectedHeaderString); WriteVersion(wr, ExpectedVersion); wr.Write((byte) 2); WriteRecord(wr, ref rec1); WriteRecord(wr, ref rec2); wr.Seek(0, SeekOrigin.Begin); var header = Header.ReadFrom(rd); Assert.That(header.HeaderRecords[0], Is.EqualTo(rec1)); Assert.That(header.HeaderRecords[1], Is.EqualTo(rec2)); Assert.That(header.HeaderRecords.Count, Is.EqualTo(2)); }