public static AtomicHeader Deserialize(Stream stream) { AtomicHeader header = new AtomicHeader(); stream.Seek(0, SeekOrigin.Begin); byte[] buffer = new byte[SIZE]; if (stream.Read(buffer, 0, SIZE) != SIZE) { throw new Exception(String.Format("Invalid {0} header.", TITLE)); } using (MemoryStream ms = new MemoryStream(buffer)) { BinaryReader reader = new BinaryReader(ms); string title = reader.ReadString(); if (title != TITLE) { throw new Exception(String.Format("Invalid {0} header.", TITLE)); } header.Version = reader.ReadInt32(); header.UseCompression = reader.ReadBoolean(); //last flush location header.SystemData = Ptr.Deserialize(reader); //tag int tagLength = reader.ReadInt32(); header.Tag = tagLength >= 0 ? reader.ReadBytes(tagLength) : null; } return(header); }
public Heap(Stream stream, bool useCompression = false, AllocationStrategy strategy = AllocationStrategy.FromTheCurrentBlock) { stream.Seek(0, SeekOrigin.Begin); //support Seek? Stream = stream; space = new Space(); used = new Dictionary <long, Pointer>(); reserved = new Dictionary <long, Pointer>(); if (stream.Length < AtomicHeader.SIZE) //create new { header = new AtomicHeader(); header.UseCompression = useCompression; space.Add(new Ptr(AtomicHeader.SIZE, long.MaxValue - AtomicHeader.SIZE)); } else //open exist (ignore the useCompression flag) { header = AtomicHeader.Deserialize(Stream); stream.Seek(header.SystemData.Position, SeekOrigin.Begin); Deserialize(new BinaryReader(stream)); //manual alloc header.SystemData var ptr = space.Alloc(header.SystemData.Size); if (ptr.Position != header.SystemData.Position) { throw new Exception("Logical error."); } } Strategy = strategy; currentVersion++; }