コード例 #1
0
            protected override byte[] RunInBackground(params string[] strings)
            {
                byte[] rv;
                try
                {
                    URLConnection connection = (new URL(strings[0])).OpenConnection();
                    connection.Connect();
                    System.IO.Stream inputStream = connection.InputStream;

                    System.IO.MemoryStream buffer = new System.IO.MemoryStream();
                    int    nRead;
                    byte[] data = new byte[16384];
                    while ((nRead = inputStream.Read(data, 0, data.Length)) != -1)
                    {
                        buffer.Write(data, 0, nRead);
                    }
                    buffer.Flush();
                    rv = buffer.ToArray();
                    buffer.Close();
                    inputStream.Close();
                }
                catch (Java.Lang.Exception)
                {
                    rv = null;
                }
                return(rv);
            }
コード例 #2
0
        private static string GetFileFromUrl(string fromUrl, string toFile)
        {
            try
            {
                URL           url        = new URL(fromUrl);
                URLConnection connection = url.OpenConnection();
                connection.Connect();

                //int fileLength = connection.GetContentLength();

                // download the file
                InputStream  input  = new BufferedInputStream(url.OpenStream());
                OutputStream output = new FileOutputStream(toFile);

                var  data  = new byte[1024];
                long total = 0;
                int  count;
                while ((count = input.Read(data)) != -1)
                {
                    total += count;
                    ////PublishProgress((int)(total * 100 / fileLength));
                    output.Write(data, 0, count);
                }

                output.Flush();
                output.Close();
                input.Close();
            }
            catch (Exception e)
            {
                Log.Error("YourApp", e.Message);
            }
            return(toFile);
        }
コード例 #3
0
        protected override string RunInBackground(params string[] @params)
        {
            string strongPath = Android.OS.Environment.ExternalStorageDirectory.Path;
            string filePath   = System.IO.Path.Combine(strongPath, "download.jpg");
            int    count;

            try
            {
                URL           url        = new URL(@params[0]);
                URLConnection connection = url.OpenConnection();
                connection.Connect();
                int          LengthOfFile = connection.ContentLength;
                InputStream  input        = new BufferedInputStream(url.OpenStream(), LengthOfFile);
                OutputStream output       = new FileOutputStream(filePath);
                byte[]       data         = new byte[1024];
                long         total        = 0;
                while ((count = input.Read(data)) != -1)
                {
                    total += count;
                    PublishProgress("" + (int)((total / 100) / LengthOfFile));
                    output.Write(data, 0, count);
                }
                output.Flush();
                output.Close();
                input.Close();
            }
            catch (Exception e)
            {
            }
            return(null);
        }
