System.Windows.Forms.DynamicFileByteProvider.InsertBytes C# (CSharp) Method

InsertBytes() public method

See IByteProvider.InsertBytes for more information.
public InsertBytes ( long index, byte bs ) : void
index long
bs byte
return void
        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);
            }
        }

Usage Example

        private void btnInsertWord_Click(object sender, EventArgs e)
        {
            long offset = Position.RoundDown(4);
            long index  = offset / 4;

            DynamicFileByteProvider d = hexBox1.ByteProvider as DynamicFileByteProvider;

            d._supportsInsDel = true;
            d.InsertBytes(offset, new byte[] { 0, 0, 0, 0 });
            d._supportsInsDel = false;

            if (index == _relocations.Count)
            {
                _relocations.Add(new Relocation(_section, (int)index));
            }
            else
            {
                _relocations.Insert((int)index, new Relocation(_section, (int)index));
                foreach (ModuleDataNode s in ((ModuleNode)_section.Root).Sections)
                {
                    foreach (Relocation r in s.Relocations)
                    {
                        FixRelocation(r, 1, offset);
                    }
                }
            }

            for (int i = (int)index + 1; i < _relocations.Count; i++)
            {
                _relocations[i]._index++;
            }

            PosChanged();
        }
All Usage Examples Of System.Windows.Forms.DynamicFileByteProvider::InsertBytes