Program: 158284
Project: 158284
Buggy source:
@Override public void run(){
  StringBuilder filePath=new StringBuilder();
  File temporaryFile=null;
  TFile outputFile=null;
  try {
    filePath.append(Constants.IO.imageBaseDir);
    if (!useSpecifiedPaths && card != null) {
      filePath.append(card.hashCode()).append("String_Node_Str").append(card.getName().replace("String_Node_Str","String_Node_Str").replace("String_Node_Str","String_Node_Str")).append("String_Node_Str");
      temporaryFile=new File(filePath.toString());
    }
    String imagePath;
    if (useSpecifiedPaths) {
      if (card != null && card.isToken()) {
        imagePath=CardImageUtils.getTokenBasePath() + actualFilename;
      }
 else       if (card != null) {
        imagePath=CardImageUtils.getImageBasePath() + actualFilename;
      }
 else {
        imagePath=Constants.IO.imageBaseDir;
      }
      String tmpFile=filePath + "String_Node_Str" + actualFilename;
      temporaryFile=new File(tmpFile);
      if (!temporaryFile.exists()) {
        temporaryFile.getParentFile().mkdirs();
      }
    }
 else {
      imagePath=CardImageUtils.generateImagePath(card);
    }
    outputFile=new TFile(imagePath);
    if (!outputFile.exists()) {
      outputFile.getParentFile().mkdirs();
    }
    File existingFile=new File(imagePath.replaceFirst("String_Node_Str","String_Node_Str"));
    if (existingFile.exists()) {
      new TFile(existingFile).cp_rp(outputFile);
synchronized (sync) {
        update(cardIndex + 1,count);
      }
      existingFile.delete();
      File parent=existingFile.getParentFile();
      if (parent != null && parent.isDirectory() && parent.list().length == 0) {
        parent.delete();
      }
      return;
    }
    BufferedOutputStream out;
    boolean useTempFile=false;
    int responseCode=0;
    URLConnection httpConn=null;
    if (temporaryFile != null && temporaryFile.length() > 100) {
      useTempFile=true;
    }
 else {
      httpConn=url.openConnection(p);
      setUpConnection(httpConn);
      httpConn.connect();
      responseCode=((HttpURLConnection)httpConn).getResponseCode();
    }
    if (responseCode == 200 || useTempFile) {
      if (!useTempFile) {
        try (BufferedInputStream in=new BufferedInputStream(((HttpURLConnection)httpConn).getInputStream())){
          out=new BufferedOutputStream(new TFileOutputStream(temporaryFile));
          byte[] buf=new byte[1024];
          int len;
          while ((len=in.read(buf)) != -1) {
            if (cancel) {
              in.close();
              out.flush();
              out.close();
              temporaryFile.delete();
              return;
            }
            out.write(buf,0,len);
          }
        }
         out.flush();
        out.close();
      }
      if (card != null && card.isTwoFacedCard()) {
        BufferedImage image=ImageIO.read(temporaryFile);
        if (image.getHeight() == 470) {
          BufferedImage renderedImage=new BufferedImage(265,370,BufferedImage.TYPE_INT_RGB);
          renderedImage.getGraphics();
          Graphics2D graphics2D=renderedImage.createGraphics();
          if (card.isTwoFacedCard() && card.isSecondSide()) {
            graphics2D.drawImage(image,0,0,265,370,313,62,578,432,null);
          }
 else {
            graphics2D.drawImage(image,0,0,265,370,41,62,306,432,null);
          }
          graphics2D.dispose();
          writeImageToFile(renderedImage,outputFile);
        }
 else {
          outputFile.getParentFile().mkdirs();
          new TFile(temporaryFile).cp_rp(outputFile);
        }
      }
 else {
        outputFile.getParentFile().mkdirs();
        new TFile(temporaryFile).cp_rp(outputFile);
      }
    }
 else {
      if (card != null && !useSpecifiedPaths) {
        logger.warn("String_Node_Str" + card.getName() + (!card.getDownloadName().equals(card.getName()) ? "String_Node_Str" + card.getDownloadName() : "String_Node_Str")+ "String_Node_Str"+ card.getSet()+ "String_Node_Str"+ responseCode+ "String_Node_Str"+ url.toString());
      }
      if (logger.isDebugEnabled()) {
        logger.debug("String_Node_Str" + convertStreamToString(((HttpURLConnection)httpConn).getErrorStream()));
      }
    }
  }
 catch (  AccessDeniedException e) {
    logger.error("String_Node_Str" + (outputFile != null ? outputFile.toString() : "String_Node_Str" + card.getName() + "String_Node_Str"+ card.getSet()+ "String_Node_Str") + "String_Node_Str");
  }
catch (  Exception e) {
    logger.error(e,e);
  }
 finally {
    if (temporaryFile != null) {
    }
  }
synchronized (sync) {
    update(cardIndex + 1,count);
  }
}

