Skip to content

Commit 5e13ff2

Browse files
committed
closes InputStream on parsing
closes #5
1 parent 06699f3 commit 5e13ff2

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

src/main/java/io/zenwave360/jsonrefparser/parser/Parser.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,27 @@ public static void withResourceClassLoader(ClassLoader resourceClassLoader) {
4343

4444
public static ExtendedJsonContext parse(URI uri) throws IOException {
4545
if("classpath".contentEquals(uri.getScheme())) {
46-
return parse(resourceClassLoader.getResourceAsStream(uri.getPath().replaceFirst("^/", "")), uri);
46+
try(var inputStream = resourceClassLoader.getResourceAsStream(uri.getPath().replaceFirst("^/", ""))) {
47+
return parse(inputStream, uri);
48+
}
4749
}
4850
// TODO: it does not support yet parsing http/https files directly
49-
return parse(new FileInputStream(new File(uri)), uri);
51+
try(var inputStream = new FileInputStream(new File(uri))) {
52+
return parse(inputStream, uri);
53+
}
5054
}
5155

5256
public static ExtendedJsonContext parse(File file) throws IOException {
53-
return parse(new FileInputStream(file), file);
57+
try(var inputStream = new FileInputStream(file)) {
58+
return parse(inputStream, file);
59+
}
5460
}
5561

5662
public static ExtendedJsonContext parse(String content) {
5763
try {
58-
return parse(new ByteArrayInputStream(content.getBytes()), "string");
64+
try(var inputStream = new ByteArrayInputStream(content.getBytes())) {
65+
return parse(inputStream, "string");
66+
}
5967
} catch (IOException e) {
6068
throw new RuntimeException(e);
6169
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.zenwave360.jsonrefparser;
2+
3+
4+
import org.junit.Test;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import java.io.File;
9+
import java.net.URI;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.nio.file.StandardCopyOption;
13+
import java.util.Map;
14+
15+
/**
16+
* Test that files are not locked after parsing.
17+
*/
18+
public class RefParserTests {
19+
20+
21+
private static final Logger log = LoggerFactory.getLogger(RefParserTests.class.getName());
22+
23+
@Test
24+
public void testParserWithUri() throws Exception {
25+
Files.copy(Path.of("src/test/resources/indexed-array.yml"), Path.of("target/indexed-array.yml"), StandardCopyOption.REPLACE_EXISTING);
26+
Map<String, Object> parsed = new $RefParser(Path.of("target/indexed-array.yml").toUri()).parse().getRefs().schema();
27+
Files.delete(Path.of("target/indexed-array.yml"));
28+
}
29+
30+
@Test
31+
public void testParserWithFile() throws Exception {
32+
Files.copy(Path.of("src/test/resources/indexed-array.yml"), Path.of("target/indexed-array.yml"), StandardCopyOption.REPLACE_EXISTING);
33+
Map<String, Object> parsed = new $RefParser(new File("target/indexed-array.yml")).parse().getRefs().schema();
34+
Files.delete(Path.of("target/indexed-array.yml"));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)