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