Close InputStream after usage in KeyReader

The `InputStream` would not be closed after reading the key file in `KeyReader#import`, it's now wrapped with `use{ }` which handles closing the stream after usage.
This commit is contained in:
PixelyIon 2022-03-10 12:38:36 +05:30
parent 647cb07dc8
commit 1d070e6332

View File

@ -47,40 +47,41 @@ object KeyReader {
val tmpOutputFile = File("${context.filesDir.canonicalFile}/${keyType.fileName}.tmp")
val inputStream = context.contentResolver.openInputStream(uri)
tmpOutputFile.bufferedWriter().use { writer ->
val valid = inputStream!!.bufferedReader().useLines {
for (line in it) {
if (line.startsWith(";") || line.isBlank()) continue
context.contentResolver.openInputStream(uri).use { inputStream ->
tmpOutputFile.bufferedWriter().use { writer ->
val valid = inputStream!!.bufferedReader().useLines {
for (line in it) {
if (line.startsWith(";") || line.isBlank()) continue
val pair = line.split("=")
if (pair.size != 2)
return@useLines false
val pair = line.split("=")
if (pair.size != 2)
return@useLines false
val key = pair[0].trim()
val value = pair[1].trim()
when (keyType) {
KeyType.Title -> {
if (key.length != 32 && !isHexString(key))
return@useLines false
if (value.length != 32 && !isHexString(value))
return@useLines false
}
KeyType.Prod -> {
if (!key.contains("_"))
return@useLines false
if (!isHexString(value))
return@useLines false
val key = pair[0].trim()
val value = pair[1].trim()
when (keyType) {
KeyType.Title -> {
if (key.length != 32 && !isHexString(key))
return@useLines false
if (value.length != 32 && !isHexString(value))
return@useLines false
}
KeyType.Prod -> {
if (!key.contains("_"))
return@useLines false
if (!isHexString(value))
return@useLines false
}
}
writer.append("$key=$value\n")
}
writer.append("$key=$value\n")
true
}
true
}
if (valid) tmpOutputFile.renameTo(File("${tmpOutputFile.parent}/${keyType.fileName}"))
return valid
if (valid) tmpOutputFile.renameTo(File("${tmpOutputFile.parent}/${keyType.fileName}"))
return valid
}
}
}