changes from review. Fixed issue where getLines(InputStream) was closing

the stream, Should be the caller's responsibility
This commit is contained in:
ghidravore 2020-07-06 18:13:07 -04:00
parent 6ebda45c2f
commit 6639d19e3b
3 changed files with 25 additions and 37 deletions

View file

@ -17,8 +17,7 @@ package ghidra.app.plugin.core.totd;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import docking.ActionContext; import docking.ActionContext;
import docking.action.DockingAction; import docking.action.DockingAction;
@ -83,7 +82,7 @@ public class TipOfTheDayPlugin extends Plugin implements FrontEndOnly {
private List<String> loadTips() throws IOException { private List<String> loadTips() throws IOException {
try (InputStream in = getClass().getResourceAsStream("tips.txt")) { try (InputStream in = getClass().getResourceAsStream("tips.txt")) {
return FileUtilities.getLines(in); return in == null ? Collections.emptyList() : FileUtilities.getLines(in);
} }
} }

View file

@ -30,13 +30,14 @@ public class DirData {
private Integer size; private Integer size;
public static List<DirData> loadTestData(String file) throws IOException { public static List<DirData> loadTestData(String file) throws IOException {
InputStream is = ResourceManager.getResourceAsStream(file); try (InputStream is = ResourceManager.getResourceAsStream(file)) {
List<String> lines = FileUtilities.getLines(is); List<String> lines = FileUtilities.getLines(is);
List<DirData> data = new ArrayList<>(); List<DirData> data = new ArrayList<>();
for (String line : lines) { for (String line : lines) {
data.add(new DirData(line)); data.add(new DirData(line));
}
return data;
} }
return data;
} }
public String getDate() { public String getDate() {

View file

@ -463,19 +463,19 @@ public final class FileUtilities {
return dir.delete(); return dir.delete();
} }
for (int i = 0; i < files.length; i++) { for (File file : files) {
monitor.checkCanceled(); monitor.checkCanceled();
if (files[i].isDirectory()) { if (file.isDirectory()) {
// use a dummy monitor as not to ruin our progress // use a dummy monitor as not to ruin our progress
if (!doDeleteDir(files[i], monitor)) { if (!doDeleteDir(file, monitor)) {
printDebug("Unable to delete directory: " + files[i]); printDebug("Unable to delete directory: " + file);
return false; return false;
} }
} }
else { else {
monitor.setMessage("Deleting file: " + files[i]); monitor.setMessage("Deleting file: " + file);
if (!files[i].delete()) { if (!file.delete()) {
printDebug("Unable to delete file: " + files[i]); printDebug("Unable to delete file: " + file);
return false; return false;
} }
} }
@ -681,13 +681,11 @@ public final class FileUtilities {
* <p> * <p>
* @param file The text file to read in * @param file The text file to read in
* @return a list of file lines * @return a list of file lines
* @throws IOException * @throws IOException if an error occurs trying to read the file.
*/ */
public static List<String> getLines(ResourceFile file) throws IOException { public static List<String> getLines(ResourceFile file) throws IOException {
try { try (InputStream is = file.getInputStream()) {
BufferedReader in = new BufferedReader( return getLines(is);
new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8));
return getLines(in);
} }
catch (FileNotFoundException exc) { catch (FileNotFoundException exc) {
return new ArrayList<>(); return new ArrayList<>();
@ -738,7 +736,7 @@ public final class FileUtilities {
* @throws IOException if there are any issues reading the file * @throws IOException if there are any issues reading the file
*/ */
public static List<String> getLines(InputStream is) throws IOException { public static List<String> getLines(InputStream is) throws IOException {
return getLines(new BufferedReader(new InputStreamReader(is))); return getLines(new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)));
} }
/** /**
@ -790,22 +788,12 @@ public final class FileUtilities {
* @throws IOException if there are any issues reading the file * @throws IOException if there are any issues reading the file
*/ */
public static List<String> getLines(BufferedReader in) throws IOException { public static List<String> getLines(BufferedReader in) throws IOException {
try { List<String> fileLines = new ArrayList<>();
List<String> fileLines = new ArrayList<>(); String line;
String line; while ((line = in.readLine()) != null) {
while ((line = in.readLine()) != null) { fileLines.add(line);
fileLines.add(line);
}
return fileLines;
}
finally {
try {
in.close();
}
catch (IOException e) {
// don't care; we tried
}
} }
return fileLines;
} }
/** /**