コード例 #1
0
        public override Task WriteAsync(Byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            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"));
            }
            Contract.EndContractBlock(); // contract validation copied from Write(...)

            // If cancellation is already requested, bail early
            if (cancellationToken.IsCancellationRequested)
            {
                return(Task.FromCancellation(cancellationToken));
            }

            try
            {
                Write(buffer, offset, count);
                return(Task.CompletedTask);
            }
            catch (OperationCanceledException oce)
            {
                return(Task.FromCancellation <VoidTaskResult>(oce));
            }
            catch (Exception exception)
            {
                return(Task.FromException(exception));
            }
        }
コード例 #2
0
 // returns a bool saying whether we allocated a new array.
 private bool EnsureCapacity(int value)
 {
     // Check for overflow
     if (value < 0)
     {
         throw new IOException(ResourceHelper.GetResourceString("IO.IO_StreamTooLong"));
     }
     if (value > _capacity)
     {
         int newCapacity = value;
         if (newCapacity < 256)
         {
             newCapacity = 256;
         }
         if (newCapacity < _capacity * 2)
         {
             newCapacity = _capacity * 2;
         }
         Capacity = newCapacity;
         return(true);
     }
     return(false);
 }
コード例 #3
0
 public static int GetPrime(int min)
 {
     if (min < 0)
     {
         throw new ArgumentException(ResourceHelper.GetResourceString("Arg_HTCapacityOverflow"));
     }
     for (int i = 0; i < HashHelpers.primes.Length; i++)
     {
         int num = HashHelpers.primes[i];
         if (num >= min)
         {
             return(num);
         }
     }
     for (int j = min | 1; j < 2147483647; j += 2)
     {
         if (HashHelpers.IsPrime(j) && (j - 1) % 101 != 0)
         {
             return(j);
         }
     }
     return(min);
 }
コード例 #4
0
 internal static void ThrowArgumentOutOfRangeException()
 {
     throw new ArgumentOutOfRangeException(ThrowHelper.GetArgumentName(ExceptionArgument.index), ResourceHelper.GetResourceString(ThrowHelper.GetResourceName(ExceptionResource.ArgumentOutOfRange_Index)));
 }
コード例 #5
0
 internal static void ThrowObjectDisposedException(string objectName, ExceptionResource resource)
 {
     throw new ObjectDisposedException(objectName, ResourceHelper.GetResourceString(ThrowHelper.GetResourceName(resource)));
 }
コード例 #6
0
 internal static void ThrowUnauthorizedAccessException(ExceptionResource resource)
 {
     throw new UnauthorizedAccessException(ResourceHelper.GetResourceString(ThrowHelper.GetResourceName(resource)));
 }
コード例 #7
0
 internal static void ThrowNotSupportedException(ExceptionResource resource)
 {
     throw new NotSupportedException(ResourceHelper.GetResourceString(ThrowHelper.GetResourceName(resource)));
 }
コード例 #8
0
 public static void StreamIsClosed()
 {
     throw new ObjectDisposedException(null, ResourceHelper.GetResourceString("ObjectDisposed_StreamClosed"));
 }
コード例 #9
0
 internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
 {
     throw new ArgumentOutOfRangeException(ThrowHelper.GetArgumentName(argument), ResourceHelper.GetResourceString(ThrowHelper.GetResourceName(resource)));
 }
コード例 #10
0
 internal static void ThrowArgumentException(ExceptionResource resource, ExceptionArgument argument)
 {
     throw new ArgumentException(ResourceHelper.GetResourceString(ThrowHelper.GetResourceName(resource)), ThrowHelper.GetArgumentName(argument));
 }
コード例 #11
0
 internal static void ThrowWrongValueTypeArgumentException(object value, Type targetType)
 {
     throw new ArgumentException(ResourceHelper.GetResourceString("Arg_WrongType"));
 }
コード例 #12
0
 internal static void ReadNotSupported()
 {
     throw new NotSupportedException(ResourceHelper.GetResourceString("NotSupported_UnreadableStream"));
 }
コード例 #13
0
 public static void WriteNotSupported()
 {
     throw new NotSupportedException(ResourceHelper.GetResourceString("NotSupported_UnwritableStream"));
 }
コード例 #14
0
 public static void MemoryStreamNotExpandable()
 {
     throw new NotSupportedException(ResourceHelper.GetResourceString("NotSupported_MemStreamNotExpandable"));
 }
