@Override protected TestParameters doInBackground(URL[] paramArrayOfURL) { TestParametersTransfer localTestParametersTransfer = (TestParametersTransfer) getResult(); try { DownloadTestTask.this.setError(SpeedTestError.None); setStartTime(SystemClock.uptimeMillis()); processDownload(paramArrayOfURL[0], localTestParametersTransfer); if (DownloadTestTask.this.getError() == SpeedTestError.None) { localTestParametersTransfer.setSuccess(true); DownloadTestTask.this.success(); setCompleted(true); return localTestParametersTransfer; } else { localTestParametersTransfer.setSuccess(false); DownloadTestTask.this.failed(DownloadTestTask.this.getError()); } } catch (Exception localException) { while (true) { DebugUtil.e("DownloadTestTask" + localException.getMessage()); DownloadTestTask.this.setError(SpeedTestError.TEST_RUN); localTestParametersTransfer.setSuccess(false); DownloadTestTask.this.failed(DownloadTestTask.this.getError()); break; } } return localTestParametersTransfer; }
private void processDownload(URL paramURL, TestParametersTransfer paramTestParametersTransfer) { int totalByte = 0; try { byte[] arrayOfByte = new byte[262144]; URLConnection localURLConnection = paramURL.openConnection(); localURLConnection.setUseCaches(false); localURLConnection.setDoInput(true); localURLConnection.setDoOutput(false); localURLConnection.setConnectTimeout(this.mConnectTimeout); localURLConnection.setReadTimeout(this.mReadTimeout); paramTestParametersTransfer.clearBytes(); paramTestParametersTransfer.clearProgress(); publishProgress(new Void[0]); BufferedInputStream localBufferedInputStream = new BufferedInputStream(localURLConnection.getInputStream(), 262144); int k = 0; long t3 = 0L; long t1 = 150L; // 控制多长时间获取一次数据 while (true) { if ((k != 0) || (getCancelled()) || (getCompleted())) { localBufferedInputStream.close(); break; } int tempByte = localBufferedInputStream.read(arrayOfByte); totalByte += tempByte; long t2 = SystemClock.uptimeMillis(); if ((tempByte == 0) && (t3 > 0L) && (t2 < 200L + t3)) continue; if (t2 > t3 + t1) { paramTestParametersTransfer.setProgress(getProgress(totalByte)); paramTestParametersTransfer.setBytes(totalByte); publishProgress(new Void[0]); t3 = t2; t1 = 30L; } if (tempByte != -1) continue; k = 1; } } catch (IOException localIOException) { DebugUtil.e("DownloadTestTask", "Download test IO failed:" + localIOException); DownloadTestTask.this.setError(SpeedTestError.TEST_RUN_IO); } catch (Exception localException) { DebugUtil.e("DownloadTestTask", "Download test failed" + localException); DownloadTestTask.this.setError(SpeedTestError.TEST_RUN); } }