Program: 15328
Project: 15328
Buggy source:
private void performDownload(final HTTPBackend.Request request){
  if (mInitiator.queueType == CacheRequest.DOWNLOAD_QUEUE_REDDIT_API) {
    if (resetUserCredentials.getAndSet(false)) {
      mInitiator.user.setAccessToken(null);
    }
    RedditOAuth.AccessToken accessToken=mInitiator.user.getMostRecentAccessToken();
    if (accessToken == null || accessToken.isExpired()) {
      mInitiator.notifyProgress(true,0,0);
      final RedditOAuth.FetchAccessTokenResult result;
      if (mInitiator.user.isAnonymous()) {
        result=RedditOAuth.fetchAnonymousAccessTokenSynchronous(mInitiator.context);
      }
 else {
        result=RedditOAuth.fetchAccessTokenSynchronous(mInitiator.context,mInitiator.user.refreshToken);
      }
      if (result.status != RedditOAuth.FetchAccessTokenResultStatus.SUCCESS) {
        mInitiator.notifyFailure(CacheRequest.REQUEST_FAILURE_REQUEST,result.error.t,result.error.httpStatus,result.error.title + "String_Node_Str" + result.error.message);
        return;
      }
      accessToken=result.accessToken;
      mInitiator.user.setAccessToken(accessToken);
    }
    request.addHeader("String_Node_Str","String_Node_Str" + accessToken.token);
  }
  if (mInitiator.queueType == CacheRequest.DOWNLOAD_QUEUE_IMGUR_API) {
    request.addHeader("String_Node_Str","String_Node_Str");
  }
  mInitiator.notifyDownloadStarted();
  request.executeInThisThread(new HTTPBackend.Listener(){
    @Override public void onError(    final @CacheRequest.RequestFailureType int failureType,    final Throwable exception,    final Integer httpStatus){
      if (mInitiator.queueType == CacheRequest.DOWNLOAD_QUEUE_REDDIT_API && TorCommon.isTorEnabled()) {
        OKHTTPBackend.recreateHttpBackend();
        resetUserCredentialsOnNextRequest();
      }
      mInitiator.notifyFailure(failureType,exception,httpStatus,"String_Node_Str");
    }
    @Override public void onSuccess(    final String mimetype,    final Long bodyBytes,    final InputStream is){
      final NotifyOutputStream cacheOs;
      final CacheManager.WritableCacheFile cacheFile;
      if (mInitiator.cache) {
        try {
          cacheFile=manager.openNewCacheFile(mInitiator,session,mimetype);
          cacheOs=cacheFile.getOutputStream();
        }
 catch (        IOException e) {
          e.printStackTrace();
          mInitiator.notifyFailure(CacheRequest.REQUEST_FAILURE_STORAGE,e,null,"String_Node_Str");
          return;
        }
      }
 else {
        cacheOs=null;
        cacheFile=null;
      }
      if (mInitiator.isJson) {
        final InputStream bis;
        if (mInitiator.cache) {
          bis=new BufferedInputStream(new CachingInputStream(is,cacheOs,new CachingInputStream.BytesReadListener(){
            public void onBytesRead(            final long total){
              if (bodyBytes != null) {
                mInitiator.notifyProgress(false,total,bodyBytes);
              }
            }
          }
),64 * 1024);
        }
 else {
          bis=new BufferedInputStream(is,64 * 1024);
        }
        final JsonValue value;
        try {
          value=new JsonValue(bis);
          mInitiator.notifyJsonParseStarted(value,RRTime.utcCurrentTimeMillis(),session,false);
          value.buildInThisThread();
        }
 catch (        Throwable t) {
          t.printStackTrace();
          mInitiator.notifyFailure(CacheRequest.REQUEST_FAILURE_PARSE,t,null,"String_Node_Str");
          return;
        }
        if (mInitiator.cache && cacheFile != null) {
          try {
            mInitiator.notifySuccess(cacheFile.getReadableCacheFile(),RRTime.utcCurrentTimeMillis(),session,false,mimetype);
          }
 catch (          IOException e) {
            if (e.getMessage().contains("String_Node_Str")) {
              mInitiator.notifyFailure(CacheRequest.REQUEST_FAILURE_DISK_SPACE,e,null,"String_Node_Str");
            }
 else {
              mInitiator.notifyFailure(CacheRequest.REQUEST_FAILURE_STORAGE,e,null,"String_Node_Str");
            }
          }
        }
      }
 else {
        if (!mInitiator.cache) {
          BugReportActivity.handleGlobalError(mInitiator.context,"String_Node_Str");
          return;
        }
        try {
          final byte[] buf=new byte[64 * 1024];
          int bytesRead;
          long totalBytesRead=0;
          while ((bytesRead=is.read(buf)) > 0) {
            totalBytesRead+=bytesRead;
            cacheOs.write(buf,0,bytesRead);
            if (bodyBytes != null) {
              mInitiator.notifyProgress(false,totalBytesRead,bodyBytes);
            }
          }
          cacheOs.flush();
          cacheOs.close();
          try {
            mInitiator.notifySuccess(cacheFile.getReadableCacheFile(),RRTime.utcCurrentTimeMillis(),session,false,mimetype);
          }
 catch (          IOException e) {
            if (e.getMessage().contains("String_Node_Str")) {
              mInitiator.notifyFailure(CacheRequest.REQUEST_FAILURE_DISK_SPACE,e,null,"String_Node_Str");
            }
 else {
              mInitiator.notifyFailure(CacheRequest.REQUEST_FAILURE_STORAGE,e,null,"String_Node_Str");
            }
          }
        }
 catch (        IOException e) {
          if (e.getMessage() != null && e.getMessage().contains("String_Node_Str")) {
            mInitiator.notifyFailure(CacheRequest.REQUEST_FAILURE_STORAGE,e,null,"String_Node_Str");
          }
 else {
            e.printStackTrace();
            mInitiator.notifyFailure(CacheRequest.REQUEST_FAILURE_CONNECTION,e,null,"String_Node_Str");
          }
        }
catch (        Throwable t) {
          t.printStackTrace();
          mInitiator.notifyFailure(CacheRequest.REQUEST_FAILURE_CONNECTION,t,null,"String_Node_Str");
        }
      }
    }
  }
);
}

