I'm trying to find bugs in on of our legacy code using findBugs. In one the methods, findBugs is giving OBL_UNSATISFIED_OBLIGATION error. I have verified that all streams are properly closed. Here is the code snippet:
FileWriter fw = null;
FileReader fr = null;
try {
if (!new File(filePath).exists()) {
requiredStrings = CommandUtils.invoke(filename);
fw = new FileWriter(filePath);
fw.write(requiredStrings);
} else {
StringBuilder sb = new StringBuilder();
fr = new FileReader(filePath);
char[] buffer = new char[BLOCK_READ_SIZE];
int bytesRead;
while (-1 != (bytesRead = fr.read(buffer, 0, BLOCK_READ_SIZE))) {
sb.append(buffer, 0, bytesRead);
}
requiredStrings = sb.toString();
}
} finally {
if (fw != null) {
fw.close();
}
if (fr != null) {
fr.close();
}
}
return requiredStrings;
The error says that Obligation to clean up resurces in not discharged, Path continues at ....line.... Remaining obligations {Reader x 1, Writer x-1}
close()
so thatfr
might be left unclosed iffw.close()
throws. Also, " the false-positive suppression heuristics for this bug pattern have not been extensively tuned, so reports about false positives are helpful to us." – Confessedly