Author: joern
Date: Sat May 21 11:55:40 2016
New Revision: 1744917
URL: http://svn.apache.org/viewvc?rev=1744917&view=rev
Log:
OPENNLP-849 Improve handling of InputStreams
All InputStreams are now using the try-with-resources statement and some
streams are now also closed correctly.
Modified:
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/namefind/TokenNameFinderTrainerTool.java
Modified:
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/namefind/TokenNameFinderTrainerTool.java
URL:
http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/namefind/TokenNameFinderTrainerTool.java?rev=1744917&r1=1744916&r2=1744917&view=diff
==============================================================================
---
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/namefind/TokenNameFinderTrainerTool.java
(original)
+++
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/namefind/TokenNameFinderTrainerTool.java
Sat May 21 11:55:40 2016
@@ -70,19 +70,12 @@ public final class TokenNameFinderTraine
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;
@@ -109,16 +102,14 @@ public final class TokenNameFinderTraine
// 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();
@@ -141,9 +132,7 @@ public final class TokenNameFinderTraine
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
@@ -151,11 +140,6 @@ public final class TokenNameFinderTraine
} catch (IOException e) {
// TODO: Fix exception handling
e.printStackTrace();
- } finally {
- try {
- resourceIn.close();
- } catch (IOException e) {
- }
}
}
}