Add Sequence<JarEntry> extension property to JarInputStream

This improves the readability of code that iterates through all entries
in a jar.

Signed-off-by: Graham <gpe@openrs2.dev>
bzip2
Graham 5 years ago
parent 6e877b52ce
commit 10145fdb43
  1. 4
      asm/src/main/java/dev/openrs2/asm/io/JarLibraryReader.kt
  2. 5
      asm/src/main/java/dev/openrs2/asm/io/SignedJarLibraryWriter.kt
  3. 3
      util/src/main/java/dev/openrs2/util/io/DeterministicJarOutputStream.kt
  4. 6
      util/src/main/java/dev/openrs2/util/io/JarInputStreamExtensions.kt

@ -2,6 +2,7 @@ package dev.openrs2.asm.io
import dev.openrs2.asm.classpath.JsrInliner import dev.openrs2.asm.classpath.JsrInliner
import dev.openrs2.asm.classpath.Library import dev.openrs2.asm.classpath.Library
import dev.openrs2.util.io.entries
import org.objectweb.asm.ClassReader import org.objectweb.asm.ClassReader
import org.objectweb.asm.tree.ClassNode import org.objectweb.asm.tree.ClassNode
import java.util.jar.JarInputStream import java.util.jar.JarInputStream
@ -10,8 +11,7 @@ class JarLibraryReader(private val input: JarInputStream) : LibraryReader {
override fun read(): Library { override fun read(): Library {
val library = Library() val library = Library()
while (true) { for (entry in input.entries) {
val entry = input.nextJarEntry ?: break
if (!entry.name.endsWith(CLASS_SUFFIX)) { if (!entry.name.endsWith(CLASS_SUFFIX)) {
continue continue
} }

@ -4,6 +4,7 @@ import dev.openrs2.asm.classpath.ClassPath
import dev.openrs2.asm.classpath.Library import dev.openrs2.asm.classpath.Library
import dev.openrs2.crypto.Pkcs12KeyStore import dev.openrs2.crypto.Pkcs12KeyStore
import dev.openrs2.util.io.DeterministicJarOutputStream import dev.openrs2.util.io.DeterministicJarOutputStream
import dev.openrs2.util.io.entries
import java.io.OutputStream import java.io.OutputStream
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.Path import java.nio.file.Path
@ -37,8 +38,8 @@ class SignedJarLibraryWriter(
private fun repack(signedJar: Path) { private fun repack(signedJar: Path) {
JarInputStream(Files.newInputStream(signedJar)).use { input -> JarInputStream(Files.newInputStream(signedJar)).use { input ->
DeterministicJarOutputStream(output, input.manifest).use { output -> DeterministicJarOutputStream(output, input.manifest).use { output ->
generateSequence { input.nextJarEntry }.forEach { for (entry in input.entries) {
output.putNextEntry(it) output.putNextEntry(entry)
input.copyTo(output) input.copyTo(output)
} }
} }

@ -34,8 +34,7 @@ class DeterministicJarOutputStream : JarOutputStream {
fun repack(src: Path, dest: Path) { fun repack(src: Path, dest: Path) {
JarInputStream(Files.newInputStream(src)).use { input -> JarInputStream(Files.newInputStream(src)).use { input ->
create(Files.newOutputStream(dest), input.manifest).use { output -> create(Files.newOutputStream(dest), input.manifest).use { output ->
while (true) { for (entry in input.entries) {
val entry = input.nextJarEntry ?: break
output.putNextEntry(entry) output.putNextEntry(entry)
input.copyTo(output) input.copyTo(output)
} }

@ -0,0 +1,6 @@
package dev.openrs2.util.io
import java.util.jar.JarInputStream
val JarInputStream.entries
get() = generateSequence { nextJarEntry }
Loading…
Cancel
Save