Ejemplo n.º 1
0
 protected override void Dispose(bool disposing)
 {
     try
     {
         if (disposing)
         {
             try
             {
                 if (this.innerStream != null && this.ownStream)
                 {
                     this.innerStream.Dispose();
                 }
             }
             finally
             {
                 this.innerStream = null;
                 try
                 {
                     if (this.md5 != null)
                     {
                         this.md5.Dispose();
                     }
                 }
                 finally
                 {
                     this.md5 = null;
                 }
             }
         }
     }
     finally
     {
         base.Dispose(disposing);
     }
 }
Ejemplo n.º 2
0
        public static IAsyncResult BeginAsyncStreamCopy(CrcStream source, CrcStream destination, long bytesToRead, int bufferSize, AsyncCrcStreamCopy.ComputeAndLogCrcMethod computeCrcMethodForCrcLogging, TimeSpan timeout, AsyncCallback callback, object state)
        {
            AsyncIteratorContext <long> asyncIteratorContext = new AsyncIteratorContext <long>("AsyncCrcStreamCopy.AsyncCopyStream", callback, state);

            asyncIteratorContext.Begin(AsyncCrcStreamCopy.AsyncStreamCopyImpl(source, destination, bytesToRead, bufferSize, computeCrcMethodForCrcLogging, timeout, asyncIteratorContext));
            return(asyncIteratorContext);
        }
Ejemplo n.º 3
0
 public MD5WriterStream(CrcStream innerStream, long streamLength, bool ownStream, bool synchronousInnerStream)
 {
     if (innerStream == null)
     {
         throw new ArgumentNullException("innerStream");
     }
     if (streamLength > (long)4194304)
     {
         throw new ArgumentOutOfRangeException("streamLength", string.Format("MD5WriterStream supports a maximum stream length of {0}.", (long)4194304));
     }
     this.md5                    = MD5.Create();
     this.innerStream            = innerStream;
     this.ownStream              = ownStream;
     this.streamLength           = streamLength;
     this.streamPosition         = (long)0;
     this.synchronousInnerStream = synchronousInnerStream;
 }
Ejemplo n.º 4
0
 public static IAsyncResult BeginAsyncStreamCopy(CrcStream source, CrcStream destination, long bytesToRead, int bufferSize, TimeSpan timeout, AsyncCallback callback, object state)
 {
     return(AsyncCrcStreamCopy.BeginAsyncStreamCopy(source, destination, bytesToRead, bufferSize, null, timeout, callback, state));
 }
Ejemplo n.º 5
0
        private static IEnumerator <IAsyncResult> AsyncStreamCopyImpl(CrcStream source, CrcStream destination, long numBytes, int bufferSize, AsyncCrcStreamCopy.ComputeAndLogCrcMethod computeCrcMethodForCrcLogging, TimeSpan timeout, AsyncIteratorContext <long> context)
        {
            Exception     exception;
            BufferWrapper buffer        = null;
            BufferWrapper bufferWrapper = null;

            try
            {
                Duration     startingNow  = Duration.StartingNow;
                IAsyncResult asyncResult  = null;
                IAsyncResult asyncResult1 = null;
                buffer        = BufferPool.GetBuffer(bufferSize);
                bufferWrapper = BufferPool.GetBuffer(bufferSize);
                long num      = (long)0;
                long?nullable = null;
                context.ResultData = (long)0;
                bool canTimeout = source.CanTimeout;
                bool flag       = destination.CanTimeout;
                while (true)
                {
                    int timeoutInMS = AsyncCrcStreamCopy.TimeSpanToTimeoutInMS(startingNow.Remaining(timeout));
                    if (timeoutInMS == 0)
                    {
                        throw new TimeoutException("The asynchronous stream copy timed out.");
                    }
                    if (canTimeout)
                    {
                        try
                        {
                            source.ReadTimeout = timeoutInMS;
                        }
                        catch (InvalidOperationException invalidOperationException)
                        {
                            canTimeout = false;
                        }
                    }
                    if (flag)
                    {
                        try
                        {
                            destination.WriteTimeout = timeoutInMS;
                        }
                        catch (InvalidOperationException invalidOperationException1)
                        {
                            flag = false;
                        }
                    }
                    if (numBytes > (long)0)
                    {
                        asyncResult = source.BeginRead(buffer.Buffer, 0, (int)Math.Min((long)((int)buffer.Buffer.Length), numBytes), context.GetResumeCallback(), context.GetResumeState("AsyncCrcStreamCopy.source.Read"));
                    }
                    exception = null;
                    if (num > (long)0)
                    {
                        try
                        {
                            asyncResult1 = destination.BeginWrite(bufferWrapper.Buffer, 0, (int)num, nullable, context.GetResumeCallback(), context.GetResumeState("AsyncCrcStreamCopy.dest.Write"));
                        }
                        catch (Exception exception1)
                        {
                            exception = exception1;
                        }
                        if (exception == null)
                        {
                            yield return(asyncResult1);

                            try
                            {
                                destination.EndWrite(asyncResult1);
                            }
                            catch (Exception exception2)
                            {
                                exception = exception2;
                            }
                        }
                    }
                    if (numBytes <= (long)0)
                    {
                        num = (long)0;
                    }
                    else
                    {
                        yield return(asyncResult);

                        num = (long)source.EndRead(out nullable, asyncResult);
                    }
                    if (exception != null)
                    {
                        throw ExceptionCloner.AttemptClone(exception, RethrowableWrapperBehavior.NoWrap);
                    }
                    if (num < (long)0)
                    {
                        break;
                    }
                    numBytes -= num;
                    AsyncIteratorContext <long> resultData = context;
                    resultData.ResultData = resultData.ResultData + num;
                    if (computeCrcMethodForCrcLogging != null)
                    {
                        computeCrcMethodForCrcLogging(buffer.Buffer, 0, (int)num, context.ResultData);
                    }
                    AsyncCrcStreamCopy.Swap <BufferWrapper>(ref buffer, ref bufferWrapper);
                    if (num <= (long)0)
                    {
                        goto Label2;
                    }
                }
                throw new TimeoutException("Reading from the stream resulted in a negative number of bytes read. This typically means an HttpWebRequest was aborted.");
            }
            finally
            {
                if (buffer != null)
                {
                    BufferPool.ReleaseBuffer(buffer);
                }
                if (bufferWrapper != null)
                {
                    BufferPool.ReleaseBuffer(bufferWrapper);
                }
            }
Label2:
            yield break;
            throw new TimeoutException("The asynchronous stream copy timed out.");
            throw ExceptionCloner.AttemptClone(exception, RethrowableWrapperBehavior.NoWrap);
        }
Ejemplo n.º 6
0
 public MD5WriterStream(CrcStream innerStream, long streamLength, bool ownStream) : this(innerStream, streamLength, ownStream, false)
 {
 }
Ejemplo n.º 7
0
 public MD5WriterStream(CrcStream innerStream, long streamLength) : this(innerStream, streamLength, true)
 {
 }