Skip to content

Commit 78f02f0

Browse files
committed
Merge pull request #13 from alankstewart/master
Fix for #7
2 parents 7aad2bd + bb0346c commit 78f02f0

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/main/java/com/jezhumble/javasysmon/FileUtils.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,16 @@ public String slurp(String fileName) throws IOException {
5050
public byte[] slurpToByteArray(String fileName) throws IOException {
5151
File fileToRead = new File(fileName);
5252
byte[] contents = new byte[(int) fileToRead.length()];
53-
final InputStream inputStream = new FileInputStream(fileToRead);
54-
inputStream.read(contents);
55-
return contents;
53+
InputStream inputStream = null;
54+
try {
55+
inputStream = new FileInputStream(fileToRead);
56+
inputStream.read(contents);
57+
return contents;
58+
} finally {
59+
if (inputStream != null) {
60+
inputStream.close();
61+
}
62+
}
5663
}
5764

5865
/**

src/main/java/com/jezhumble/javasysmon/UnixPasswdParser.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
class UnixPasswdParser {
1010

1111
public HashMap parse(BufferedReader reader) {
12+
if (reader == null) {
13+
System.err.println("Error parsing password file: reader is null");
14+
return new HashMap();
15+
}
16+
1217
HashMap users = new HashMap();
1318
try {
1419
String line;
@@ -22,6 +27,12 @@ public HashMap parse(BufferedReader reader) {
2227
} catch (IOException e) {
2328
System.err.println("Error parsing password file: " + e.getMessage());
2429
return new HashMap();
30+
} finally {
31+
try {
32+
reader.close();
33+
} catch (IOException e) {
34+
System.err.println("Error closing reader: " + e.getMessage());
35+
}
2536
}
2637
}
2738

0 commit comments

Comments
 (0)