Skip to content

Commit f3b6a15

Browse files
committed
fix the code for inputstream close to move to finally block
1 parent e76213a commit f3b6a15

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/main/java/com/mservicetech/openapi/validation/OpenApiValidator.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,28 @@ public class OpenApiValidator {
5252
* The default construct will try to load openapi.yml file from classpath
5353
*/
5454
public OpenApiValidator() {
55+
InputStream in = this.getClass().getClassLoader().getResourceAsStream(OPENAPI_YAML_CONFIG);;
5556
try {
56-
InputStream in = this.getClass().getClassLoader().getResourceAsStream(OPENAPI_YAML_CONFIG);
5757
if (in == null) {
5858
in = this.getClass().getClassLoader().getResourceAsStream(OPENAPI_YML_CONFIG);
5959
if (in==null) {
60-
throw new IOException("cannot load openapi spec file");
60+
throw new IOException("Cannot load openapi spec file");
6161
}
6262
}
6363
spec = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
6464
openApiHelper = new OpenApiHelper(spec);
6565
schemaValidator = new SchemaValidator(openApiHelper.openApi3);
66-
in.close();
67-
} catch (Exception e) {
68-
logger.error("initial failed:" + e);
66+
} catch (IOException e) {
67+
logger.error("Initial failed:" + e);
68+
}
69+
finally {
70+
try {
71+
if( in!=null ) {
72+
in.close();
73+
}
74+
} catch(IOException e) {
75+
logger.error(" Failed to close input stream:" + e);
76+
}
6977
}
7078
}
7179

0 commit comments

Comments
 (0)