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);
            }
        }
Beispiel #2
0
 public DataBlock Replace(DataBlock block, DataBlock newBlock)
 {
     AddAfterInternal(block, newBlock);
     RemoveInternal(block);
     return(newBlock);
 }
Beispiel #3
0
 public void Remove(DataBlock block)
 {
     RemoveInternal(block);
 }
Beispiel #4
0
 public void AddBefore(DataBlock block, DataBlock newBlock)
 {
     AddBeforeInternal(block, newBlock);
 }
Beispiel #5
0
 public void AddAfter(DataBlock block, DataBlock newBlock)
 {
     AddAfterInternal(block, newBlock);
 }
Beispiel #6
0
 void InvalidateBlock(DataBlock block)
 {
     block._map           = null;
     block._nextBlock     = null;
     block._previousBlock = null;
 }