Skip to content

Commit

Permalink
Use '.use { }' to close resources
Browse files Browse the repository at this point in the history
  • Loading branch information
krzema12 committed Jan 15, 2025
1 parent ffb5375 commit e702b2b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,35 @@ fun getFileByName(name: String): File =

fun canonicalParse(input2: InputStream, label: String): List<Event> {
val settings = LoadSettings.builder().setLabel(label).build()
val reader = StreamReader(settings, YamlUnicodeReader(input2.source()))
val buffer = StringBuffer()
while (reader.peek() != 0) {
buffer.appendCodePoint(reader.peek())
reader.forward()
}
val parser = CanonicalParser(buffer.toString().replace(System.lineSeparator(), "\n"), label)
val result = buildList {
while (parser.hasNext()) {
add(parser.next())
input2.use {
val reader = StreamReader(settings, YamlUnicodeReader(input2.source()))
val buffer = StringBuffer()
while (reader.peek() != 0) {
buffer.appendCodePoint(reader.peek())
reader.forward()
}
val parser = CanonicalParser(buffer.toString().replace(System.lineSeparator(), "\n"), label)
val result = buildList {
while (parser.hasNext()) {
add(parser.next())
}
}
return result
}
input2.close()
return result
}

fun parse(input: InputStream): List<Event> {
val settings = LoadSettings.builder().build()
val reader = StreamReader(settings, YamlUnicodeReader(input.source()))
val parser = ParserImpl(settings, reader)
val result = buildList {
while (parser.hasNext()) {
add(parser.next())
input.use {
val reader = StreamReader(settings, YamlUnicodeReader(input.source()))
val parser = ParserImpl(settings, reader)
val result = buildList {
while (parser.hasNext()) {
add(parser.next())
}
}
return result
}
input.close()
return result
}

private class InheritedFilenameFilter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@ class InheritedReaderTest: FunSpec({
// KMP basically only supports UTF-8.
.filter { it.name !in setOf("odd-utf16.stream-error", "invalid-utf8-byte.stream-error") }
.forEach { file ->
val input = FileInputStream(file)
val unicodeReader = YamlUnicodeReader(input.source())
val stream = StreamReader(LoadSettings.builder().build(), unicodeReader)
try {
while (stream.peek() != 0) {
stream.forward()
FileInputStream(file).use { input ->
val unicodeReader = YamlUnicodeReader(input.source())
val stream = StreamReader(LoadSettings.builder().build(), unicodeReader)
try {
while (stream.peek() != 0) {
stream.forward()
}
fail("Invalid stream must not be accepted: ${file.absolutePath}; encoding=${unicodeReader.encoding}")
} catch (e: ReaderException) {
e.toString() shouldContain " special characters are not allowed"
} catch (e: YamlEngineException) {
e.toString() shouldContain " MalformedInputException"
}
fail("Invalid stream must not be accepted: ${file.absolutePath}; encoding=${unicodeReader.encoding}")
} catch (e: ReaderException) {
e.toString() shouldContain " special characters are not allowed"
} catch (e: YamlEngineException) {
e.toString() shouldContain " MalformedInputException"
} finally {
input.close()
}
}
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,31 @@ class InheritedTokensTest: FunSpec({

val tokens1 = mutableListOf<String>()
val settings = LoadSettings.builder().build()
val reader = StreamReader(
loadSettings = settings,
stream = YamlUnicodeReader(FileInputStream(getFileByName(dataName)).source()),
)
val scanner = ScannerImpl(settings, reader)
try {
while (scanner.checkToken()) {
val token = scanner.next()
if (!(token is StreamStartToken || token is StreamEndToken)) {
val replacement = replaces[token.tokenId]!!
tokens1.add(replacement)
FileInputStream(getFileByName(dataName)).source().use { source ->
val reader = StreamReader(
loadSettings = settings,
stream = YamlUnicodeReader(source),
)
val scanner = ScannerImpl(settings, reader)
try {
while (scanner.checkToken()) {
val token = scanner.next()
if (!(token is StreamStartToken || token is StreamEndToken)) {
val replacement = replaces[token.tokenId]!!
tokens1.add(replacement)
}
}
tokens1 shouldBe tokens2
} catch (_: RuntimeException) {
println("File name: \n${tokenFile.name}")
val data = getResource(tokenFile.name)
println("Data: \n$data")
println("Tokens:")
tokens1.forEach {
println("token")
}
fail("Cannot scan: $tokenFile")
}
tokens1 shouldBe tokens2
} catch (_: RuntimeException) {
println("File name: \n${tokenFile.name}")
val data = getResource(tokenFile.name)
println("Data: \n$data")
println("Tokens:")
tokens1.forEach {
println("token")
}
fail("Cannot scan: $tokenFile")
}
}
}
Expand All @@ -85,26 +87,25 @@ class InheritedTokensTest: FunSpec({
files.size shouldBeGreaterThan 0
files.forEach { file ->
val tokens = mutableListOf<String>()
val input = FileInputStream(file)
val settings = LoadSettings.builder().build()
val reader = StreamReader(settings, YamlUnicodeReader(input.source()))
val scanner = ScannerImpl(settings, reader)
try {
while (scanner.checkToken()) {
val token = scanner.next()
tokens.add(token.javaClass.name)
}
} catch (e: RuntimeException) {
println("File name: \n${file.name}")
val data = getResource(file.name)
println("Data: \n$data")
println("Tokens:")
tokens.forEach {
println("token")
FileInputStream(file).source().use { source ->
val settings = LoadSettings.builder().build()
val reader = StreamReader(settings, YamlUnicodeReader(source))
val scanner = ScannerImpl(settings, reader)
try {
while (scanner.checkToken()) {
val token = scanner.next()
tokens.add(token.javaClass.name)
}
} catch (e: RuntimeException) {
println("File name: \n${file.name}")
val data = getResource(file.name)
println("Data: \n$data")
println("Tokens:")
tokens.forEach {
println("token")
}
fail("Cannot scan: $file; ${e.message}")
}
fail("Cannot scan: $file; ${e.message}")
} finally {
input.close()
}
}
}
Expand Down

0 comments on commit e702b2b

Please sign in to comment.