Fixed version (ground truth):
private void performDownload(final HTTPBackend.Request request){
  if (mInitiator.queueType == CacheRequest.DOWNLOAD_QUEUE_REDDIT_API) {
    if (resetUserCredentials.getAndSet(false)) {
      mInitiator.user.setAccessToken(null);
    }
    RedditOAuth.AccessToken accessToken=mInitiator.user.getMostRecentAccessToken();
    if (accessToken == null || accessToken.isExpired()) {
      mInitiator.notifyProgress(true,0,0);
      final RedditOAuth.FetchAccessTokenResult result;
      if (mInitiator.user.isAnonymous()) {
        result=RedditOAuth.fetchAnonymousAccessTokenSynchronous(mInitiator.context);
      }
 else {
        result=RedditOAuth.fetchAccessTokenSynchronous(mInitiator.context,mInitiator.user.refreshToken);
      }
      if (result.status != RedditOAuth.FetchAccessTokenResultStatus.SUCCESS) {
        mInitiator.notifyFailure(CacheRequest.REQUEST_FAILURE_REQUEST,result.error.t,result.error.httpStatus,result.error.title + "String_Node_Str" + result.error.message);
        return;
      }
      accessToken=result.accessToken;
      mInitiator.user.setAccessToken(accessToken);
    }
    request.addHeader("String_Node_Str","String_Node_Str" + accessToken.token);
  }
  if (mInitiator.queueType == CacheRequest.DOWNLOAD_QUEUE_IMGUR_API) {
    request.addHeader("String_Node_Str","String_Node_Str");
  }
  mInitiator.notifyDownloadStarted();
  request.executeInThisThread(new HTTPBackend.Listener(){
    @Override public void onError(    final @CacheRequest.RequestFailureType int failureType,    final Throwable exception,    final Integer httpStatus){
      if (mInitiator.queueType == CacheRequest.DOWNLOAD_QUEUE_REDDIT_API && TorCommon.isTorEnabled()) {
        OKHTTPBackend.recreateHttpBackend();
        resetUserCredentialsOnNextRequest();
      }
      mInitiator.notifyFailure(failureType,exception,httpStatus,"String_Node_Str");
    }
    @Override public void onSuccess(    final String mimetype,    final Long bodyBytes,    final InputStream is){
      final NotifyOutputStream cacheOs;
      final CacheManager.WritableCacheFile cacheFile;
      if (mInitiator.cache) {
        try {
          cacheFile=manager.openNewCacheFile(mInitiator,session,mimetype);
          cacheOs=cacheFile.getOutputStream();
        }
 catch (        IOException e) {
          e.printStackTrace();
          final int failureType;
          if (manager.getPreferredCacheLocation().exists()) {
            failureType=CacheRequest.REQUEST_FAILURE_STORAGE;
          }
 else {
            failureType=CacheRequest.REQUEST_FAILURE_CACHE_DIR_DOES_NOT_EXIST;
          }
          mInitiator.notifyFailure(failureType,e,null,"String_Node_Str");
          return;
        }
      }
 else {
        cacheOs=null;
        cacheFile=null;
      }
      if (mInitiator.isJson) {
        final InputStream bis;
        if (mInitiator.cache) {
          bis=new BufferedInputStream(new CachingInputStream(is,cacheOs,new CachingInputStream.BytesReadListener(){
            public void onBytesRead(            final long total){
              if (bodyBytes != null) {
                mInitiator.notifyProgress(false,total,bodyBytes);
              }
            }
          }
),64 * 1024);
        }
 else {
          bis=new BufferedInputStream(is,64 * 1024);
        }
        final JsonValue value;
        try {
          value=new JsonValue(bis);
          mInitiator.notifyJsonParseStarted(value,RRTime.utcCurrentTimeMillis(),session,false);
          value.buildInThisThread();
        }
 catch (        Throwable t) {
          t.printStackTrace();
          mInitiator.notifyFailure(CacheRequest.REQUEST_FAILURE_PARSE,t,null,"String_Node_Str");
          return;
        }
        if (mInitiator.cache && cacheFile != null) {
          try {
            mInitiator.notifySuccess(cacheFile.getReadableCacheFile(),RRTime.utcCurrentTimeMillis(),session,false,mimetype);
          }
 catch (          IOException e) {
            if (e.getMessage().contains("String_Node_Str")) {
              mInitiator.notifyFailure(CacheRequest.REQUEST_FAILURE_DISK_SPACE,e,null,"String_Node_Str");
            }
 else {
              mInitiator.notifyFailure(CacheRequest.REQUEST_FAILURE_STORAGE,e,null,"String_Node_Str");
            }
          }
        }
      }
 else {
        if (!mInitiator.cache) {
          BugReportActivity.handleGlobalError(mInitiator.context,"String_Node_Str");
          return;
        }
        try {
          final byte[] buf=new byte[64 * 1024];
          int bytesRead;
          long totalBytesRead=0;
          while ((bytesRead=is.read(buf)) > 0) {
            totalBytesRead+=bytesRead;
            cacheOs.write(buf,0,bytesRead);
            if (bodyBytes != null) {
              mInitiator.notifyProgress(false,totalBytesRead,bodyBytes);
            }
          }
          cacheOs.flush();
          cacheOs.close();
          try {
            mInitiator.notifySuccess(cacheFile.getReadableCacheFile(),RRTime.utcCurrentTimeMillis(),session,false,mimetype);
          }
 catch (          IOException e) {
            if (e.getMessage().contains("String_Node_Str")) {
              mInitiator.notifyFailure(CacheRequest.REQUEST_FAILURE_DISK_SPACE,e,null,"String_Node_Str");
            }
 else {
              mInitiator.notifyFailure(CacheRequest.REQUEST_FAILURE_STORAGE,e,null,"String_Node_Str");
            }
          }
        }
 catch (        IOException e) {
          if (e.getMessage() != null && e.getMessage().contains("String_Node_Str")) {
            mInitiator.notifyFailure(CacheRequest.REQUEST_FAILURE_STORAGE,e,null,"String_Node_Str");
          }
 else {
            e.printStackTrace();
            mInitiator.notifyFailure(CacheRequest.REQUEST_FAILURE_CONNECTION,e,null,"String_Node_Str");
          }
        }
catch (        Throwable t) {
          t.printStackTrace();
          mInitiator.notifyFailure(CacheRequest.REQUEST_FAILURE_CONNECTION,t,null,"String_Node_Str");
        }
      }
    }
  }
);
}