private void AppendStreamData(int startSID, int streamLength, byte[] data) { int sid = startSID; int next_sid = SectorAllocation.GetNextSectorID(sid); int index = 0; while (next_sid != SID.EOC) { sid = next_sid; next_sid = SectorAllocation.GetNextSectorID(sid); index += SectorSize; } if (index < streamLength) { int position = streamLength - index; int length = SectorSize - position; if (data.Length <= length) { WriteInSector(sid, position, data, 0, data.Length); } else { WriteInSector(sid, position, data, 0, length); next_sid = AllocateDataSectorAfter(sid); byte[] remained_data = new byte[data.Length - length]; Array.Copy(data, length, remained_data, 0, remained_data.Length); WriteStreamData(next_sid, remained_data); } } else { next_sid = AllocateDataSectorAfter(sid); WriteStreamData(next_sid, data); } }
internal CompoundDocument(Stream stream, FileHeader header) { this.FileStorage = stream; this.Reader = new BinaryReader(this.FileStorage); // added this "if" to prevent exception when file is read only if( this.FileStorage.CanWrite ) this.Writer = new BinaryWriter(this.FileStorage, Encoding.Unicode); this.Header = header; this.SectorSize = (int)Math.Pow(2, Header.SectorSizeInPot); // prevent errors by limiting sector size to a most used value of 2^9 // compare it to zero because this integer can overflow if (this.SectorSize < 0) this.SectorSize = 512; this.ShortSectorSize = (int)Math.Pow(2, Header.ShortSectorSizeInPot); // prevent errors by limiting sector size to a most used value of 2^6 // compare it to zero because this integer can overflow if (this.ShortSectorSize < 0) this.ShortSectorSize = 64; this.TotalSectors = stream.Length == 0 ? 0 : (int)(stream.Length - 512) / this.SectorSize; this.MasterSectorAllocation = new MasterSectorAllocation(this); this.SectorAllocation = new SectorAllocation(this); this.ShortSectorAllocation = new ShortSectorAllocation(this); }
internal List <int> GetStreamDataAsIntegers(int StartSID) { List <int> chain = SectorAllocation.GetSIDChain(StartSID); List <int> data = new List <int>(); foreach (int sid in chain) { data.AddRange(ReadSectorDataAsIntegers(sid)); } return(data); }
private byte[] GetStreamDataAsBytes(int StartSID) { List <int> chain = SectorAllocation.GetSIDChain(StartSID); List <byte> data = new List <byte>(); foreach (int sid in chain) { data.AddRange(ReadSectorDataAsBytes(sid)); } return(data.ToArray()); }
internal CompoundDocument(Stream stream, FileHeader header) { this.FileStorage = stream; this.Reader = new BinaryReader(this.FileStorage); this.Writer = new BinaryWriter(this.FileStorage, Encoding.Unicode); this.Header = header; checked { this.SectorSize = (int)Math.Pow(2.0, (double)this.Header.SectorSizeInPot); this.ShortSectorSize = (int)Math.Pow(2.0, (double)this.Header.ShortSectorSizeInPot); this.TotalSectors = ((stream.Length == 0L) ? 0 : ((int)(stream.Length - 512L) / this.SectorSize)); this.MasterSectorAllocation = new MasterSectorAllocation(this); this.SectorAllocation = new SectorAllocation(this); this.ShortSectorAllocation = new ShortSectorAllocation(this); } }
internal CompoundDocument(Stream stream, FileHeader header) { this.FileStorage = stream; this.Reader = new BinaryReader(this.FileStorage); this.Writer = new BinaryWriter(this.FileStorage, Encoding.Unicode); this.Header = header; this.SectorSize = (int)Math.Pow(2, Header.SectorSizeInPot); this.ShortSectorSize = (int)Math.Pow(2, Header.ShortSectorSizeInPot); this.TotalSectors = stream.Length == 0 ? 0 : (int)(stream.Length - 512) / this.SectorSize; this.MasterSectorAllocation = new MasterSectorAllocation(this); this.SectorAllocation = new SectorAllocation(this); this.ShortSectorAllocation = new ShortSectorAllocation(this); }
internal void WriteStreamData(int startSID, byte[] data) { int prev_sid = SID.EOC; int sid = startSID; int index = 0; while (index < data.Length) { if (sid == SID.EOC) { if (prev_sid == SID.EOC) { sid = this.AllocateDataSector(); } else { sid = this.AllocateDataSectorAfter(prev_sid); } } int offset = GetSectorOffset(sid); Writer.BaseStream.Position = offset; if (index + SectorSize < data.Length) { Writer.Write(data, index, SectorSize); } else { Writer.Write(data, index, data.Length - index); } index += SectorSize; prev_sid = sid; sid = this.SectorAllocation.GetNextSectorID(prev_sid); } if (sid != SID.EOC && prev_sid != SID.EOC) { SectorAllocation.LinkSectorID(prev_sid, SID.EOC); while (sid != SID.EOC) { int next_sid = SectorAllocation.GetNextSectorID(sid); SectorAllocation.LinkSectorID(sid, SID.Free); sid = next_sid; } } }
internal CompoundDocument(Stream stream, FileHeader header) { this.FileStorage = stream; this.Reader = new BinaryReader(this.FileStorage); if (stream.CanWrite) { this.Writer = new BinaryWriter(this.FileStorage, Encoding.Unicode); } this.Header = header; this.SectorSize = (int)Math.Pow(2, Header.SectorSizeInPot); this.ShortSectorSize = (int)Math.Pow(2, Header.ShortSectorSizeInPot); this.TotalSectors = stream.Length == 0 ? 0 : (int)(stream.Length - 512) / this.SectorSize; this.MasterSectorAllocation = new MasterSectorAllocation(this); this.SectorAllocation = new SectorAllocation(this); this.ShortSectorAllocation = new ShortSectorAllocation(this); }
internal CompoundDocument(Stream stream, FileHeader header) { this.FileStorage = stream; this.Reader = new BinaryReader(this.FileStorage); try { this.Writer = new BinaryWriter(this.FileStorage, Encoding.Unicode); } catch (Exception StreamNotReadable) { Console.Write("stream not readable"); } this.Header = header; this.SectorSize = (int)Math.Pow(2, Header.SectorSizeInPot); this.ShortSectorSize = (int)Math.Pow(2, Header.ShortSectorSizeInPot); this.TotalSectors = stream.Length == 0 ? 0 : (int)(stream.Length - 512) / this.SectorSize; this.MasterSectorAllocation = new MasterSectorAllocation(this); this.SectorAllocation = new SectorAllocation(this); this.ShortSectorAllocation = new ShortSectorAllocation(this); }