コード例 #15
0
        public override Task CopyToAsync(Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
        {
            // This implementation offers beter performance compared to the base class version.

            // The parameter checks must be in [....] with the base version:
            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }

            if (bufferSize <= 0)
            {
                throw new ArgumentOutOfRangeException("bufferSize", ResourceHelper.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
            }

            if (!CanRead && !CanWrite)
            {
                throw new ObjectDisposedException(null, ResourceHelper.GetResourceString("ObjectDisposed_StreamClosed"));
            }

            if (!destination.CanRead && !destination.CanWrite)
            {
                throw new ObjectDisposedException("destination", ResourceHelper.GetResourceString("ObjectDisposed_StreamClosed"));
            }

            if (!CanRead)
            {
                throw new NotSupportedException(ResourceHelper.GetResourceString("NotSupported_UnreadableStream"));
            }

            if (!destination.CanWrite)
            {
                throw new NotSupportedException(ResourceHelper.GetResourceString("NotSupported_UnwritableStream"));
            }

            Contract.EndContractBlock();

            // If we have been inherited into a subclass, the following implementation could be incorrect
            // since it does not call through to Read() or Write() which a subclass might have overriden.
            // To be safe we will only use this implementation in cases where we know it is safe to do so,
            // and delegate to our base class (which will call into Read/Write) when we are not sure.
            if (this.GetType() != typeof(MemoryStream))
            {
                return(base.CopyToAsync(destination, bufferSize, cancellationToken));
            }

            // If cancelled - return fast:
            if (cancellationToken.IsCancellationRequested)
            {
                return(Task.FromCancellation(cancellationToken));
            }

            // Avoid copying data from this buffer into a temp buffer:
            //   (require that InternalEmulateRead does not throw,
            //    otherwise it needs to be wrapped into try-catch-Task.FromException like memStrDest.Write below)

            Int32 pos = _position;
            Int32 n   = InternalEmulateRead(_length - _position);

            // If destination is not a memory stream, write there asynchronously:
            MemoryStream memStrDest = destination as MemoryStream;

            if (memStrDest == null)
            {
                return(destination.WriteAsync(_buffer, pos, n, cancellationToken));
            }

            try {
                // If destination is a MemoryStream, CopyTo synchronously:
                memStrDest.Write(_buffer, pos, n);
                return(Task.CompletedTask);
            } catch (Exception ex) {
                return(Task.FromException(ex));
            }
        }
コード例 #16
0
 internal static void ThrowInvalidOperationException(ExceptionResource resource)
 {
     throw new InvalidOperationException(ResourceHelper.GetResourceString(ThrowHelper.GetResourceName(resource)));
 }
コード例 #17
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;
        }
コード例 #18
0
 internal static void ThrowSecurityException(ExceptionResource resource)
 {
     throw new SecurityException(ResourceHelper.GetResourceString(ThrowHelper.GetResourceName(resource)));
 }
コード例 #19
0
        private static void ParseFrameworkName(string frameworkName, out string identifier, out int version, out string profile)
        {
            if (frameworkName == null)
            {
                throw new ArgumentNullException("frameworkName");
            }
            if (frameworkName.Length == 0)
            {
                throw new ArgumentException(ResourceHelper
                                            .GetResourceString("Argument_StringZeroLength"), "frameworkName");
            }
            string[] array = frameworkName.Split(new char[]
            {
                ','
            });
            version = 0;
            if (array.Length < 2 || array.Length > 3)
            {
                throw new ArgumentException(ResourceHelper.GetResourceString("Argument_FrameworkNameTooShort"), "frameworkName");
            }
            identifier = array[0].Trim();
            if (identifier.Length == 0)
            {
                throw new ArgumentException(ResourceHelper.GetResourceString("Argument_FrameworkNameInvalid"), "frameworkName");
            }
            bool flag = false;

            profile = null;
            for (int i = 1; i < array.Length; i++)
            {
                string[] array2 = array[i].Split(new char[]
                {
                    '='
                });
                if (array2.Length != 2)
                {
                    throw new ArgumentException(ResourceHelper.GetResourceString("SR.Argument_FrameworkNameInvalid"), "frameworkName");
                }
                string text  = array2[0].Trim();
                string text2 = array2[1].Trim();
                if (text.Equals("Version", StringComparison.OrdinalIgnoreCase))
                {
                    flag = true;
                    if (text2.Length > 0 && (text2[0] == 'v' || text2[0] == 'V'))
                    {
                        text2 = text2.Substring(1);
                    }
                    Version version2 = new Version(text2);
                    version = version2.Major * 10000;
                    if (version2.Minor > 0)
                    {
                        version += version2.Minor * 100;
                    }
                    if (version2.Build > 0)
                    {
                        version += version2.Build;
                    }
                }
                else
                {
                    if (!text.Equals("Profile", StringComparison.OrdinalIgnoreCase))
                    {
                        throw new ArgumentException(ResourceHelper.GetResourceString("Argument_FrameworkNameInvalid"), "frameworkName");
                    }
                    if (!string.IsNullOrEmpty(text2))
                    {
                        profile = text2;
                    }
                }
            }
            if (!flag)
            {
                throw new ArgumentException(ResourceHelper.GetResourceString("Argument_FrameworkNameMissingVersion"), "frameworkName");
            }
        }
コード例 #20
0
 public static void EndOfFile()
 {
     throw new EndOfStreamException(ResourceHelper.GetResourceString("IO.EOF_ReadBeyondEOF"));
 }