Skip to content

Commit

Permalink
Fix resource leak in Metamorph file-maps
Browse files Browse the repository at this point in the history
`FileMap` did not properly close the input file if an error occurred
while opening the streams for reading the file.
  • Loading branch information
cboehme committed Nov 28, 2016
1 parent 447ae60 commit d519e8c
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions src/main/java/org/culturegraph/mf/morph/maps/FileMap.java
Expand Up @@ -18,8 +18,9 @@
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -50,35 +51,24 @@ public void setFiles(final String files) {
}

public void setFile(final String file) {
final BufferedReader reader;
try {
reader = new BufferedReader(new InputStreamReader(
ResourceUtil.getStream(file), "UTF-8"));

try {
String line = null;
while ((line = reader.readLine()) != null) {
if (line.isEmpty()) {
continue;
}
final String[] parts = split.split(line);
if (parts.length == 2) {
map.put(parts[0], parts[1]);
}
try (InputStream stream = ResourceUtil.getStream(file);
BufferedReader reader = new BufferedReader(
new InputStreamReader(stream, StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
if (line.isEmpty()) {
continue;
}
final String[] parts = split.split(line);
if (parts.length == 2) {
map.put(parts[0], parts[1]);
}

} finally {
reader.close();
}

} catch (final UnsupportedEncodingException e) {
throw new MorphException(e);
} catch (final FileNotFoundException e) {
throw new MorphException("resource '" + file + "' not found", e);
} catch (final IOException e) {
throw new MorphException(e);
}

}

public void setSeparator(final String delimiter) {
Expand Down

0 comments on commit d519e8c

Please sign in to comment.