public ClusteredMemoryStream(byte[][] buffer, bool writable)
        {
            int position = 0;

            foreach (byte[] bytes in buffer)
            {
                _buffer.CopyFrom(bytes, 0, position, bytes.Length);
                position += bytes.Length;
            }
            _length     = _capacity = position;
            _writable   = writable;
            _expandable = false;
            _origin     = 0;
            _isOpen     = true;
        }
        //Changelog: Used ClusteredArray's internal CopyFrom method.
        public ClusteredMemoryStream(byte[] buffer, int index, int count, bool writable, bool publiclyVisible)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer", ResourceHelper.GetResourceString("ArgumentNull_Buffer"));
            }
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index", ResourceHelper.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count", ResourceHelper.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (buffer.Length - index < count)
            {
                throw new ArgumentException(ResourceHelper.GetResourceString("Argument_InvalidOffLen"));
            }
#if DEBUG
            Contract.EndContractBlock();
#endif
            _buffer = new ClusteredArray <byte>(buffer.Length);
            _buffer.CopyFrom(buffer, 0, 0, buffer.Length);
            _origin     = _position = index;
            _length     = _capacity = index + count;
            _writable   = writable;
            _exposable  = publiclyVisible; // Can GetBuffer return the array?
            _expandable = false;
            _isOpen     = true;
        }
Example #3
0
        // Inserts the elements of the given collection at a given index. If
        // required, the capacity of the list is increased to twice the previous
        // capacity or the new size, whichever is larger.  Ranges may be added
        // to the end of the list by setting index to the List's size.
        //
        public void InsertRange(int index, IEnumerable <T> collection)
        {
            if (collection == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
            }

            if ((uint)index > (uint)_size)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_Index);
            }
#if DEBUG
            Contract.EndContractBlock();
#endif
            ICollection <T> c = collection as ICollection <T>;
            if (c != null)
            {
                int count = c.Count;
                if (count > 0)
                {
                    EnsureCapacity(_size + count);
                    if (index < _size)
                    {
                        ClusteredArray <T> .Copy(_items, index, _items, index + count, _size - index);
                    }

                    // If we're inserting a List into itself, we want to be able to deal with that.
                    if (this == c)
                    {
                        // Copy first part of _items to insert location
                        ClusteredArray <T> .Copy(_items, 0, _items, index, index);

                        // Copy last part of _items back to inserted location
                        ClusteredArray <T> .Copy(_items, index + count, _items, index * 2, _size - index);
                    }
                    else
                    {
                        T[] itemsToInsert = new T[count];
                        c.CopyTo(itemsToInsert, 0);
                        _items.CopyFrom(itemsToInsert, 0, index, count);
                    }
                    _size += count;
                }
            }
            else
            {
                using (IEnumerator <T> en = collection.GetEnumerator())
                {
                    while (en.MoveNext())
                    {
                        Insert(index++, en.Current);
                    }
                }
            }
            _version++;
        }
        public ClusteredMemoryStream(byte[] buffer, bool writable)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer", ResourceHelper.GetResourceString("ArgumentNull_Buffer"));
            }
#if DEBUG
            Contract.EndContractBlock();
#endif
            _buffer = new ClusteredArray <byte>(buffer.Length);
            _buffer.CopyFrom(buffer, 0, 0, buffer.Length);
            _length    = _capacity = buffer.Length;
            _writable  = writable;
            _exposable = false;
            _origin    = 0;
            _isOpen    = true;
        }