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