public bool HasChanges() { if (this._readOnly) { return(false); } if (this._totalLength != this._stream.Length) { return(true); } long num = 0L; for (DataBlock dataBlock = this._dataMap.FirstBlock; dataBlock != null; dataBlock = dataBlock.NextBlock) { FileDataBlock fileDataBlock = dataBlock as FileDataBlock; if (fileDataBlock == null || fileDataBlock.FileOffset != num) { return(true); } num += fileDataBlock.Length; } return(num != this._stream.Length); }
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); } }
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); } }
private void MoveFileBlock(FileDataBlock fileBlock, long dataOffset) { long nextDataOffset; FileDataBlock nextFileDataBlock = this.GetNextFileDataBlock((DataBlock)fileBlock, dataOffset, out nextDataOffset); if (nextFileDataBlock != null && dataOffset + fileBlock.Length > nextFileDataBlock.FileOffset) this.MoveFileBlock(nextFileDataBlock, nextDataOffset); if (fileBlock.FileOffset > dataOffset) { byte[] buffer = new byte[4096]; long num1 = 0L; while (num1 < fileBlock.Length) { long num2 = fileBlock.FileOffset + num1; int count = (int)Math.Min((long)buffer.Length, fileBlock.Length - num1); this._stream.Position = num2; this._stream.Read(buffer, 0, count); this._stream.Position = dataOffset + num1; this._stream.Write(buffer, 0, count); num1 += (long)buffer.Length; } } else { byte[] buffer = new byte[4096]; long num = 0L; while (num < fileBlock.Length) { int count = (int)Math.Min((long)buffer.Length, fileBlock.Length - num); this._stream.Position = fileBlock.FileOffset + fileBlock.Length - num - (long)count; this._stream.Read(buffer, 0, count); this._stream.Position = dataOffset + fileBlock.Length - num - (long)count; this._stream.Write(buffer, 0, count); num += (long)buffer.Length; } } fileBlock.SetFileOffset(dataOffset); }
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); } }