Esempio n. 1
0
        private void WebDownloader()
        {
            Stream webStream = null;

            try
            {
                long audioLengthInBytes;

                if (__RequestTasksStop)
                {
                    return;
                }

                webStream = __Downloader.GetUrlStream(__Url, __CacheStream.Position, out audioLengthInBytes);

                if (webStream == null)
                {
                    throw new PuckevichException("Error while creating Url Stream!");
                }

                if (__RequestTasksStop)
                {
                    return;
                }

                if (__CacheStream.Status == AudioStorageStatus.NotStored)
                {
                    __CacheStream.AudioSize = audioLengthInBytes;
                }

                if (__RequestTasksStop)
                {
                    return;
                }

                __ProducerConsumerStream = new ProducerConsumerMemoryStream(__CacheStream);
                __ProducerConsumerStream.LoadToMemory();

                if (__RequestTasksStop)
                {
                    return;
                }

                var buffer = new byte[WEB_BUFFER_SIZE];

                int blockRead = 0;
                int lengthRead;

                if (__RequestTasksStop)
                {
                    return;
                }

                while ((lengthRead = webStream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    blockRead += lengthRead;
                    __ProducerConsumerStream.Write(buffer, 0, lengthRead);

                    if (__RequestTasksStop)
                    {
                        return;
                    }

                    if (blockRead >= WEB_THRESHOLD)
                    {
                        __ThresholdDownloaded = true;
                    }

                    if (__CacheStream == null)
                    {
                        return;
                    }

                    if (__RequestTasksStop)
                    {
                        return;
                    }
                }
            }
            finally
            {
                if (webStream != null)
                {
                    webStream.Dispose();
                }
                __ProducerConsumerStream.FlushToCache();
                __ProducerConsumerStream.WriteFinished = true;
                __ThresholdDownloaded = true;
            }
        }