Skip to content

Commit f23a76d

Browse files
committed
fix(model): Close the parser after reading in readValueOrNull()
Signed-off-by: Sebastian Schuberth <[email protected]>
1 parent a890f2b commit f23a76d

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

model/src/main/kotlin/FileFormat.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ inline fun <reified T : Any> File.readValue(): T =
102102
*/
103103
inline fun <reified T : Any> File.readValueOrNull(): T? {
104104
val mapper = mapper()
105-
val parser = mapper.factory.createParser(this)
106105

107-
val values = mapper.readValues<T>(parser).readAll().also {
108-
if (it.isEmpty()) return null
109-
if (it.size > 1) throw IOException("Multiple top-level objects found in file '$this'.")
106+
val values = mapper.factory.createParser(this).use { parser ->
107+
mapper.readValues<T>(parser).readAll().also {
108+
if (it.isEmpty()) return null
109+
if (it.size > 1) throw IOException("Multiple top-level objects found in file '$this'.")
110+
}
110111
}
111112

112113
return values.first()

0 commit comments

Comments
 (0)