コード例 #4
0
        protected override string RunInBackground(params string[] @params)
        {
            var storagePath = Android.OS.Environment.ExternalStorageDirectory.Path;
            var filePath    = System.IO.Path.Combine(storagePath, $"{fileName}.jpg");

            try
            {
                URL           url        = new URL(@params[0]);
                URLConnection connection = url.OpenConnection();
                connection.Connect();
                int          lengthOfFile = connection.ContentLength;
                InputStream  input        = new BufferedInputStream(url.OpenStream(), lengthOfFile);
                OutputStream output       = new FileOutputStream(filePath);

                byte[] data  = new byte[1024];
                long   total = 0;
                int    count = 0;
                while ((count = input.Read(data)) != -1)
                {
                    total += count;
                    PublishProgress($"{(int)(total / 100) / lengthOfFile}");
                    output.Write(data, 0, count);
                }
                output.Flush();
                output.Close();
                input.Close();
                return(string.Empty);
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
コード例 #5
0
        /// <summary>access a url, ignoring some IOException such as the page does not exist</summary>
        /// <exception cref="System.IO.IOException"/>
        internal static void Access(string urlstring)
        {
            Log.Warn("access " + urlstring);
            Uri           url        = new Uri(urlstring);
            URLConnection connection = url.OpenConnection();

            connection.Connect();
            try
            {
                BufferedReader @in = new BufferedReader(new InputStreamReader(connection.GetInputStream
                                                                                  ()));
                try
                {
                    for (; @in.ReadLine() != null;)
                    {
                    }
                }
                finally
                {
                    @in.Close();
                }
            }
            catch (IOException ioe)
            {
                Log.Warn("urlstring=" + urlstring, ioe);
            }
        }
コード例 #6
0
        //***************************************************************************************************
        //******************FUNCION  QUE SE ENCARGA DEL PROCESO DE DESCARGA DE ARCHIVO***********************
        //***************************************************************************************************
        void DescargaArchivo(ProgressCircleView tem_pcv, string url_archivo)
        {
            //OBTENEMOS LA RUTA DONDE SE ENCUENTRA LA CARPETA PICTURES DE NUESTRO DISPOSITIVO Y LE CONCTENAMOS
            //EL NOMBRE DE UNA CARPETA NUEVA CARPETA, ES AQUI DONDE GUADAREMOS EL ARCHIVO A DESCARGAR
            string filePath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).AbsolutePath + "/DescargaImagen/";

            Java.IO.File directory = new Java.IO.File(filePath);

            //VERIFICAMOS SI LA CARPETA EXISTE, SI NO LA CREAMOS
            if (directory.Exists() == false)
            {
                directory.Mkdir();
            }

            //ESTABLECEMOS LA UBICACION DE NUESTRO ARCHIVO A DESCARGAR
            URL url = new URL(url_archivo);

            //CABRIMOS UNA CONEXION CON EL ARCHIVO
            URLConnection conexion = url.OpenConnection();

            conexion.Connect();

            //OBTENEMOS EL TAMAÑO DEL ARCHIVO A DESCARGAR
            int lenghtOfFile = conexion.ContentLength;

            //CREAMOS UN INPUTSTREAM PARA PODER EXTRAER EL ARCHIVO DE LA CONEXION
            InputStream input = new BufferedInputStream(url.OpenStream());

            //ASIGNAMOS LA RUTA DONDE SE GUARDARA EL ARCHIVO, Y ASIGNAMOS EL NOMBRE CON EL QUE SE DESCARGAR EL ARCHIVO
            //PARA ESTE CASO CONSERVA EL MISMO NOMBRE
            string NewFile = directory.AbsolutePath + "/" + url_archivo.Substring(url_archivo.LastIndexOf("/") + 1);

            //CREAMOS UN OUTPUTSTREAM EN EL CUAL UTILIZAREMOS PARA CREAR EL ARCHIVO QUE ESTAMOS DESCARGANDO
            OutputStream output = new FileOutputStream(NewFile);

            byte[] data  = new byte[lenghtOfFile];
            long   total = 0;

            int count;

            //COMENSAMOS A LEER LOS DATOS DE NUESTRO INPUTSTREAM
            while ((count = input.Read(data)) != -1)
            {
                total += count;
                //CON ESTA OPCION REPORTAMOS EL PROGRESO DE LA DESCARGA EN PORCENTAJE A NUESTRO CONTROL
                //QUE SE ENCUENTRA EN EL HILO PRINCIPAL
                RunOnUiThread(() => tem_pcv.setPorcentaje((int)((total * 100) / lenghtOfFile)));

                //ESCRIBIMOS LOS DATOS DELIDOS ES NUESTRO OUTPUTSTREAM
                output.Write(data, 0, count);
            }
            output.Flush();
            output.Close();
            input.Close();

            //INDICAMOS A NUESTRO PROGRESS QUE SE HA COMPLETADO LA DESCARGA AL 100%
            RunOnUiThread(() => tem_pcv.setPorcentaje(100));
        }
コード例 #7
0
        private static Bitmap DecodeBitmap(URLConnection connection)
        {
            connection.DoInput = true;
            connection.Connect();
            var bitmap = BitmapFactory.DecodeStream(connection.InputStream);

            connection.Dispose();
            return(bitmap);
        }
コード例 #8
0
ファイル: DownloadHelper.cs プロジェクト: ssorrrell/Thoth
        private string DownloadFile(string sUrl, string filePath)
        {
            try
            {
                //get result from uri
                URL           url        = new Java.Net.URL(sUrl);
                URLConnection connection = url.OpenConnection();
                connection.Connect();

                // this will be useful so that you can show a tipical 0-100%
                // progress bar
                int lengthOfFile = connection.ContentLength;

                // download the file
                InputStream input = new BufferedInputStream(url.OpenStream(), 8192);

                // Output stream
                Log.WriteLine(LogPriority.Info, "DownloadFile FilePath ", filePath);
                OutputStream output = new FileOutputStream(filePath);

                byte[] data = new byte[1024];

                long total = 0;

                int count;
                while ((count = input.Read(data)) != -1)
                {
                    total += count;
                    // publishing the progress....
                    // After this onProgressUpdate will be called
                    //publishProgress("" + (int)((total * 100) / lengthOfFile));
                    if (total % 10 == 10)                     //log for every 10th increment
                    {
                        Log.WriteLine(LogPriority.Info, "DownloadFile Progress ", "" + (int)((total * 100) / lengthOfFile));
                    }

                    // writing data to file
                    output.Write(data, 0, count);
                }

                // flushing output
                output.Flush();

                // closing streams
                output.Close();
                input.Close();
            }
            catch (Exception ex)
            {
                Log.WriteLine(LogPriority.Error, "DownloadFile Error", ex.Message);
            }
            return(filePath);
        }
コード例 #9
0
        /// <exception cref="System.Exception"/>
        public virtual void TestDynamicLogLevel()
        {
            string logName = typeof(TestLogLevel).FullName;

            Org.Apache.Commons.Logging.Log testlog = LogFactory.GetLog(logName);
            //only test Log4JLogger
            if (testlog is Log4JLogger)
            {
                Logger log = ((Log4JLogger)testlog).GetLogger();
                log.Debug("log.debug1");
                log.Info("log.info1");
                log.Error("log.error1");
                Assert.True(!Level.Error.Equals(log.GetEffectiveLevel()));
                HttpServer2 server = new HttpServer2.Builder().SetName("..").AddEndpoint(new URI(
                                                                                             "http://*****:*****@out.WriteLine("*** Connecting to " + url);
                URLConnection connection = url.OpenConnection();
                connection.Connect();
                BufferedReader @in = new BufferedReader(new InputStreamReader(connection.GetInputStream
                                                                                  ()));
                for (string line; (line = @in.ReadLine()) != null; @out.WriteLine(line))
                {
                }
                @in.Close();
                log.Debug("log.debug2");
                log.Info("log.info2");
                log.Error("log.error2");
                Assert.True(Level.Error.Equals(log.GetEffectiveLevel()));
                //command line
                string[] args = new string[] { "-setlevel", authority, logName, Level.Debug.ToString
                                                   () };
                LogLevel.Main(args);
                log.Debug("log.debug3");
                log.Info("log.info3");
                log.Error("log.error3");
                Assert.True(Level.Debug.Equals(log.GetEffectiveLevel()));
            }
            else
            {
                @out.WriteLine(testlog.GetType() + " not tested.");
            }
        }
コード例 #10
0
        private static void downloadFile(Context context, string urlString, string FileName)
        {
            URL url = new URL(urlString);
            // 创建连接
            URLConnection conn = url.OpenConnection();

            //     Java.Net.HttpURLConnection conn = (HttpURLConnection) url.OpenConnection();
            conn.Connect();
            // 获取文件大小
            int length = conn.ContentLength;

            // 创建输入流
            //	FileInputStream getdataInputStream=conn.InputStream;
            //  InputStream getdataInputStream =conn.InputStream;
            System.IO.Stream getdataInputStream = conn.InputStream;
            Java.IO.File     apkFile            = new Java.IO.File(setMkdir(context), FileName);
            FileOutputStream fos = new FileOutputStream(apkFile);
            int count            = 0;

            // 缓存
            byte[] buf = new byte[1024];
            // 写入到文件中
            do
            {
                UTF8Encoding enc     = new UTF8Encoding();
                int          numread = getdataInputStream.Read(buf, 0, 1024);
                //
                count += numread;

                // 计算进度条位置
                //      progress = (int)(((float) count / length) * 100);
                // 更新进度
                //      mHandler.sendEmptyMessage(DOWNLOAD);
                if (numread <= 0)
                {
                    // 下载完成
                    //          mHandler.sendEmptyMessage(DOWNLOAD_FINISH);
                    break;
                }
                // 写入文件
                fos.Write(buf, 0, numread);
            } while (true);// 点击取消就停止下载.
            fos.Close();
            getdataInputStream.Close();
        }
コード例 #11
0
 private static bool CanAccess(string scheme, IPEndPoint addr)
 {
     if (addr == null)
     {
         return(false);
     }
     try
     {
         Uri           url  = new Uri(scheme + "://" + NetUtils.GetHostPortString(addr));
         URLConnection conn = connectionFactory.OpenConnection(url);
         conn.Connect();
         conn.GetContent();
     }
     catch (Exception)
     {
         return(false);
     }
     return(true);
 }
コード例 #12
0
            protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params)
            {
                //REALIZAMOS EL PROCESO DE DESCARGA DEL ARCHIVO
                try{
                    string       filePath  = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).AbsolutePath + "/DescargaImagen/";
                    Java.IO.File directory = new Java.IO.File(filePath);
                    if (directory.Exists() == false)
                    {
                        directory.Mkdir();
                    }

                    //RECUPERAMOS LA DIRECCION DEL ARCHIVO QUE DESEAMOS DESCARGAR
                    string        url_link = @params [0].ToString();
                    URL           url      = new URL(url_link);
                    URLConnection conexion = url.OpenConnection();
                    conexion.Connect();

                    int         lenghtOfFile = conexion.ContentLength;
                    InputStream input        = new BufferedInputStream(url.OpenStream());

                    string       NewImageFile = directory.AbsolutePath + "/" + url_link.Substring(url_link.LastIndexOf("/") + 1);
                    OutputStream output       = new FileOutputStream(NewImageFile);
                    byte[]       data         = new byte[lenghtOfFile];


                    int count;
                    while ((count = input.Read(data)) != -1)
                    {
                        output.Write(data, 0, count);
                    }
                    output.Flush();
                    output.Close();
                    input.Close();
                    return(true);
                }catch {
                    return(false);
                }
            }
コード例 #13
0
ファイル: LogLevel.cs プロジェクト: orf53975/hadoop.net
 private static void Process(string urlstring)
 {
     try
     {
         Uri url = new Uri(urlstring);
         System.Console.Out.WriteLine("Connecting to " + url);
         URLConnection connection = url.OpenConnection();
         connection.Connect();
         BufferedReader @in = new BufferedReader(new InputStreamReader(connection.GetInputStream
                                                                           (), Charsets.Utf8));
         for (string line; (line = @in.ReadLine()) != null;)
         {
             if (line.StartsWith(Marker))
             {
                 System.Console.Out.WriteLine(Tag.Matcher(line).ReplaceAll(string.Empty));
             }
         }
         @in.Close();
     }
     catch (IOException ioe)
     {
         System.Console.Error.WriteLine(string.Empty + ioe);
     }
 }
コード例 #14
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="id"></param>
        /// <param name="mirrorNames"></param>
        /// <param name="mirrorUrls"></param>
        /// <param name="mirror"></param>
        /// <param name="title"></param>
        /// <param name="path">FULL PATH</param>
        /// <param name="poster"></param>
        /// <param name="fileName"></param>
        /// <param name="beforeTxt"></param>
        /// <param name="openWhenDone"></param>
        /// <param name="showNotificaion"></param>
        /// <param name="showDoneNotificaion"></param>
        /// <param name="showDoneAsToast"></param>
        /// <param name="resumeIntent"></param>
        public static void HandleIntent(int id, List <BasicMirrorInfo> mirrors, int mirror, string title, string path, string poster, string beforeTxt, bool openWhenDone, bool showNotificaion, bool showDoneNotificaion, bool showDoneAsToast, bool resumeIntent)
        {
            const int UPDATE_TIME = 1;

            try {
                isStartProgress[id] = true;
                print("START DLOAD::: " + id);
                if (isPaused.ContainsKey(id))
                {
                    //if (isPaused[id] == 2) {
                    //  isPaused.Remove(id);
                    //    print("YEET DELETED KEEEE");
                    return;
                    //  }
                }
                var context = Application.Context;

                //$"{nameof(id)}={id}|||{nameof(title)}={title}|||{nameof(path)}={path}|||{nameof(poster)}={poster}|||{nameof(fileName)}={fileName}|||{nameof(beforeTxt)}={beforeTxt}|||{nameof(openWhenDone)}={openWhenDone}|||{nameof(showDoneNotificaion)}={showDoneNotificaion}|||{nameof(showDoneAsToast)}={showDoneAsToast}|||");

                int progress = 0;

                int bytesPerSec = 0;

                void UpdateDloadNot(string progressTxt)
                {
                    //poster != ""
                    if (!isPaused.ContainsKey(id))
                    {
                        isPaused[id] = 0;
                    }
                    try {
                        int  isPause  = isPaused[id];
                        bool canPause = isPause == 0;
                        if (isPause != 2)
                        {
                            ShowLocalNot(new LocalNot()
                            {
                                actions = new List <LocalAction>()
                                {
                                    new LocalAction()
                                    {
                                        action = $"handleDownload|||id={id}|||dType={(canPause ? 1 : 0)}|||", name = canPause ? "Pause" : "Resume"
                                    }, new LocalAction()
                                    {
                                        action = $"handleDownload|||id={id}|||dType=2|||", name = "Stop"
                                    }
                                }, mediaStyle = false, bigIcon = poster, title = $"{title} - {ConvertBytesToAny(bytesPerSec / UPDATE_TIME, 2, 2)} MB/s", autoCancel = false, showWhen = false, onGoing = canPause, id = id, smallIcon = PublicNot, progress = progress, body = progressTxt
                            }, context);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   //canPause
                        }
                    }
                    catch (Exception _ex) {
                        print("ERRORLOADING PROGRESS:::" + _ex);
                    }
                }

                void ShowDone(bool succ, string overrideText = null)
                {
                    print("DAAAAAAAAAASHOW DONE" + succ);
                    if (succ)
                    {
                        App.RemoveKey(DOWNLOAD_KEY, id.ToString());
                        App.RemoveKey(DOWNLOAD_KEY_INTENT, id.ToString());
                    }
                    if (showDoneNotificaion)
                    {
                        print("DAAAAAAAAAASHOW DONE!!!!");
                        Device.BeginInvokeOnMainThread(() => {
                            try {
                                print("DAAAAAAAAAASHOW DONE222");
                                ShowLocalNot(new LocalNot()
                                {
                                    mediaStyle = poster != "", bigIcon = poster, title = title, autoCancel = true, onGoing = false, id = id, smallIcon = PublicNot, body = overrideText ?? (succ ? "Download done!" : "Download Failed")
                                }, context);                                                                                                                                                                                                                                                                    // ((e.Cancelled || e.Error != null) ? "Download Failed!"
                            }
                            catch (Exception _ex) {
                                print("SUPERFATALEX: " + _ex);
                            }
                        });
                        //await Task.Delay(1000); // 100% sure that it is downloaded
                        OnSomeDownloadFinished?.Invoke(null, EventArgs.Empty);
                    }
                    else
                    {
                        print("DONT SHOW WHEN DONE");
                    }
                    // Toast.MakeText(context, "PG DONE!!!", ToastLength.Long).Show();
                }



                void StartT()
                {
                    isStartProgress[id] = true;
                    if (isPaused.ContainsKey(id))
                    {
                        //if (isPaused[id] == 2) {
                        //    isPaused.Remove(id);
                        return;
                        //  }
                    }

                    Thread t = new Thread(() => {
                        print("START:::");
                        string json = JsonConvert.SerializeObject(new DownloadHandleNot()
                        {
                            id = id, mirrors = mirrors, showDoneAsToast = showDoneAsToast, openWhenDone = openWhenDone, showDoneNotificaion = showDoneNotificaion, beforeTxt = beforeTxt, mirror = mirror, path = path, poster = poster, showNotificaion = showNotificaion, title = title
                        });

                        App.SetKey(DOWNLOAD_KEY_INTENT, id.ToString(), json);

                        var mirr = mirrors[mirror];

                        string url     = mirr.mirror;
                        string urlName = mirr.name;
                        string referer = mirr.referer ?? "";
                        bool isM3u8    = url.Contains(".m3u8");

                        if ((int)Android.OS.Build.VERSION.SdkInt > 9)
                        {
                            StrictMode.ThreadPolicy policy = new
                                                             StrictMode.ThreadPolicy.Builder().PermitAll().Build();
                            StrictMode.SetThreadPolicy(policy);
                        }
                        long total     = 0;
                        int fileLength = 0;

                        void UpdateProgress()
                        {
                            UpdateDloadNot($"{beforeTxt.Replace("{name}", urlName)}{progress} % ({ConvertBytesToAny(total, 1, 2)} MB/{ConvertBytesToAny(fileLength, 1, 2)} MB)");
                        }

                        void UpdateFromId(object sender, int _id)
                        {
                            if (_id == id)
                            {
                                UpdateProgress();
                            }
                        }

                        bool removeKeys = true;
                        var rFile       = new Java.IO.File(path);

                        try {
                            // CREATED DIRECTORY IF NEEDED
                            try {
                                Java.IO.File __file = new Java.IO.File(path);
                                __file.Mkdirs();
                            }
                            catch (Exception _ex) {
                                print("FAILED:::" + _ex);
                            }

                            URL _url = new URL(url);

                            URLConnection connection = _url.OpenConnection();

                            print("SET CONNECT ::");
                            if (!rFile.Exists())
                            {
                                print("FILE DOSENT EXITS");
                                rFile.CreateNewFile();
                            }
                            else
                            {
                                if (resumeIntent)
                                {
                                    total = rFile.Length();
                                    connection.SetRequestProperty("Range", "bytes=" + rFile.Length() + "-");
                                }
                                else
                                {
                                    rFile.Delete();
                                    rFile.CreateNewFile();
                                }
                            }
                            print("SET CONNECT ::2");
                            connection.SetRequestProperty("Accept-Encoding", "identity");
                            if (referer != "")
                            {
                                connection.SetRequestProperty("Referer", referer);
                            }
                            int clen = 0;

                            if (isM3u8)
                            {
                                clen = 1;
                            }
                            else
                            {
                                bool Completed = ExecuteWithTimeLimit(TimeSpan.FromMilliseconds(10000), () => {
                                    connection.Connect();
                                    clen = connection.ContentLength;
                                    if (clen < 5000000 && !path.Contains("/YouTube/"))                                       // min of 5 MB
                                    {
                                        clen = 0;
                                    }
                                });
                                if (!Completed)
                                {
                                    print("FAILED MASS");
                                    clen = 0;
                                }
                                print("SET CONNECT ::3");
                            }

                            print("TOTALLLLL::: " + clen);

                            if (clen == 0)
                            {
                                if (isStartProgress.ContainsKey(id))
                                {
                                    isStartProgress.Remove(id);
                                }
                                if (mirror < mirrors.Count - 1 && progress < 2 && rFile.Length() < 1000000)                                   // HAVE MIRRORS LEFT
                                {
                                    mirror++;
                                    removeKeys   = false;
                                    resumeIntent = false;
                                    rFile.Delete();

                                    print("DELETE;;;");
                                }
                                else
                                {
                                    ShowDone(false);
                                }
                            }
                            else
                            {
                                fileLength = clen + (int)total;
                                print("FILELEN:::: " + fileLength);
                                App.SetKey("dlength", "id" + id, fileLength);
                                string fileExtension = MimeTypeMap.GetFileExtensionFromUrl(url);
                                InputStream input    = new BufferedInputStream(connection.InputStream);

                                //long skip = App.GetKey<long>(DOWNLOAD_KEY, id.ToString(), 0);

                                OutputStream output = new FileOutputStream(rFile, true);

                                outputStreams[id] = output;
                                inputStreams[id]  = input;

                                if (isPaused.ContainsKey(id))
                                {
                                    //if (isPaused[id] == 2) {
                                    //    isPaused.Remove(id);
                                    return;
                                    //  }
                                }

                                isPaused[id] = 0;
                                activeIds.Add(id);

                                int m3u8Progress = 0;

                                int cProgress()
                                {
                                    if (isM3u8)
                                    {
                                        return(m3u8Progress);
                                    }
                                    return((int)(total * 100 / fileLength));
                                }
                                progress = cProgress();

                                byte[] data = new byte[1024];
                                // skip;
                                int count;
                                int previousProgress = 0;
                                UpdateDloadNot(total == 0 ? "Download starting" : "Download resuming");

                                System.DateTime lastUpdateTime = System.DateTime.Now;
                                long lastTotal = total;

                                changedPause += UpdateFromId;

                                if (isStartProgress.ContainsKey(id))
                                {
                                    isStartProgress.Remove(id);
                                }
                                bool showDone = true;

                                bool WriteDataUpdate()
                                {
                                    progressDownloads[id] = total;
                                    progress = cProgress();

                                    if (isPaused[id] == 1)
                                    {
                                        print("PAUSEDOWNLOAD");
                                        UpdateProgress();
                                        while (isPaused[id] == 1)
                                        {
                                            Thread.Sleep(100);
                                        }
                                        if (isPaused[id] != 2)
                                        {
                                            UpdateProgress();
                                        }
                                    }
                                    if (isPaused[id] == 2)                                       // DELETE FILE
                                    {
                                        print("DOWNLOAD STOPPED");
                                        ShowDone(false, "Download Stopped");
                                        output.Flush();
                                        output.Close();
                                        input.Close();
                                        outputStreams.Remove(id);
                                        inputStreams.Remove(id);
                                        isPaused.Remove(id);
                                        rFile.Delete();
                                        App.RemoveKey(DOWNLOAD_KEY, id.ToString());
                                        App.RemoveKey(DOWNLOAD_KEY_INTENT, id.ToString());
                                        App.RemoveKey(App.hasDownloadedFolder, id.ToString());
                                        App.RemoveKey("dlength", "id" + id);
                                        App.RemoveKey("DownloadIds", id.ToString());
                                        changedPause -= UpdateFromId;
                                        activeIds.Remove(id);
                                        removeKeys = true;
                                        OnSomeDownloadFailed?.Invoke(null, EventArgs.Empty);
                                        Thread.Sleep(100);
                                        return(true);
                                    }

                                    if (DateTime.Now.Subtract(lastUpdateTime).TotalSeconds > UPDATE_TIME)
                                    {
                                        lastUpdateTime = DateTime.Now;
                                        long diff      = total - lastTotal;
                                        //  UpdateDloadNot($"{ConvertBytesToAny(diff/UPDATE_TIME, 2,2)}MB/s | {progress}%");
                                        //{ConvertBytesToAny(diff / UPDATE_TIME, 2, 2)}MB/s |
                                        if (progress >= 100)
                                        {
                                            print("DLOADPG DONE!");
                                            ShowDone(true);
                                        }
                                        else
                                        {
                                            UpdateProgress();
                                            // UpdateDloadNot(progress + "%");
                                        }
                                        bytesPerSec = 0;

                                        lastTotal = total;
                                    }

                                    if (progress >= 100 || progress > previousProgress)
                                    {
                                        // Only post progress event if we've made progress.
                                        previousProgress = progress;
                                        if (progress >= 100)
                                        {
                                            print("DLOADPG DONE!");
                                            ShowDone(true);
                                            showDone = false;
                                        }
                                        else
                                        {
                                            // UpdateProgress();
                                            // UpdateDloadNot(progress + "%");
                                        }
                                    }
                                    return(false);
                                }


                                void OnError()
                                {
                                    showDone = false;
                                    ShowDone(false, "Download Failed");
                                }

                                if (isM3u8)
                                {
                                    var links   = ParseM3u8(url, referer);
                                    int counter = 0;
                                    byte[] buffer;
                                    try {
                                        while ((buffer = CloudStreamCore.DownloadByteArrayFromUrl(links[counter], referer)) != null)
                                        {
                                            counter++;
                                            m3u8Progress = counter * 100 / links.Length;
                                            count        = buffer.Length;
                                            total       += count;
                                            bytesPerSec += count;
                                            output.Write(buffer, 0, count);
                                            if (WriteDataUpdate())
                                            {
                                                return;
                                            }
                                        }
                                    }
                                    catch (Exception) {
                                        OnError();
                                    }
                                }
                                else
                                {
                                    try {
                                        while ((count = input.Read(data)) != -1)
                                        {
                                            total       += count;
                                            bytesPerSec += count;

                                            output.Write(data, 0, count);
                                            if (WriteDataUpdate())
                                            {
                                                return;
                                            }
                                        }
                                    }
                                    catch (Exception) {
                                        OnError();
                                    }
                                }

                                if (showDone)
                                {
                                    ShowDone(true);
                                }
                                output.Flush();
                                output.Close();
                                input.Close();
                                outputStreams.Remove(id);
                                inputStreams.Remove(id);
                                activeIds.Remove(id);
                            }
                        }
                        catch (Exception _ex) {
                            print("DOWNLOADURL: " + url);
                            print("DOWNLOAD FAILED BC: " + _ex);
                            if (mirror < mirrors.Count - 1 && progress < 2)                               // HAVE MIRRORS LEFT
                            {
                                mirror++;
                                removeKeys   = false;
                                resumeIntent = false;
                                rFile.Delete();
                            }
                            else
                            {
                                ShowDone(false);
                            }
                        }
                        finally {
                            changedPause -= UpdateFromId;
                            isPaused.Remove(id);
                            if (isStartProgress.ContainsKey(id))
                            {
                                isStartProgress.Remove(id);
                            }
                            if (removeKeys)
                            {
                                //App.RemoveKey(DOWNLOAD_KEY, id.ToString());
                                //App.RemoveKey(DOWNLOAD_KEY_INTENT, id.ToString());
                            }
                            else
                            {
                                StartT();
                            }
                        }
                    });

                    t.Start();
                }
                StartT();
            }
            catch (Exception) {
            }
        }
コード例 #15
0
ファイル: urlConnection.cs プロジェクト: iLanceS/fastCSharp
 public static void connect(this URLConnection url)
 {
     url.Connect();
 }
コード例 #16
0
ファイル: Fetcher.cs プロジェクト: orf53975/hadoop.net
        /// <summary>
        /// The connection establishment is attempted multiple times and is given up
        /// only on the last failure.
        /// </summary>
        /// <remarks>
        /// The connection establishment is attempted multiple times and is given up
        /// only on the last failure. Instead of connecting with a timeout of
        /// X, we try connecting with a timeout of x &lt; X but multiple times.
        /// </remarks>
        /// <exception cref="System.IO.IOException"/>
        private void Connect(URLConnection connection, int connectionTimeout)
        {
            int unit = 0;

            if (connectionTimeout < 0)
            {
                throw new IOException("Invalid timeout " + "[timeout = " + connectionTimeout + " ms]"
                                      );
            }
            else
            {
                if (connectionTimeout > 0)
                {
                    unit = Math.Min(UnitConnectTimeout, connectionTimeout);
                }
            }
            long startTime = Time.MonotonicNow();
            long lastTime  = startTime;
            int  attempts  = 0;

            // set the connect timeout to the unit-connect-timeout
            connection.SetConnectTimeout(unit);
            while (true)
            {
                try
                {
                    attempts++;
                    connection.Connect();
                    break;
                }
                catch (IOException ioe)
                {
                    long currentTime            = Time.MonotonicNow();
                    long retryTime              = currentTime - startTime;
                    long leftTime               = connectionTimeout - retryTime;
                    long timeSinceLastIteration = currentTime - lastTime;
                    // throw an exception if we have waited for timeout amount of time
                    // note that the updated value if timeout is used here
                    if (leftTime <= 0)
                    {
                        int retryTimeInSeconds = (int)retryTime / 1000;
                        Log.Error("Connection retry failed with " + attempts + " attempts in " + retryTimeInSeconds
                                  + " seconds");
                        throw;
                    }
                    // reset the connect timeout for the last try
                    if (leftTime < unit)
                    {
                        unit = (int)leftTime;
                        // reset the connect time out for the final connect
                        connection.SetConnectTimeout(unit);
                    }
                    if (timeSinceLastIteration < unit)
                    {
                        try
                        {
                            // sleep the left time of unit
                            Sleep(unit - timeSinceLastIteration);
                        }
                        catch (Exception)
                        {
                            Log.Warn("Sleep in connection retry get interrupted.");
                            if (stopped)
                            {
                                return;
                            }
                        }
                    }
                    // update the total remaining connect-timeout
                    lastTime = Time.MonotonicNow();
                }
            }
        }
コード例 #17
0
        // 1. Create the input
        // 1.1 Create a protocol buffer
        // 1.2 Create the query params
        // 2. Create a connection
        // 3. Do the annotation
        //    This method has two contracts:
        //    1. It should call the two relevant callbacks
        //    2. It must not throw an exception
        /// <summary>Actually try to perform the annotation on the server side.</summary>
        /// <remarks>
        /// Actually try to perform the annotation on the server side.
        /// This is factored out so that we can retry up to 3 times.
        /// </remarks>
        /// <param name="annotation">The annotation we need to fill.</param>
        /// <param name="backend">The backend we are querying against.</param>
        /// <param name="serverURL">The URL of the server we are hitting.</param>
        /// <param name="message">The message we are sending the server (don't need to recompute each retry).</param>
        /// <param name="tries">The number of times we've tried already.</param>
        private void DoAnnotation(Annotation annotation, StanfordCoreNLPClient.Backend backend, URL serverURL, byte[] message, int tries)
        {
            try
            {
                // 1. Set up the connection
                URLConnection connection = serverURL.OpenConnection();
                // 1.1 Set authentication
                if (apiKey != null && apiSecret != null)
                {
                    string userpass  = apiKey + ":" + apiSecret;
                    string basicAuth = "Basic " + Sharpen.Runtime.GetStringForBytes(Base64.GetEncoder().Encode(Sharpen.Runtime.GetBytesForString(userpass)));
                    connection.SetRequestProperty("Authorization", basicAuth);
                }
                // 1.2 Set some protocol-independent properties
                connection.SetDoOutput(true);
                connection.SetRequestProperty("Content-Type", "application/x-protobuf");
                connection.SetRequestProperty("Content-Length", int.ToString(message.Length));
                connection.SetRequestProperty("Accept-Charset", "utf-8");
                connection.SetRequestProperty("User-Agent", typeof(StanfordCoreNLPClient).FullName);
                switch (backend.protocol)
                {
                case "https":
                case "http":
                {
                    // 1.3 Set some protocol-dependent properties
                    ((HttpURLConnection)connection).SetRequestMethod("POST");
                    break;
                }

                default:
                {
                    throw new InvalidOperationException("Haven't implemented protocol: " + backend.protocol);
                }
                }
                // 2. Annotate
                // 2.1. Fire off the request
                connection.Connect();
                connection.GetOutputStream().Write(message);
                connection.GetOutputStream().Flush();
                // 2.2 Await a response
                // -- It might be possible to send more than one message, but we are not going to do that.
                Annotation response = serializer.Read(connection.GetInputStream()).first;
                // 2.3. Copy response over to original annotation
                foreach (Type key in response.KeySet())
                {
                    annotation.Set(key, response.Get(key));
                }
            }
            catch (Exception t)
            {
                // 3. We encountered an error -- retry
                if (tries < 3)
                {
                    log.Warn(t);
                    DoAnnotation(annotation, backend, serverURL, message, tries + 1);
                }
                else
                {
                    throw new Exception(t);
                }
            }
        }