Esempio n. 1
0
  public int copy(InputStream input, RandomAccessFile out, TTask.Task task)
      throws Exception, IOException {
    this.mBuffer = new byte[8192];
    BufferedInputStream in = new BufferedInputStream(input, 8192);
    TLog.v(this.TAG, "length" + out.length());
    out.seek(out.length());

    int count = 0;
    int byteCount = 0;
    long errorBlockTimePreviousTime = -1L;
    long expireTime = 0L;
    try {
      while (!task.isCancel()) {
        byteCount = in.read(this.mBuffer, 0, 8192);
        if (byteCount == -1) {
          break;
        }
        out.write(this.mBuffer, 0, byteCount);
        count += byteCount;

        if (!TMDownloadManager.getInstance().isOnline()) {
          task.stopTask();
          setErrorCode(2);
          break;
        }

        if (this.mSpeed == 0L) {
          if (errorBlockTimePreviousTime > 0L) {
            expireTime = System.currentTimeMillis() - errorBlockTimePreviousTime;
            if (expireTime > 30000L) {
              setErrorCode(2);
              task.stopTask();
            }
          } else {
            errorBlockTimePreviousTime = System.currentTimeMillis();
          }
        } else {
          expireTime = 0L;
          errorBlockTimePreviousTime = -1L;
        }
      }
    } finally {
      try {
        out.close();
      } catch (IOException e) {
        setErrorCode(3);
        TLog.e(this.TAG, e.getMessage());
      }
      try {
        in.close();
      } catch (IOException e) {
        setErrorCode(3);
        TLog.e(this.TAG, e.getMessage());
      }
    }

    this.mBuffer = null;
    return count;
  }
Esempio n. 2
0
  private void approvePendingReports() {
    TLog.d(TCrash.TAG, "Mark all pending reports as approved.");

    CrashReportFinder reportFinder = new CrashReportFinder();
    String[] reportFileNames = reportFinder.getCrashReportFiles();

    for (String reportFileName : reportFileNames)
      if (!this.fileNameParser.isApproved(reportFileName)) {
        File reportFile = new File(TCrash.getInstance().getFilePath(), reportFileName);

        String newName = reportFileName.replace(".stacktrace", "-approved.stacktrace");

        File newFile = new File(TCrash.getInstance().getFilePath(), newName);
        if (!reportFile.renameTo(newFile))
          TLog.e(
              TCrash.TAG, "Could not rename approved report from " + reportFile + " to " + newFile);
      }
  }
Esempio n. 3
0
  private void checkAndSendReports() {
    TLog.d(TCrash.TAG, "#checkAndSendReports - start");
    CrashReportFinder reportFinder = new CrashReportFinder();
    String[] reportFiles = reportFinder.getCrashReportFiles();
    Arrays.sort(reportFiles);

    int reportsSentCount = 0;

    for (String curFileName : reportFiles)
      if (reportsSentCount > 1) {
        deleteFile(curFileName);
      } else {
        TLog.i(TCrash.TAG, "Sending file " + curFileName);
        try {
          TCrashReportPersister persister = new TCrashReportPersister();
          CrashReportData previousCrashReport = persister.load(curFileName);
          sendCrashReport(previousCrashReport);
          deleteFile(curFileName);
        } catch (RuntimeException e) {
          TLog.e(TCrash.TAG, "Failed to send crash reports for " + curFileName + e.getMessage());
          deleteFile(curFileName);
          break;
        } catch (IOException e) {
          TLog.e(TCrash.TAG, "Failed to load crash report for " + curFileName + e.getMessage());
          deleteFile(curFileName);
          break;
        } catch (ReportSenderException e) {
          TLog.e(TCrash.TAG, "Failed to send crash report for " + curFileName + e.getMessage());
          break;
        }

        reportsSentCount++;
      }
    TLog.d(TCrash.TAG, "#checkAndSendReports - finish");
  }
Esempio n. 4
0
  private void sendCrashReport(CrashReportData errorContent) throws ReportSenderException {
    boolean sentAtLeastOnce = false;
    for (TIReportSender sender : TCrash.getInstance().getReportSenders())
      try {
        sender.send(errorContent);

        sentAtLeastOnce = true;
      } catch (ReportSenderException e) {
        if (!sentAtLeastOnce) {
          throw e;
        }
        TLog.w(
            TCrash.TAG,
            "ReportSender of class "
                + sender.getClass().getName()
                + " failed but other senders completed their task. ACRA will not send this report again.");
      }
  }
Esempio n. 5
0
  private long downloadFile(TTask.Task task) throws Exception {
    this.mAndroidHttpClient = AndroidHttpClient.newInstance(this.TAG);
    HttpGet httpGet = new HttpGet(this.mUrl);
    HttpResponse response = this.mAndroidHttpClient.execute(httpGet);
    this.mTotalSize = response.getEntity().getContentLength();

    File file = new File(this.mFilePath, this.mFileName);

    if ((file.length() > 0L) && (this.mTotalSize > 0L) && (this.mTotalSize > file.length())) {
      httpGet.addHeader("Range", "bytes=" + file.length() + "-");
      this.mPreviousFileSize = file.length();

      this.mAndroidHttpClient.close();
      this.mAndroidHttpClient = AndroidHttpClient.newInstance("DownloadTask");
      response = this.mAndroidHttpClient.execute(httpGet);
      TLog.v(this.TAG, "File is not complete, .");
      TLog.v(
          this.TAG, "download now,File length:" + file.length() + " totalSize:" + this.mTotalSize);
    } else if ((file.exists()) && (this.mTotalSize == file.length())) {
      TLog.v(this.TAG, "Output file already exists. Skipping download.");
      return 0L;
    }

    long storage = TStorageUtils.getAvailableStorage();
    TLog.i(this.TAG, "storage:" + storage + " totalSize:" + this.mTotalSize);
    if (this.mTotalSize - file.length() > storage) {
      setErrorCode(1);
      task.stopTask();
      this.mAndroidHttpClient.close();
      return 0L;
    }
    try {
      this.mRandomAccessFile = new ProgressReportingRandomAccessFile(file, "rw");
    } catch (FileNotFoundException e) {
      TLog.v(this.TAG, "OutputStream Error");
    }

    InputStream input = null;
    try {
      input = response.getEntity().getContent();
    } catch (IOException ex) {
      setErrorCode(3);
      this.mAndroidHttpClient.close();
      TLog.v(this.TAG, "InputStream Error" + ex.getMessage());
      return 0L;
    }

    int bytesCopied = copy(input, this.mRandomAccessFile, task);

    if ((this.mPreviousFileSize + bytesCopied != this.mTotalSize)
        && (this.mTotalSize != -1L)
        && (!task.isCancel())) {
      throw new IOException("Download incomplete: " + bytesCopied + " != " + this.mTotalSize);
    }

    this.mRandomAccessFile.close();
    this.mAndroidHttpClient.close();
    this.mAndroidHttpClient = null;
    this.mRandomAccessFile = null;
    TLog.v(this.TAG, "Download completed successfully.");
    return bytesCopied;
  }
Esempio n. 6
0
 private void deleteFile(String fileName) {
   boolean deleted = TCrash.getInstance().getContext().deleteFile(fileName);
   if (!deleted) TLog.w(TCrash.TAG, "Could not delete error report : " + fileName);
 }