Esempio n. 1
0
        /// <summary>
        // Reads 'count' structs of type T from unmanaged memory, into 'array' starting at 'offset'.
        // Note: this method is not safe, since it overwrites the contents of structures, it can
        // be used to modify the private members of a struct.
        /// </summary>
        public int ReadArray <T>(Int64 position, T[] array, Int32 offset, Int32 count) where T : struct
        {
            if (array == null)
            {
                throw new ArgumentNullException(nameof(array), SR.ArgumentNull_Buffer);
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum);
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
            }
            if (array.Length - offset < count)
            {
                throw new ArgumentException(SR.Argument_InvalidOffLen);
            }
            if (!CanRead)
            {
                if (!_isOpen)
                {
                    throw new ObjectDisposedException(nameof(UnmanagedMemoryAccessor), SR.ObjectDisposed_ViewAccessorClosed);
                }
                else
                {
                    throw new NotSupportedException(SR.NotSupported_Reading);
                }
            }
            if (position < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(position), SR.ArgumentOutOfRange_NeedNonNegNum);
            }

            uint sizeOfT = SafeBuffer.AlignedSizeOf <T>();

            // only check position and ask for fewer Ts if count is too big
            if (position >= _capacity)
            {
                throw new ArgumentOutOfRangeException(nameof(position), SR.ArgumentOutOfRange_PositionLessThanCapacityRequired);
            }

            int  n         = count;
            long spaceLeft = _capacity - position;

            if (spaceLeft < 0)
            {
                n = 0;
            }
            else
            {
                ulong spaceNeeded = (ulong)(sizeOfT * count);
                if ((ulong)spaceLeft < spaceNeeded)
                {
                    n = (int)(spaceLeft / sizeOfT);
                }
            }

            _buffer.ReadArray <T>((UInt64)(_offset + position), array, offset, n);

            return(n);
        }