Skip to content

Commit

Permalink
Make FastServiceLoader compatible with Java 1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
qwwdfsad committed Jun 6, 2019
1 parent e2a5671 commit d15d8d6
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions kotlinx-coroutines-core/jvm/src/internal/FastServiceLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,37 @@ internal object FastServiceLoader {
val pathToJar = path.substringAfter("jar:file:").substringBefore('!')
val entry = path.substringAfter("!/")
// mind the verify = false flag!
(JarFile(pathToJar, false) as Closeable).use { file ->
BufferedReader(InputStreamReader((file as JarFile).getInputStream(ZipEntry(entry)), "UTF-8")).use { r ->
(JarFile(pathToJar, false)).use { file ->
BufferedReader(InputStreamReader(file.getInputStream(ZipEntry(entry)), "UTF-8")).use { r ->
return parseFile(r)
}
}
}
// Regular path for everything elese
// Regular path for everything else
return BufferedReader(InputStreamReader(url.openStream())).use { reader ->
parseFile(reader)
}
}

// JarFile does no implement Closesable on Java 1.6
private inline fun <R> JarFile.use(block: (JarFile) -> R): R {
var cause: Throwable? = null
try {
return block(this)
} catch (e: Throwable) {
cause = e
throw e
} finally {
try {
close()
} catch (closeException: Throwable) {
if (cause === null) throw closeException
cause.addSuppressed(closeException)
throw cause
}
}
}

private fun parseFile(r: BufferedReader): List<String> {
val names = mutableSetOf<String>()
while (true) {
Expand Down

0 comments on commit d15d8d6

Please sign in to comment.