예제 #1
0
        // Sets the length of the stream to a given value.  The new
        // value must be nonnegative and less than the space remaining in
        // the array, Int32.MaxValue - origin
        // Origin is 0 in all cases other than a MemoryStream created on
        // top of an existing array and a specific starting offset was passed
        // into the MemoryStream constructor.  The upper bounds prevents any
        // situations where a stream may be created on top of an array then
        // the stream is made longer than the maximum possible length of the
        // array (Int32.MaxValue).
        //
        public override void SetLength(long value)
        {
            if (value < 0 || value > Int32.MaxValue)
            {
                throw new ArgumentOutOfRangeException("value", ResourceHelper.GetResourceString("ArgumentOutOfRange_StreamLength"));
            }
#if DEBUG
            Contract.Ensures(_length - _origin == value);
            Contract.EndContractBlock();
#endif
            EnsureWriteable();

            // Origin wasn't publicly exposed above.
#if DEBUG
            Contract.Assert(MemStreamMaxLength == Int32.MaxValue);  // Check parameter validation logic in this method if this fails.
#endif
            if (value > (Int32.MaxValue - _origin))
            {
                throw new ArgumentOutOfRangeException("value", ResourceHelper.GetResourceString("ArgumentOutOfRange_StreamLength"));
            }

            int  newLength         = _origin + (int)value;
            bool allocatedNewArray = EnsureCapacity(newLength);
            if (!allocatedNewArray && newLength > _length)
            {
                ClusteredArray <byte> .Clear(_buffer, _length, newLength - _length);
            }
            _length = newLength;
            if (_position > newLength)
            {
                _position = newLength;
            }
        }
예제 #2
0
        public ClusteredArray <byte> ToClusteredArray()
        {
            ClusteredArray <byte> clone = (ClusteredArray <byte>)_buffer.Clone();

            clone.Resize(_length);
            return(clone);
        }
예제 #3
0
        public override void WriteByte(byte value)
        {
            if (!_isOpen)
            {
                __Error.StreamIsClosed();
            }
            EnsureWriteable();

            if (_position >= _length)
            {
                int  newLength = _position + 1;
                bool mustZero  = _position > _length;
                if (newLength >= _capacity)
                {
                    bool allocatedNewArray = EnsureCapacity(newLength);
                    if (allocatedNewArray)
                    {
                        mustZero = false;
                    }
                }
                if (mustZero)
                {
                    ClusteredArray <byte> .Clear(_buffer, _length, _position - _length);
                }
                _length = newLength;
            }
            _buffer[_position++] = value;
        }
예제 #4
0
        public static void Clear(ClusteredArray <T> array, int index, int length)
        {
            if (index < 0 || index > array._length)
            {
                throw new ArgumentOutOfRangeException("index");
            }
            if (length < 0 || index + length > array._length)
            {
                throw new ArgumentOutOfRangeException("length");
            }
            int _lengthThreshold = array._lengthThreshold;
            int superStartIndex  = (index / _lengthThreshold);
            int superEndIndex    = ((index + length) / _lengthThreshold);
            int chunkIndex       = index % _lengthThreshold;

            if (superStartIndex == superEndIndex)
            {
                Array.Clear(array._chunks[superStartIndex], chunkIndex, length);
                return;
            }
            for (int i = superStartIndex; i <= superEndIndex; i++)
            {
                int toBeCleared = i == superStartIndex
                    ? _lengthThreshold - chunkIndex
                    : i == superEndIndex ? length % _lengthThreshold : 0;
                Array.Clear(array._chunks[i], i == superStartIndex ? chunkIndex : 0, toBeCleared);
                length -= toBeCleared;
            }
        }
예제 #5
0
        //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;
        }
예제 #6
0
        public object Clone()
        {
            ClusteredArray <T> newarray = new ClusteredArray <T>(_length);

            Copy(this, 0, newarray, 0, _length);
            return(newarray);
        }
