Exemplo n.º 1
0
        public override void RemoveBytes(long index, long length, BytePartList bytePartList)
        {
            if (bytePartList == null)
            {
                throw new ArgumentNullException("bytePartList", "FileBytePart.RemoveBytes: The parameter cannot be NULL.");
            }

            if (index > _filePartLength)
            {
                throw new ArgumentOutOfRangeException("index", "FileBytePart.RemoveBytes: The index cannot be greater than the length.");
            }

            if (index + length > _filePartLength)
            {
                throw new ArgumentOutOfRangeException("length", "FileBytePart.RemoveBytes: No more elements can be removed than are present.");
            }

            long startIndex  = _filePartIndex;
            long startLength = index;

            long endIndex  = _filePartIndex + index + length;
            long endLength = _filePartLength - length - startLength;


            if (startLength > 0 && endLength > 0)
            {
                _filePartIndex  = startIndex;
                _filePartLength = startLength;

                LinkedListNode <BytePart> bp = bytePartList.Find(this);
                bytePartList.AddAfter(bp, new FileBytePart(endIndex, endLength));
            }
            else
            {
                if (startLength > 0)
                {
                    _filePartIndex  = startIndex;
                    _filePartLength = startLength;
                }
                else
                {
                    _filePartIndex  = endIndex;
                    _filePartLength = endLength;
                }
            }
        }
Exemplo n.º 2
0
        public void WriteByte(long index, byte value, bool track_Change = true)
        {
            long startIndex = 0;
            byte oldValue   = ReadByte(index);

            if (track_Change)
            {
                _undoList.Add(new UndoAction(new byte[] { oldValue }, index, mod_type.mod_replace));
            }

            BytePart bytePart = BytePartGetInfo(index, out startIndex);

            FileBytePart   fileBytePart   = bytePart as FileBytePart;
            MemoryBytePart memoryBytePart = bytePart as MemoryBytePart;

            if (memoryBytePart != null)
            {
                memoryBytePart.Bytes[index - startIndex] = value;
                OnDataChanged(EventArgs.Empty);

                return;
            }

            if (fileBytePart != null)
            {
                LinkedListNode <BytePart> bp = _bytePartList.Find(bytePart);

                if (index == startIndex && bp.Previous != null)
                {
                    MemoryBytePart prevMemoryBytePart = bp.Previous.Value as MemoryBytePart;

                    if (prevMemoryBytePart != null)
                    {
                        prevMemoryBytePart.AddByteToEnd(value);
                        fileBytePart.RemoveBytesFromStart(1);

                        if (fileBytePart.Length == 0)
                        {
                            _bytePartList.Remove(fileBytePart);
                        }

                        OnDataChanged(EventArgs.Empty);

                        return;
                    }
                }

                if (index == startIndex + fileBytePart.Length - 1 && bp.Next != null)
                {
                    MemoryBytePart nextMemoryBytePart = bp.Next.Value as MemoryBytePart;
                    if (nextMemoryBytePart != null)
                    {
                        nextMemoryBytePart.AddByteToStart(value);
                        fileBytePart.RemoveBytesFromEnd(1);

                        if (fileBytePart.Length == 0)
                        {
                            _bytePartList.Remove(fileBytePart);
                        }

                        OnDataChanged(EventArgs.Empty);

                        return;
                    }
                }

                FileBytePart newPrevFileBytePart = null;
                FileBytePart newNextFileBytePart = null;

                if (index > startIndex)
                {
                    newPrevFileBytePart = new FileBytePart(fileBytePart.FilePartIndex, index - startIndex);
                }

                if (index < startIndex + fileBytePart.Length - 1)
                {
                    newNextFileBytePart = new FileBytePart(
                        fileBytePart.FilePartIndex + index - startIndex + 1,
                        fileBytePart.Length - (index - startIndex + 1));
                }

                BytePart newMemoryBP = new MemoryBytePart(value);
                _bytePartList.Find(bytePart).Value = newMemoryBP;
                bytePart = newMemoryBP;

                if (newPrevFileBytePart != null)
                {
                    LinkedListNode <BytePart> bp1 = _bytePartList.Find(bytePart);
                    _bytePartList.AddBefore(bp1, newPrevFileBytePart);
                }

                if (newNextFileBytePart != null)
                {
                    LinkedListNode <BytePart> bp2 = _bytePartList.Find(bytePart);
                    _bytePartList.AddAfter(bp2, newNextFileBytePart);
                }

                OnDataChanged(EventArgs.Empty);
            }
            else
            {
                throw new ArgumentNullException("index", "FileByteData.WriteByte: The internal BytePartList is corrupt.");
            }
        }