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]; for (long index = 0; index < fileBlock.Length; index += (long)buffer.Length) { long num = fileBlock.FileOffset + index; int count = (int)Math.Min((long)buffer.Length, fileBlock.Length - index); this._stream.Position = num; this._stream.Read(buffer, 0, count); this._stream.Position = dataOffset + index; this._stream.Write(buffer, 0, count); } } else { byte[] buffer = new byte[4096]; for (long index = 0; index < fileBlock.Length; index += (long)buffer.Length) { int count = (int)Math.Min((long)buffer.Length, fileBlock.Length - index); this._stream.Position = fileBlock.FileOffset + fileBlock.Length - index - (long)count; this._stream.Read(buffer, 0, count); this._stream.Position = dataOffset + fileBlock.Length - index - (long)count; this._stream.Write(buffer, 0, count); } } fileBlock.SetFileOffset(dataOffset); }
void MoveFileBlock(FileDataBlock fileBlock, long dataOffset) { // First, determine whether the next file block needs to move before this one. long nextDataOffset; FileDataBlock nextFileBlock = GetNextFileDataBlock(fileBlock, dataOffset, out nextDataOffset); if (nextFileBlock != null && dataOffset + fileBlock.Length > nextFileBlock.FileOffset) { // The next block needs to move first, so do that now. MoveFileBlock(nextFileBlock, nextDataOffset); } // Now, move the block. if (fileBlock.FileOffset > dataOffset) { // Move the section to earlier in the file stream (done in chunks starting at the beginning of the section). byte[] buffer = new byte[COPY_BLOCK_SIZE]; for (long relativeOffset = 0; relativeOffset < fileBlock.Length; relativeOffset += buffer.Length) { long readOffset = fileBlock.FileOffset + relativeOffset; int bytesToRead = (int)Math.Min(buffer.Length, fileBlock.Length - relativeOffset); _stream.Position = readOffset; _stream.Read(buffer, 0, bytesToRead); long writeOffset = dataOffset + relativeOffset; _stream.Position = writeOffset; _stream.Write(buffer, 0, bytesToRead); } } else { // Move the section to later in the file stream (done in chunks starting at the end of the section). byte[] buffer = new byte[COPY_BLOCK_SIZE]; for (long relativeOffset = 0; relativeOffset < fileBlock.Length; relativeOffset += buffer.Length) { int bytesToRead = (int)Math.Min(buffer.Length, fileBlock.Length - relativeOffset); long readOffset = fileBlock.FileOffset + fileBlock.Length - relativeOffset - bytesToRead; _stream.Position = readOffset; _stream.Read(buffer, 0, bytesToRead); long writeOffset = dataOffset + fileBlock.Length - relativeOffset - bytesToRead; _stream.Position = writeOffset; _stream.Write(buffer, 0, bytesToRead); } } // This block now points to a different position in the file. fileBlock.SetFileOffset(dataOffset); }
/// <summary> /// See <see cref="IByteProvider.InsertBytes" /> for more information. /// </summary> public void InsertBytes(long index, byte[] bs) { try { // Find the block affected. long blockOffset; DataBlock block = GetDataBlock(index, out blockOffset); // If the insertion point is in a memory block, just insert it. MemoryDataBlock memoryBlock = block as MemoryDataBlock; if (memoryBlock != null) { memoryBlock.InsertBytes(index - blockOffset, bs); return; } FileDataBlock fileBlock = (FileDataBlock)block; // If the insertion point is at the start of a file block, and the previous block is a memory block, append it to that block. if (blockOffset == index && block.PreviousBlock != null) { MemoryDataBlock previousMemoryBlock = block.PreviousBlock as MemoryDataBlock; if (previousMemoryBlock != null) { previousMemoryBlock.InsertBytes(previousMemoryBlock.Length, bs); return; } } // Split the block into a prefix and a suffix and place a memory block in-between. FileDataBlock prefixBlock = null; if (index > blockOffset) { prefixBlock = new FileDataBlock(fileBlock.FileOffset, index - blockOffset); } FileDataBlock suffixBlock = null; if (index < blockOffset + fileBlock.Length) { suffixBlock = new FileDataBlock( fileBlock.FileOffset + index - blockOffset, fileBlock.Length - (index - blockOffset)); } block = _dataMap.Replace(block, new MemoryDataBlock(bs)); if (prefixBlock != null) { _dataMap.AddBefore(block, prefixBlock); } if (suffixBlock != null) { _dataMap.AddAfter(block, suffixBlock); } } finally { _totalLength += bs.Length; OnLengthChanged(EventArgs.Empty); OnChanged(EventArgs.Empty); } }
/// <summary> /// See <see cref="IByteProvider.WriteByte" /> for more information. /// </summary> public void WriteByte(long index, byte value) { try { // Find the block affected. long blockOffset; DataBlock block = GetDataBlock(index, out blockOffset); // If the byte is already in a memory block, modify it. MemoryDataBlock memoryBlock = block as MemoryDataBlock; if (memoryBlock != null) { memoryBlock.Data[index - blockOffset] = value; return; } FileDataBlock fileBlock = (FileDataBlock)block; // If the byte changing is the first byte in the block and the previous block is a memory block, extend that. if (blockOffset == index && block.PreviousBlock != null) { MemoryDataBlock previousMemoryBlock = block.PreviousBlock as MemoryDataBlock; if (previousMemoryBlock != null) { previousMemoryBlock.AddByteToEnd(value); fileBlock.RemoveBytesFromStart(1); if (fileBlock.Length == 0) { _dataMap.Remove(fileBlock); } return; } } // If the byte changing is the last byte in the block and the next block is a memory block, extend that. if (blockOffset + fileBlock.Length - 1 == index && block.NextBlock != null) { MemoryDataBlock nextMemoryBlock = block.NextBlock as MemoryDataBlock; if (nextMemoryBlock != null) { nextMemoryBlock.AddByteToStart(value); fileBlock.RemoveBytesFromEnd(1); if (fileBlock.Length == 0) { _dataMap.Remove(fileBlock); } return; } } // Split the block into a prefix and a suffix and place a memory block in-between. FileDataBlock prefixBlock = null; if (index > blockOffset) { prefixBlock = new FileDataBlock(fileBlock.FileOffset, index - blockOffset); } FileDataBlock suffixBlock = null; if (index < blockOffset + fileBlock.Length - 1) { suffixBlock = new FileDataBlock( fileBlock.FileOffset + index - blockOffset + 1, fileBlock.Length - (index - blockOffset + 1)); } block = _dataMap.Replace(block, new MemoryDataBlock(value)); if (prefixBlock != null) { _dataMap.AddBefore(block, prefixBlock); } if (suffixBlock != null) { _dataMap.AddAfter(block, suffixBlock); } } finally { OnChanged(EventArgs.Empty); } }
public void WriteByte(long index, byte value) { try { long blockOffset; DataBlock dataBlock = this.GetDataBlock(index, out blockOffset); if (dataBlock is MemoryDataBlock memoryDataBlock) { memoryDataBlock.Data[index - blockOffset] = value; } else { FileDataBlock fileDataBlock1 = (FileDataBlock)dataBlock; if (blockOffset == index && dataBlock.PreviousBlock != null && dataBlock.PreviousBlock is MemoryDataBlock previousBlock) { previousBlock.AddByteToEnd(value); fileDataBlock1.RemoveBytesFromStart(1L); if (fileDataBlock1.Length != 0L) { return; } this._dataMap.Remove((DataBlock)fileDataBlock1); } else if (blockOffset + fileDataBlock1.Length - 1L == index && dataBlock.NextBlock != null && dataBlock.NextBlock is MemoryDataBlock nextBlock) { nextBlock.AddByteToStart(value); fileDataBlock1.RemoveBytesFromEnd(1L); if (fileDataBlock1.Length != 0L) { return; } this._dataMap.Remove((DataBlock)fileDataBlock1); } else { 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) { return; } this._dataMap.AddAfter(block, (DataBlock)fileDataBlock3); } } } finally { this.OnChanged(EventArgs.Empty); } }
// Token: 0x06000369 RID: 873 RVA: 0x000122D4 File Offset: 0x000104D4 public void WriteByte(long index, byte value) { try { long num; DataBlock dataBlock = this.GetDataBlock(index, out num); MemoryDataBlock memoryDataBlock = dataBlock as MemoryDataBlock; if (memoryDataBlock != null) { memoryDataBlock.Data[(int)(checked ((IntPtr)(unchecked (index - num))))] = value; } else { FileDataBlock fileDataBlock = (FileDataBlock)dataBlock; if (num == index && dataBlock.PreviousBlock != null) { MemoryDataBlock memoryDataBlock2 = dataBlock.PreviousBlock as MemoryDataBlock; if (memoryDataBlock2 != null) { memoryDataBlock2.AddByteToEnd(value); fileDataBlock.RemoveBytesFromStart(1L); if (fileDataBlock.Length == 0L) { this._dataMap.Remove(fileDataBlock); } return; } } if (num + fileDataBlock.Length - 1L == index && dataBlock.NextBlock != null) { MemoryDataBlock memoryDataBlock3 = dataBlock.NextBlock as MemoryDataBlock; if (memoryDataBlock3 != null) { memoryDataBlock3.AddByteToStart(value); fileDataBlock.RemoveBytesFromEnd(1L); if (fileDataBlock.Length == 0L) { this._dataMap.Remove(fileDataBlock); } return; } } FileDataBlock fileDataBlock2 = null; if (index > num) { fileDataBlock2 = new FileDataBlock(fileDataBlock.FileOffset, index - num); } FileDataBlock fileDataBlock3 = null; if (index < num + fileDataBlock.Length - 1L) { fileDataBlock3 = new FileDataBlock(fileDataBlock.FileOffset + index - num + 1L, fileDataBlock.Length - (index - num + 1L)); } dataBlock = this._dataMap.Replace(dataBlock, new MemoryDataBlock(value)); if (fileDataBlock2 != null) { this._dataMap.AddBefore(dataBlock, fileDataBlock2); } if (fileDataBlock3 != null) { this._dataMap.AddAfter(dataBlock, fileDataBlock3); } } } finally { this.OnChanged(EventArgs.Empty); } }
void MoveFileBlock(FileDataBlock fileBlock, long dataOffset) { long nextDataOffset; FileDataBlock nextFileBlock = GetNextFileDataBlock(fileBlock, dataOffset, out nextDataOffset); if (nextFileBlock != null && dataOffset + fileBlock.Length > nextFileBlock.FileOffset) { MoveFileBlock(nextFileBlock, nextDataOffset); } if (fileBlock.FileOffset > dataOffset) { byte[] buffer = new byte[COPY_BLOCK_SIZE]; for (long relativeOffset = 0; relativeOffset < fileBlock.Length; relativeOffset += buffer.Length) { long readOffset = fileBlock.FileOffset + relativeOffset; int bytesToRead = (int)Math.Min(buffer.Length, fileBlock.Length - relativeOffset); _fileStream.Position = readOffset; _fileStream.Read(buffer, 0, bytesToRead); long writeOffset = dataOffset + relativeOffset; _fileStream.Position = writeOffset; _fileStream.Write(buffer, 0, bytesToRead); } } else { byte[] buffer = new byte[COPY_BLOCK_SIZE]; for (long relativeOffset = 0; relativeOffset < fileBlock.Length; relativeOffset += buffer.Length) { int bytesToRead = (int)Math.Min(buffer.Length, fileBlock.Length - relativeOffset); long readOffset = fileBlock.FileOffset + fileBlock.Length - relativeOffset - bytesToRead; _fileStream.Position = readOffset; _fileStream.Read(buffer, 0, bytesToRead); long writeOffset = dataOffset + fileBlock.Length - relativeOffset - bytesToRead; _fileStream.Position = writeOffset; _fileStream.Write(buffer, 0, bytesToRead); } } fileBlock.SetFileOffset(dataOffset); }
public void WriteByte(long index, byte value) { try { long blockOffset; DataBlock block = GetDataBlock(index, out blockOffset); MemoryDataBlock memoryBlock = block as MemoryDataBlock; if (memoryBlock != null) { memoryBlock.Data[index - blockOffset] = value; return; } FileDataBlock fileBlock = (FileDataBlock)block; if (blockOffset == index && block.PreviousBlock != null) { MemoryDataBlock previousMemoryBlock = block.PreviousBlock as MemoryDataBlock; if (previousMemoryBlock != null) { previousMemoryBlock.AddByteToEnd(value); fileBlock.RemoveBytesFromStart(1); if (fileBlock.Length == 0) { _dataMap.Remove(fileBlock); } return; } } if (blockOffset + fileBlock.Length - 1 == index && block.NextBlock != null) { MemoryDataBlock nextMemoryBlock = block.NextBlock as MemoryDataBlock; if (nextMemoryBlock != null) { nextMemoryBlock.AddByteToStart(value); fileBlock.RemoveBytesFromEnd(1); if (fileBlock.Length == 0) { _dataMap.Remove(fileBlock); } return; } } FileDataBlock prefixBlock = null; if (index > blockOffset) { prefixBlock = new FileDataBlock(fileBlock.FileOffset, index - blockOffset); } FileDataBlock suffixBlock = null; if (index < blockOffset + fileBlock.Length - 1) { suffixBlock = new FileDataBlock( fileBlock.FileOffset + index - blockOffset + 1, fileBlock.Length - (index - blockOffset + 1)); } block = _dataMap.Replace(block, new MemoryDataBlock(value)); if (prefixBlock != null) { _dataMap.AddBefore(block, prefixBlock); } if (suffixBlock != null) { _dataMap.AddAfter(block, suffixBlock); } } finally { OnChanged(EventArgs.Empty); } }
public void InsertBytes(long index, byte[] bs) { try { long blockOffset; DataBlock block = GetDataBlock(index, out blockOffset); MemoryDataBlock memoryBlock = block as MemoryDataBlock; if (memoryBlock != null) { memoryBlock.InsertBytes(index - blockOffset, bs); return; } FileDataBlock fileBlock = (FileDataBlock)block; if (blockOffset == index && block.PreviousBlock != null) { MemoryDataBlock previousMemoryBlock = block.PreviousBlock as MemoryDataBlock; if (previousMemoryBlock != null) { previousMemoryBlock.InsertBytes(previousMemoryBlock.Length, bs); return; } } FileDataBlock prefixBlock = null; if (index > blockOffset) { prefixBlock = new FileDataBlock(fileBlock.FileOffset, index - blockOffset); } FileDataBlock suffixBlock = null; if (index < blockOffset + fileBlock.Length) { suffixBlock = new FileDataBlock( fileBlock.FileOffset + index - blockOffset, fileBlock.Length - (index - blockOffset)); } block = _dataMap.Replace(block, new MemoryDataBlock(bs)); if (prefixBlock != null) { _dataMap.AddBefore(block, prefixBlock); } if (suffixBlock != null) { _dataMap.AddAfter(block, suffixBlock); } } finally { _totalLength += bs.Length; OnLengthChanged(EventArgs.Empty); OnChanged(EventArgs.Empty); } }