public void InsertBytes(long index, byte[] bs) { try { long blockOffset; DataBlock dataBlock = this.GetDataBlock(index, out blockOffset); MemoryDataBlock memoryDataBlock1 = dataBlock as MemoryDataBlock; if (memoryDataBlock1 != null) { memoryDataBlock1.InsertBytes(index - blockOffset, bs); } else { FileDataBlock fileDataBlock1 = (FileDataBlock)dataBlock; if (blockOffset == index && dataBlock.PreviousBlock != null) { MemoryDataBlock memoryDataBlock2 = dataBlock.PreviousBlock as MemoryDataBlock; if (memoryDataBlock2 != null) { memoryDataBlock2.InsertBytes(memoryDataBlock2.Length, bs); return; } } FileDataBlock fileDataBlock2 = (FileDataBlock)null; if (index > blockOffset) { fileDataBlock2 = new FileDataBlock(fileDataBlock1.FileOffset, index - blockOffset); } FileDataBlock fileDataBlock3 = (FileDataBlock)null; if (index < blockOffset + fileDataBlock1.Length) { fileDataBlock3 = new FileDataBlock(fileDataBlock1.FileOffset + index - blockOffset, fileDataBlock1.Length - (index - blockOffset)); } DataBlock block = this._dataMap.Replace(dataBlock, (DataBlock) new MemoryDataBlock(bs)); if (fileDataBlock2 != null) { this._dataMap.AddBefore(block, (DataBlock)fileDataBlock2); } if (fileDataBlock3 != null) { this._dataMap.AddAfter(block, (DataBlock)fileDataBlock3); } } } finally { this._totalLength += (long)bs.Length; this.OnLengthChanged(EventArgs.Empty); this.OnChanged(EventArgs.Empty); } }
public void ApplyChanges() { if (this._readOnly) { throw new OperationCanceledException("File is in read-only mode"); } if (this._totalLength > this._stream.Length) { this._stream.SetLength(this._totalLength); } long dataOffset = 0L; for (DataBlock dataBlock = this._dataMap.FirstBlock; dataBlock != null; dataBlock = dataBlock.NextBlock) { FileDataBlock fileBlock = dataBlock as FileDataBlock; if (fileBlock != null && fileBlock.FileOffset != dataOffset) { this.MoveFileBlock(fileBlock, dataOffset); } dataOffset += dataBlock.Length; } long num = 0L; for (DataBlock dataBlock = this._dataMap.FirstBlock; dataBlock != null; dataBlock = dataBlock.NextBlock) { MemoryDataBlock memoryDataBlock = dataBlock as MemoryDataBlock; if (memoryDataBlock != null) { this._stream.Position = num; int offset = 0; while ((long)offset < memoryDataBlock.Length) { this._stream.Write(memoryDataBlock.Data, offset, (int)Math.Min(4096L, memoryDataBlock.Length - (long)offset)); offset += 4096; } } num += dataBlock.Length; } this._stream.SetLength(this._totalLength); this.ReInitialize(); }
public void WriteByte(long index, byte value) { try { long blockOffset; DataBlock dataBlock = this.GetDataBlock(index, out blockOffset); MemoryDataBlock memoryDataBlock1 = dataBlock as MemoryDataBlock; if (memoryDataBlock1 != null) { memoryDataBlock1.Data[index - blockOffset] = value; } else { FileDataBlock fileDataBlock1 = (FileDataBlock)dataBlock; if (blockOffset == index && dataBlock.PreviousBlock != null) { MemoryDataBlock memoryDataBlock2 = dataBlock.PreviousBlock as MemoryDataBlock; if (memoryDataBlock2 != null) { memoryDataBlock2.AddByteToEnd(value); fileDataBlock1.RemoveBytesFromStart(1L); if (fileDataBlock1.Length != 0L) { return; } this._dataMap.Remove((DataBlock)fileDataBlock1); return; } } if (blockOffset + fileDataBlock1.Length - 1L == index && dataBlock.NextBlock != null) { MemoryDataBlock memoryDataBlock2 = dataBlock.NextBlock as MemoryDataBlock; if (memoryDataBlock2 != null) { memoryDataBlock2.AddByteToStart(value); fileDataBlock1.RemoveBytesFromEnd(1L); if (fileDataBlock1.Length != 0L) { return; } this._dataMap.Remove((DataBlock)fileDataBlock1); return; } } FileDataBlock fileDataBlock2 = (FileDataBlock)null; if (index > blockOffset) { fileDataBlock2 = new FileDataBlock(fileDataBlock1.FileOffset, index - blockOffset); } FileDataBlock fileDataBlock3 = (FileDataBlock)null; if (index < blockOffset + fileDataBlock1.Length - 1L) { fileDataBlock3 = new FileDataBlock(fileDataBlock1.FileOffset + index - blockOffset + 1L, fileDataBlock1.Length - (index - blockOffset + 1L)); } DataBlock block = this._dataMap.Replace(dataBlock, (DataBlock) new MemoryDataBlock(value)); if (fileDataBlock2 != null) { this._dataMap.AddBefore(block, (DataBlock)fileDataBlock2); } if (fileDataBlock3 != null) { this._dataMap.AddAfter(block, (DataBlock)fileDataBlock3); } } } finally { this.OnChanged(EventArgs.Empty); } }