Skip to content

Commit

Permalink
OPENNLP-849 Improve handling of InputStreams
Browse files Browse the repository at this point in the history
All InputStreams are now using the try-with-resources statement and some streams are now also closed correctly.

git-svn-id: https://svn.apache.org/repos/asf/opennlp/trunk@1744917 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
kottmann committed May 21, 2016
1 parent ddb17e0 commit c904f6f
Showing 1 changed file with 5 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,12 @@ static byte[] openFeatureGeneratorBytes(File featureGenDescriptorFile) {
byte featureGeneratorBytes[] = null;
// load descriptor file into memory
if (featureGenDescriptorFile != null) {
InputStream bytesIn = CmdLineUtil.openInFile(featureGenDescriptorFile);

try {
try (InputStream bytesIn = CmdLineUtil.openInFile(featureGenDescriptorFile)) {
featureGeneratorBytes = ModelUtil.read(bytesIn);
} catch (IOException e) {
throw new TerminateToolException(-1, "IO error while reading training data or indexing data: "
+ e.getMessage(), e);
} finally {
try {
bytesIn.close();
} catch (IOException e) {
// sorry that this can fail
}
}
}
return featureGeneratorBytes;
Expand All @@ -109,16 +102,14 @@ public static Map<String, Object> loadResources(File resourcePath, File featureG
// TODO: If there is descriptor file, it should be consulted too
if (featureGenDescriptor != null) {

InputStream xmlDescriptorIn = CmdLineUtil.openInFile(featureGenDescriptor);

try {
try (InputStream xmlDescriptorIn = CmdLineUtil.openInFile(featureGenDescriptor)) {
artifactSerializers.putAll(GeneratorFactory.extractCustomArtifactSerializerMappings(xmlDescriptorIn));
} catch (IOException e) {
// TODO: Improve error handling!
e.printStackTrace();
}
InputStream inputStreamXML = CmdLineUtil.openInFile(featureGenDescriptor);
try {

try (InputStream inputStreamXML = CmdLineUtil.openInFile(featureGenDescriptor)) {
elements = GeneratorFactory.getDescriptorElements(inputStreamXML);
} catch (IOException e) {
e.printStackTrace();
Expand All @@ -141,21 +132,14 @@ public static Map<String, Object> loadResources(File resourcePath, File featureG
if (serializer == null)
continue;

InputStream resourceIn = CmdLineUtil.openInFile(resourceFile);

try {
try (InputStream resourceIn = CmdLineUtil.openInFile(resourceFile)) {
resources.put(resourceName, serializer.create(resourceIn));
} catch (InvalidFormatException e) {
// TODO: Fix exception handling
e.printStackTrace();
} catch (IOException e) {
// TODO: Fix exception handling
e.printStackTrace();
} finally {
try {
resourceIn.close();
} catch (IOException e) {
}
}
}
}
Expand Down

0 comments on commit c904f6f

Please sign in to comment.