Program: 31972
Project: 31972
Buggy source:
@Override protected Void doInBackground(Integer... params){
  try {
    URL url=new URL(mUrl);
    Log.d(LOG_TAG,"String_Node_Str" + this + "String_Node_Str");
    mDownloadStats=new IMXMediaDownloadListener.DownloadStats();
    mDownloadStats.mEstimatedRemainingTime=-1;
    InputStream stream=null;
    int filelen=-1;
    URLConnection connection=null;
    try {
      connection=url.openConnection();
      if (mHsConfig != null && connection instanceof HttpsURLConnection) {
        HttpsURLConnection sslConn=(HttpsURLConnection)connection;
        try {
          sslConn.setSSLSocketFactory(CertUtil.newPinnedSSLSocketFactory(mHsConfig));
          sslConn.setHostnameVerifier(CertUtil.newHostnameVerifier(mHsConfig));
        }
 catch (        Exception e) {
          Log.e(LOG_TAG,"String_Node_Str" + e.getMessage());
        }
      }
      float scale=(null != mNetworkConnectivityReceiver) ? mNetworkConnectivityReceiver.getTimeoutScale() : 1.0f;
      connection.setReadTimeout((int)(DOWNLOAD_TIME_OUT * scale));
      filelen=connection.getContentLength();
      stream=connection.getInputStream();
    }
 catch (    Exception e) {
      Log.e(LOG_TAG,"String_Node_Str" + e.getMessage());
      InputStream errorStream=((HttpsURLConnection)connection).getErrorStream();
      if (null != errorStream) {
        try {
          BufferedReader streamReader=new BufferedReader(new InputStreamReader(errorStream,"String_Node_Str"));
          StringBuilder responseStrBuilder=new StringBuilder();
          String inputStr;
          while ((inputStr=streamReader.readLine()) != null) {
            responseStrBuilder.append(inputStr);
          }
          mErrorAsJsonElement=new JsonParser().parse(responseStrBuilder.toString());
        }
 catch (        Exception ee) {
          Log.e(LOG_TAG,"String_Node_Str" + ee.getMessage());
        }
      }
      Log.d(LOG_TAG,"String_Node_Str");
synchronized (mUnreachableUrls) {
        mUnreachableUrls.add(mUrl);
      }
    }
    dispatchDownloadStart();
    if (!isDownloadCancelled() && (null == mErrorAsJsonElement)) {
      final long startDownloadTime=System.currentTimeMillis();
      String filename=MXMediaDownloadWorkerTask.buildFileName(mUrl,mMimeType) + "String_Node_Str";
      FileOutputStream fos=new FileOutputStream(new File(mDirectoryFile,filename));
      mDownloadStats.mDownloadId=mUrl;
      mDownloadStats.mProgress=0;
      mDownloadStats.mDownloadedSize=0;
      mDownloadStats.mFileSize=filelen;
      mDownloadStats.mElapsedTime=0;
      mDownloadStats.mEstimatedRemainingTime=-1;
      mDownloadStats.mBitRate=0;
      final android.os.Handler uiHandler=new android.os.Handler(Looper.getMainLooper());
      final Timer refreshTimer=new Timer();
      uiHandler.post(new Runnable(){
        @Override public void run(){
          try {
            refreshTimer.scheduleAtFixedRate(new TimerTask(){
              @Override public void run(){
                uiHandler.post(new Runnable(){
                  @Override public void run(){
                    if (!mIsDone) {
                      publishProgress(startDownloadTime);
                    }
                  }
                }
);
              }
            }
,new java.util.Date(),100);
          }
 catch (          Throwable throwable) {
            Log.e(LOG_TAG,"String_Node_Str" + throwable.getMessage());
          }
        }
      }
);
      try {
        byte[] buf=new byte[DOWNLOAD_BUFFER_READ_SIZE];
        int len;
        while (!isDownloadCancelled() && (len=stream.read(buf)) != -1) {
          fos.write(buf,0,len);
          mDownloadStats.mDownloadedSize+=len;
        }
        if (!isDownloadCancelled()) {
          mDownloadStats.mProgress=100;
        }
      }
 catch (      OutOfMemoryError outOfMemoryError) {
        Log.e(LOG_TAG,"String_Node_Str");
      }
catch (      Exception e) {
        Log.e(LOG_TAG,"String_Node_Str" + e.getMessage());
      }
      mIsDone=true;
      close(stream);
      fos.flush();
      fos.close();
      uiHandler.post(new Runnable(){
        @Override public void run(){
          refreshTimer.cancel();
        }
      }
);
      if ((null != connection) && (connection instanceof HttpsURLConnection)) {
        ((HttpsURLConnection)connection).disconnect();
      }
      if (mDownloadStats.mProgress == 100) {
        try {
          File originalFile=new File(mDirectoryFile,filename);
          String newFileName=MXMediaDownloadWorkerTask.buildFileName(mUrl,mMimeType);
          File newFile=new File(mDirectoryFile,newFileName);
          if (newFile.exists()) {
            mApplicationContext.deleteFile(newFileName);
          }
          originalFile.renameTo(newFile);
        }
 catch (        Exception e) {
          Log.e(LOG_TAG,"String_Node_Str" + e.getMessage());
        }
      }
    }
    if (mDownloadStats.mProgress == 100) {
      Log.d(LOG_TAG,"String_Node_Str" + this + "String_Node_Str");
    }
 else {
      if (null != mErrorAsJsonElement) {
        Log.d(LOG_TAG,"String_Node_Str" + this + "String_Node_Str"+ mErrorAsJsonElement.toString());
      }
 else {
        Log.d(LOG_TAG,"String_Node_Str" + this + "String_Node_Str");
      }
    }
  }
 catch (  Exception e) {
    Log.e(LOG_TAG,"String_Node_Str" + this);
  }
synchronized (mPendingDownloadByUrl) {
    mPendingDownloadByUrl.remove(mUrl);
  }
  return null;
}