예제 #7
0
 public ClusteredMemoryStream(int chunkLength, int size)
 {
     _buffer     = new ClusteredArray <byte>(chunkLength, size);
     _capacity   = size;
     _expandable = true;
     _writable   = true;
     _exposable  = true;
     _origin     = 0;  // Must be 0 for byte[]'s created by MemoryStream
     _isOpen     = true;
 }
예제 #8
0
        public static void Copy(ClusteredArray <T> source, int sourceIndex, ClusteredArray <T> destination,
                                int destinationIndex, int length)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }
            if (sourceIndex > source.Length)
            {
                throw new ArgumentOutOfRangeException("sourceIndex");
            }
            if (destinationIndex > destination.Length)
            {
                throw new ArgumentOutOfRangeException("destinationIndex");
            }
            if ((sourceIndex + length) > source.Length)
            {
                throw new ArgumentOutOfRangeException("length");
            }
            if ((destinationIndex + length) > destination.Length)
            {
                throw new ArgumentOutOfRangeException("length");
            }

            VirtualIndex srcIndex   = new VirtualIndex(source._lengthThreshold, sourceIndex);
            VirtualIndex dstIndex   = new VirtualIndex(source._lengthThreshold, destinationIndex);
            int          dataCopied = 0;

            while (dataCopied < length)
            {
                T[] srcArray = source._chunks[srcIndex.YIndex];
                T[] dstArray = destination._chunks[dstIndex.YIndex];

                int dataRemaining = length - dataCopied;
                int dstSpaceLeft  = dstArray.Length - dstIndex.XIndex;
                int data2Copy     = dataRemaining > dstSpaceLeft ? dstSpaceLeft : dataRemaining;

                int srcSpaceLeft = srcArray.Length - srcIndex.XIndex;
                if (data2Copy > srcSpaceLeft)
                {
                    data2Copy = srcSpaceLeft;
                }

                Array.Copy(srcArray, srcIndex.XIndex, dstArray, dstIndex.XIndex, data2Copy);
                dataCopied += data2Copy;

                srcIndex.IncrementBy(data2Copy);
                dstIndex.IncrementBy(data2Copy);
            }
        }
예제 #9
0
        // Creates a queue with room for capacity objects. The default grow factor
        // is used.
        //
        /// <include file='doc\Queue.uex' path='docs/doc[@for="Queue.Queue1"]/*' />
        public ClusteredQueue(int capacity)
        {
            if (capacity < 0)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity);
            }

            _array = new ClusteredArray <T>(capacity);
            _head  = 0;
            _tail  = 0;
            _size  = 0;
        }
예제 #10
0
        public ClusteredMemoryStream(ClusteredArray <byte> buffer)
        {
            _buffer = new ClusteredArray <byte>(buffer.LengthThreshold, buffer.Length);
            ClusteredArray <byte> .Copy(buffer, 0, _buffer, 0, buffer.Length);

            _length     = _capacity = buffer.Length;
            _expandable = true;
            _writable   = true;
            _exposable  = true;
            _origin     = 0;  // Must be 0 for byte[]'s created by MemoryStream
            _isOpen     = true;
        }
예제 #11
0
        public ClusteredMemoryStream(int capacity)
        {
            if (capacity < 0)
            {
                throw new ArgumentOutOfRangeException("capacity", ResourceHelper.GetResourceString("ArgumentOutOfRange_NegativeCapacity"));
            }
#if DEBUG
            Contract.EndContractBlock();
#endif
            _buffer     = new ClusteredArray <byte>(capacity);
            _capacity   = capacity;
            _expandable = true;
            _writable   = true;
            _exposable  = true;
            _origin     = 0;  // Must be 0 for byte[]'s created by MemoryStream
            _isOpen     = true;
        }
예제 #12
0
        // Creates a queue with room for capacity objects with the given grow factor
        //
        /// <include file='doc\Queue.uex' path='docs/doc[@for="Queue.Queue1"]/*' />
        public ClusteredQueue(int capacity, float growFactor)
        {
            if (capacity < 0)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity);
            }
            if (!(growFactor >= 1.0 && growFactor <= 10.0))
            {
                throw new ArgumentOutOfRangeException("growFactor", ResourceHelper.GetResourceString("ArgumentOutOfRange_QueueGrowFactor"));
            }

            _array      = new ClusteredArray <T>(capacity);
            _head       = 0;
            _tail       = 0;
            _size       = 0;
            _GrowFactor = (int)(growFactor * 100);
        }