Fixed version (ground truth):
@Override public void run(){
  StringBuilder filePath=new StringBuilder();
  File temporaryFile=null;
  TFile outputFile=null;
  try {
    filePath.append(Constants.IO.imageBaseDir);
    if (!useSpecifiedPaths && card != null) {
      filePath.append(card.hashCode()).append("String_Node_Str").append(card.getName().replace("String_Node_Str","String_Node_Str").replace("String_Node_Str","String_Node_Str")).append("String_Node_Str");
      temporaryFile=new File(filePath.toString());
    }
    String imagePath;
    if (useSpecifiedPaths) {
      if (card != null && card.isToken()) {
        imagePath=CardImageUtils.getTokenBasePath() + actualFilename;
      }
 else       if (card != null) {
        imagePath=CardImageUtils.getImageBasePath() + actualFilename;
      }
 else {
        imagePath=Constants.IO.imageBaseDir;
      }
      String tmpFile=filePath + "String_Node_Str" + actualFilename;
      temporaryFile=new File(tmpFile);
      if (!temporaryFile.exists()) {
        temporaryFile.getParentFile().mkdirs();
      }
    }
 else {
      imagePath=CardImageUtils.generateImagePath(card);
    }
    outputFile=new TFile(imagePath);
    if (!outputFile.exists()) {
      outputFile.getParentFile().mkdirs();
    }
    File existingFile=new File(imagePath.replaceFirst("String_Node_Str","String_Node_Str"));
    if (existingFile.exists()) {
      new TFile(existingFile).cp_rp(outputFile);
synchronized (sync) {
        update(cardIndex + 1,count);
      }
      existingFile.delete();
      File parent=existingFile.getParentFile();
      if (parent != null && parent.isDirectory() && parent.list().length == 0) {
        parent.delete();
      }
      return;
    }
    BufferedOutputStream out;
    boolean useTempFile=false;
    int responseCode=0;
    URLConnection httpConn=null;
    if (temporaryFile != null && temporaryFile.length() > 100) {
      useTempFile=true;
    }
 else {
      cardImageSource.doPause(url.getPath());
      httpConn=url.openConnection(p);
      setUpConnection(httpConn);
      httpConn.connect();
      responseCode=((HttpURLConnection)httpConn).getResponseCode();
    }
    if (responseCode == 200 || useTempFile) {
      if (!useTempFile) {
        try (BufferedInputStream in=new BufferedInputStream(((HttpURLConnection)httpConn).getInputStream())){
          out=new BufferedOutputStream(new TFileOutputStream(temporaryFile));
          byte[] buf=new byte[1024];
          int len;
          while ((len=in.read(buf)) != -1) {
            if (cancel) {
              in.close();
              out.flush();
              out.close();
              temporaryFile.delete();
              return;
            }
            out.write(buf,0,len);
          }
        }
         out.flush();
        out.close();
      }
      if (card != null && card.isTwoFacedCard()) {
        BufferedImage image=ImageIO.read(temporaryFile);
        if (image.getHeight() == 470) {
          BufferedImage renderedImage=new BufferedImage(265,370,BufferedImage.TYPE_INT_RGB);
          renderedImage.getGraphics();
          Graphics2D graphics2D=renderedImage.createGraphics();
          if (card.isTwoFacedCard() && card.isSecondSide()) {
            graphics2D.drawImage(image,0,0,265,370,313,62,578,432,null);
          }
 else {
            graphics2D.drawImage(image,0,0,265,370,41,62,306,432,null);
          }
          graphics2D.dispose();
          writeImageToFile(renderedImage,outputFile);
        }
 else {
          outputFile.getParentFile().mkdirs();
          new TFile(temporaryFile).cp_rp(outputFile);
        }
      }
 else {
        outputFile.getParentFile().mkdirs();
        new TFile(temporaryFile).cp_rp(outputFile);
      }
    }
 else {
      if (card != null && !useSpecifiedPaths) {
        logger.warn("String_Node_Str" + card.getName() + (!card.getDownloadName().equals(card.getName()) ? "String_Node_Str" + card.getDownloadName() : "String_Node_Str")+ "String_Node_Str"+ card.getSet()+ "String_Node_Str"+ responseCode+ "String_Node_Str"+ url.toString());
      }
      if (logger.isDebugEnabled()) {
        logger.debug("String_Node_Str" + convertStreamToString(((HttpURLConnection)httpConn).getErrorStream()));
      }
    }
  }
 catch (  AccessDeniedException e) {
    logger.error("String_Node_Str" + (outputFile != null ? outputFile.toString() : "String_Node_Str" + card.getName() + "String_Node_Str"+ card.getSet()+ "String_Node_Str") + "String_Node_Str");
  }
catch (  Exception e) {
    logger.error(e,e);
  }
 finally {
    if (temporaryFile != null) {
    }
  }
synchronized (sync) {
    update(cardIndex + 1,count);
  }
}