Skip to content

Commit

Permalink
Ensure all opened input streams are closed
Browse files Browse the repository at this point in the history
  • Loading branch information
ansman authored and ting-yuan committed Oct 10, 2024
1 parent cdc89da commit 5b6d886
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream
import kotlinx.serialization.json.encodeToStream
import java.io.BufferedInputStream
import java.io.File

private object FileSerializer : KSerializer<File> {
Expand Down Expand Up @@ -53,7 +54,7 @@ abstract class PersistentMap<K, V>(
@OptIn(ExperimentalSerializationApi::class)
protected fun <K, V> deserialize(serializer: KSerializer<Map<K, V>>, storage: File): MutableMap<K, V> {
return if (storage.exists()) {
storage.inputStream().use {
BufferedInputStream(storage.inputStream()).use {
Json.decodeFromStream(serializer, it).toMutableMap()
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream
import kotlinx.serialization.json.encodeToStream
import java.io.BufferedInputStream
import java.io.File

private object FileSerializer : KSerializer<File> {
Expand Down Expand Up @@ -53,7 +54,9 @@ abstract class PersistentMap<K, V>(
@OptIn(ExperimentalSerializationApi::class)
protected fun <K, V> deserialize(serializer: KSerializer<Map<K, V>>, storage: File): MutableMap<K, V> {
return if (storage.exists()) {
Json.decodeFromStream(serializer, storage.inputStream()).toMutableMap()
BufferedInputStream(storage.inputStream()).use {
Json.decodeFromStream(serializer, it).toMutableMap()
}
} else {
mutableMapOf()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ class KSPLoader {
processorProvider: List<SymbolProcessorProvider>,
logLevel: Int
): Int {
val objectInputStream = ObjectInputStream(ByteArrayInputStream(kspConfigStream))
val kspConfig = objectInputStream.readObject() as KSPConfig
val kspConfig = ObjectInputStream(ByteArrayInputStream(kspConfigStream)).use { objectInputStream ->
objectInputStream.readObject() as KSPConfig
}
val ksp = KotlinSymbolProcessing(kspConfig, processorProvider, KspGradleLogger(logLevel))
return ksp.execute().ordinal
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,10 @@ public static String getVersion() {
return VERSION.equals("@snapshot@") ? null : VERSION;
}

@SuppressWarnings({"TryFinallyCanBeTryWithResources", "ConstantConditions"})
@SuppressWarnings("ConstantConditions")
private static String loadKotlinCompilerVersion() throws IOException {
BufferedReader versionReader = new BufferedReader(
new InputStreamReader(KotlinCompilerVersion.class.getResourceAsStream(VERSION_FILE_PATH)));
try {
try (BufferedReader versionReader = new BufferedReader(new InputStreamReader(KotlinCompilerVersion.class.getResourceAsStream(VERSION_FILE_PATH)))) {
return versionReader.readLine();
} finally {
versionReader.close();
}
}

Expand Down

0 comments on commit 5b6d886

Please sign in to comment.