Fixed version (ground truth):
@Override protected Void doInBackground(Integer... params){
  MatrixError defaultError=new MatrixError();
  defaultError.errcode=MatrixError.UNKNOWN;
  try {
    URL url=new URL(mUrl);
    Log.d(LOG_TAG,"String_Node_Str" + this + "String_Node_Str");
    mDownloadStats=new IMXMediaDownloadListener.DownloadStats();
    mDownloadStats.mEstimatedRemainingTime=-1;
    InputStream stream=null;
    int filelen=-1;
    URLConnection connection=null;
    try {
      connection=url.openConnection();
      if (mHsConfig != null && connection instanceof HttpsURLConnection) {
        HttpsURLConnection sslConn=(HttpsURLConnection)connection;
        try {
          sslConn.setSSLSocketFactory(CertUtil.newPinnedSSLSocketFactory(mHsConfig));
          sslConn.setHostnameVerifier(CertUtil.newHostnameVerifier(mHsConfig));
        }
 catch (        Exception e) {
          Log.e(LOG_TAG,"String_Node_Str" + e.getMessage());
        }
      }
      float scale=(null != mNetworkConnectivityReceiver) ? mNetworkConnectivityReceiver.getTimeoutScale() : 1.0f;
      connection.setReadTimeout((int)(DOWNLOAD_TIME_OUT * scale));
      filelen=connection.getContentLength();
      stream=connection.getInputStream();
    }
 catch (    Exception e) {
      Log.e(LOG_TAG,"String_Node_Str" + e.getMessage());
      defaultError.error=e.getLocalizedMessage();
      InputStream errorStream=((HttpsURLConnection)connection).getErrorStream();
      if (null != errorStream) {
        try {
          BufferedReader streamReader=new BufferedReader(new InputStreamReader(errorStream,"String_Node_Str"));
          StringBuilder responseStrBuilder=new StringBuilder();
          String inputStr;
          while ((inputStr=streamReader.readLine()) != null) {
            responseStrBuilder.append(inputStr);
          }
          mErrorAsJsonElement=new JsonParser().parse(responseStrBuilder.toString());
        }
 catch (        Exception ee) {
          Log.e(LOG_TAG,"String_Node_Str" + ee.getMessage());
        }
      }
      Log.d(LOG_TAG,"String_Node_Str");
synchronized (mUnreachableUrls) {
        mUnreachableUrls.add(mUrl);
      }
    }
    dispatchDownloadStart();
    if (!isDownloadCancelled() && (null == mErrorAsJsonElement)) {
      final long startDownloadTime=System.currentTimeMillis();
      String filename=MXMediaDownloadWorkerTask.buildFileName(mUrl,mMimeType) + "String_Node_Str";
      FileOutputStream fos=new FileOutputStream(new File(mDirectoryFile,filename));
      mDownloadStats.mDownloadId=mUrl;
      mDownloadStats.mProgress=0;
      mDownloadStats.mDownloadedSize=0;
      mDownloadStats.mFileSize=filelen;
      mDownloadStats.mElapsedTime=0;
      mDownloadStats.mEstimatedRemainingTime=-1;
      mDownloadStats.mBitRate=0;
      final android.os.Handler uiHandler=new android.os.Handler(Looper.getMainLooper());
      final Timer refreshTimer=new Timer();
      uiHandler.post(new Runnable(){
        @Override public void run(){
          try {
            refreshTimer.scheduleAtFixedRate(new TimerTask(){
              @Override public void run(){
                uiHandler.post(new Runnable(){
                  @Override public void run(){
                    if (!mIsDone) {
                      publishProgress(startDownloadTime);
                    }
                  }
                }
);
              }
            }
,new java.util.Date(),100);
          }
 catch (          Throwable throwable) {
            Log.e(LOG_TAG,"String_Node_Str" + throwable.getMessage());
          }
        }
      }
);
      try {
        byte[] buf=new byte[DOWNLOAD_BUFFER_READ_SIZE];
        int len;
        while (!isDownloadCancelled() && (len=stream.read(buf)) != -1) {
          fos.write(buf,0,len);
          mDownloadStats.mDownloadedSize+=len;
        }
        if (!isDownloadCancelled()) {
          mDownloadStats.mProgress=100;
        }
      }
 catch (      OutOfMemoryError outOfMemoryError) {
        Log.e(LOG_TAG,"String_Node_Str");
        defaultError.error=outOfMemoryError.getLocalizedMessage();
      }
catch (      Exception e) {
        Log.e(LOG_TAG,"String_Node_Str" + e.getMessage());
        defaultError.error=e.getLocalizedMessage();
      }
      mIsDone=true;
      close(stream);
      fos.flush();
      fos.close();
      uiHandler.post(new Runnable(){
        @Override public void run(){
          refreshTimer.cancel();
        }
      }
);
      if ((null != connection) && (connection instanceof HttpsURLConnection)) {
        ((HttpsURLConnection)connection).disconnect();
      }
      if (mDownloadStats.mProgress == 100) {
        try {
          File originalFile=new File(mDirectoryFile,filename);
          String newFileName=MXMediaDownloadWorkerTask.buildFileName(mUrl,mMimeType);
          File newFile=new File(mDirectoryFile,newFileName);
          if (newFile.exists()) {
            mApplicationContext.deleteFile(newFileName);
          }
          originalFile.renameTo(newFile);
        }
 catch (        Exception e) {
          Log.e(LOG_TAG,"String_Node_Str" + e.getMessage());
          defaultError.error=e.getLocalizedMessage();
        }
      }
    }
    if (mDownloadStats.mProgress == 100) {
      Log.d(LOG_TAG,"String_Node_Str" + this + "String_Node_Str");
    }
 else {
      if (null != mErrorAsJsonElement) {
        Log.d(LOG_TAG,"String_Node_Str" + this + "String_Node_Str"+ mErrorAsJsonElement.toString());
      }
 else {
        Log.d(LOG_TAG,"String_Node_Str" + this + "String_Node_Str");
      }
    }
  }
 catch (  Exception e) {
    Log.e(LOG_TAG,"String_Node_Str" + this);
    defaultError.error=e.getMessage();
  }
  if (!TextUtils.isEmpty(defaultError.error)) {
    mErrorAsJsonElement=JsonUtils.getGson(false).toJsonTree(defaultError);
  }
synchronized (mPendingDownloadByUrl) {
    mPendingDownloadByUrl.remove(mUrl);
  }
  return null;
}