1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-25 07:57:12 -07:00

Better sequence.

This commit is contained in:
Cedric Beust 2017-03-24 12:26:25 -07:00
parent f788761e4c
commit 43346a256d

View file

@ -117,32 +117,7 @@ class VerifyKobaltZipTest : KobaltTest() {
}
}
private fun toSequence(ins: InputStream) = Sequence<JarEntry> { JarInputStreamIterator(JarInputStream(ins)) }
}
/**
* I don't want to hold the whole content of the jar file in memory to run tests on its content,
* so this iterator allows me to create a sequence out of it, so each entry in the jar file can
* be verified lazily
*/
class JarInputStreamIterator(val ins: JarInputStream) : Iterator<JarEntry> {
var next: JarEntry? = null
override fun next(): JarEntry {
if (next != null) {
next?.let {
val result = it
next = null
return result
}
} else {
return ins.nextJarEntry
}
throw IllegalArgumentException("Should never happen")
}
override fun hasNext(): Boolean {
if (next == null) next = ins.nextJarEntry
return next != null
}
fun JarInputStream.asSequence() = generateSequence { nextJarEntry }
private fun toSequence(ins: InputStream): Sequence<JarEntry> = JarInputStream(ins).asSequence()
}