public IO.RecyclableMemoryStream DownloadToMemory(System.Uri address, string tag)
 {
     IO.RecyclableMemoryStream result = null;
     if (!this.IsBusy)
     {
         OnWorkStarted();
         this.retried = 0;
         this.LastURL = address;
         for (short i = 0; i < Retry; i++)
         {
             try
             {
                 result = this.innerWebClient.DownloadToMemory(address, tag);
                 OnWorkFinished();
                 break;
             }
             catch (WebException ex)
             {
                 if (IsHTTP(address))
                 {
                     if (ex.Response is HttpWebResponse)
                     {
                         if (!WorthRetry(ex.Response as HttpWebResponse))
                         {
                             if (result != null)
                             {
                                 result.Dispose();
                             }
                             result = null;
                             OnWorkFinished();
                             throw ex;
                         }
                     }
                 }
                 else
                 {
                     if (result != null)
                     {
                         result.Dispose();
                     }
                     result = null;
                     OnWorkFinished();
                     throw ex;
                 }
             }
         }
     }
     return(result);
 }
Esempio n. 2
0
        public static UgoiraPlayer Create(Stream sourceStream, EventHandler <UgoiraReadingEventArgs> reportProgress)
        {
            string illustTitle = string.Empty, illustUsername = string.Empty;
            Dictionary <string, BitmapImage> framelist = new Dictionary <string, BitmapImage>(StringComparer.OrdinalIgnoreCase);
            Dictionary <string, FrameHeader> headerlist = new Dictionary <string, FrameHeader>(StringComparer.OrdinalIgnoreCase);

            using (UgoiraReader reader = new UgoiraReader(sourceStream))
            {
                UgoiraReadingEventArgs eventa = null;
                if (reportProgress != null)
                {
                    eventa = new UgoiraReadingEventArgs(reader.Entries.Length);
                }
                for (int i = 0; i < reader.Entries.Length; i++)
                {
                    if (eventa != null)
                    {
                        eventa.SetCurrent(i + 1);
                    }
                    using (IO.RecyclableMemoryStream rms = new IO.RecyclableMemoryStream(reader[i].Filename, Convert.ToInt32(reader[i].Filesize)))
                    {
                        reader[i].Extract(rms);
                        rms.Position = 0;
                        switch (reader[i].Type)
                        {
                        case EntryType.Metadata:
                            JObject jo;
                            using (StreamReader sr = new StreamReader(rms))
                                using (Newtonsoft.Json.JsonTextReader jr = new Newtonsoft.Json.JsonTextReader(sr))
                                {
                                    jr.CloseInput = true;
                                    jo            = JObject.Load(jr);
                                    jr.Close();
                                }
                            if (jo != null)
                            {
                                illustTitle    = jo.Value <object>("illustTitle").ToString();
                                illustUsername = jo.Value <object>("userName").ToString();

                                JToken jt = jo.SelectToken("ugokuIllustData");
                                if (jt != null)
                                {
                                    jt = jo.SelectToken("frames");
                                    if (jt != null)
                                    {
                                        string currentfilename;
                                        foreach (JToken token in jt.Children())
                                        {
                                            currentfilename = token.Value <object>("file").ToString();
                                            headerlist.Add(currentfilename, new FrameHeader(currentfilename, token.Value <int>("delay")));
                                        }
                                    }
                                }
                            }
                            break;

                        case EntryType.Image:
                            framelist.Add(reader[i].Filename, QuickMethods.CreateBitmapImage(rms, true));
                            break;
                        }
                    }
                    if (eventa != null)
                    {
                        reportProgress?.Invoke(reader, eventa);
                        if (eventa.Cancel)
                        {
                            framelist.Clear();
                            headerlist.Clear();
                            return(null);
                        }
                    }
                }
            }
            return(new UgoiraPlayer(MakeFrames(framelist, headerlist, true)));
        }