예제 #13
0
        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;
        }
예제 #14
0
        // Removes all Objects from the queue.
        /// <include file='doc\Queue.uex' path='docs/doc[@for="Queue.Clear"]/*' />
        public virtual void Clear()
        {
            if (_head < _tail)
            {
                ClusteredArray <T> .Clear(_array, _head, _size);
            }
            else
            {
                ClusteredArray <T> .Clear(_array, _head, _array.Length - _head);

                ClusteredArray <T> .Clear(_array, 0, _tail);
            }

            _head = 0;
            _tail = 0;
            _size = 0;
            _version++;
        }
예제 #15
0
        // Fills a Queue with the elements of an ICollection.  Uses the enumerator
        // to get each of the elements.
        //
        /// <include file='doc\Queue.uex' path='docs/doc[@for="Queue.Queue3"]/*' />
        public ClusteredQueue(IEnumerable <T> collection)
        {
            if (collection == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
            }

            _array   = new ClusteredArray <T>(_DefaultCapacity);
            _size    = 0;
            _version = 0;

            using (IEnumerator <T> en = collection.GetEnumerator())
            {
                while (en.MoveNext())
                {
                    Enqueue(en.Current);
                }
            }
        }
예제 #16
0
        // PRIVATE Grows or shrinks the buffer to hold capacity objects. Capacity
        // must be >= _size.
        private void SetCapacity(int capacity)
        {
            ClusteredArray <T> newarray = new ClusteredArray <T>(capacity);

            if (_size > 0)
            {
                if (_head < _tail)
                {
                    ClusteredArray <T> .Copy(_array, _head, newarray, 0, _size);
                }
                else
                {
                    ClusteredArray <T> .Copy(_array, _head, newarray, 0, _array.Length - _head);

                    ClusteredArray <T> .Copy(_array, 0, newarray, _array.Length - _head, _tail);
                }
            }

            _array = newarray;
            _head  = 0;
            _tail  = (_size == capacity) ? 0 : _size;
            _version++;
        }
예제 #17
0
 // Creates a queue with room for capacity objects. The default initial
 // capacity and grow factor are used.
 /// <include file='doc\Queue.uex' path='docs/doc[@for="Queue.Queue"]/*' />
 public ClusteredQueue()
 {
     _array = _emptyArray;
 }
예제 #18
0
        //Changelog: Used ClusteredArray's internal CopyFrom method.
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer", ResourceHelper.GetResourceString("ArgumentNull_Buffer"));
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset", ResourceHelper.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count", ResourceHelper.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (buffer.Length - offset < count)
            {
                throw new ArgumentException(ResourceHelper.GetResourceString("Argument_InvalidOffLen"));
            }
#if DEBUG
            Contract.EndContractBlock();
#endif
            if (!_isOpen)
            {
                __Error.StreamIsClosed();
            }
            EnsureWriteable();

            int i = _position + count;
            // Check for overflow
            if (i < 0)
            {
                throw new IOException(ResourceHelper.GetResourceString("IO.IO_StreamTooLong"));
            }

            if (i > _length)
            {
                bool mustZero = _position > _length;
                if (i > _capacity)
                {
                    bool allocatedNewArray = EnsureCapacity(i);
                    if (allocatedNewArray)
                    {
                        mustZero = false;
                    }
                }
                if (mustZero)
                {
                    ClusteredArray <byte> .Clear(_buffer, _length, i - _length);
                }
                _length = i;
            }
            if (count <= 8)
            {
                int byteCount = count;
                while (--byteCount >= 0)
                {
                    _buffer[_position + byteCount] = buffer[offset + byteCount];
                }
            }
            else
            {
                _buffer.CopyFrom(buffer, offset, _position, count);
            }
            _position = i;
        }