From 10145fdb43bf5850e642cef8a847bfc0d6df3065 Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 2 May 2020 17:43:23 +0100 Subject: [PATCH] Add Sequence extension property to JarInputStream This improves the readability of code that iterates through all entries in a jar. Signed-off-by: Graham --- asm/src/main/java/dev/openrs2/asm/io/JarLibraryReader.kt | 4 ++-- .../main/java/dev/openrs2/asm/io/SignedJarLibraryWriter.kt | 5 +++-- .../dev/openrs2/util/io/DeterministicJarOutputStream.kt | 3 +-- .../java/dev/openrs2/util/io/JarInputStreamExtensions.kt | 6 ++++++ 4 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 util/src/main/java/dev/openrs2/util/io/JarInputStreamExtensions.kt diff --git a/asm/src/main/java/dev/openrs2/asm/io/JarLibraryReader.kt b/asm/src/main/java/dev/openrs2/asm/io/JarLibraryReader.kt index e9924f2d07..187e53d9b8 100644 --- a/asm/src/main/java/dev/openrs2/asm/io/JarLibraryReader.kt +++ b/asm/src/main/java/dev/openrs2/asm/io/JarLibraryReader.kt @@ -2,6 +2,7 @@ package dev.openrs2.asm.io import dev.openrs2.asm.classpath.JsrInliner import dev.openrs2.asm.classpath.Library +import dev.openrs2.util.io.entries import org.objectweb.asm.ClassReader import org.objectweb.asm.tree.ClassNode import java.util.jar.JarInputStream @@ -10,8 +11,7 @@ class JarLibraryReader(private val input: JarInputStream) : LibraryReader { override fun read(): Library { val library = Library() - while (true) { - val entry = input.nextJarEntry ?: break + for (entry in input.entries) { if (!entry.name.endsWith(CLASS_SUFFIX)) { continue } diff --git a/asm/src/main/java/dev/openrs2/asm/io/SignedJarLibraryWriter.kt b/asm/src/main/java/dev/openrs2/asm/io/SignedJarLibraryWriter.kt index 12a8184279..b6ed65072e 100644 --- a/asm/src/main/java/dev/openrs2/asm/io/SignedJarLibraryWriter.kt +++ b/asm/src/main/java/dev/openrs2/asm/io/SignedJarLibraryWriter.kt @@ -4,6 +4,7 @@ import dev.openrs2.asm.classpath.ClassPath import dev.openrs2.asm.classpath.Library import dev.openrs2.crypto.Pkcs12KeyStore import dev.openrs2.util.io.DeterministicJarOutputStream +import dev.openrs2.util.io.entries import java.io.OutputStream import java.nio.file.Files import java.nio.file.Path @@ -37,8 +38,8 @@ class SignedJarLibraryWriter( private fun repack(signedJar: Path) { JarInputStream(Files.newInputStream(signedJar)).use { input -> DeterministicJarOutputStream(output, input.manifest).use { output -> - generateSequence { input.nextJarEntry }.forEach { - output.putNextEntry(it) + for (entry in input.entries) { + output.putNextEntry(entry) input.copyTo(output) } } diff --git a/util/src/main/java/dev/openrs2/util/io/DeterministicJarOutputStream.kt b/util/src/main/java/dev/openrs2/util/io/DeterministicJarOutputStream.kt index 76820ba2a9..cbe2a70498 100644 --- a/util/src/main/java/dev/openrs2/util/io/DeterministicJarOutputStream.kt +++ b/util/src/main/java/dev/openrs2/util/io/DeterministicJarOutputStream.kt @@ -34,8 +34,7 @@ class DeterministicJarOutputStream : JarOutputStream { fun repack(src: Path, dest: Path) { JarInputStream(Files.newInputStream(src)).use { input -> create(Files.newOutputStream(dest), input.manifest).use { output -> - while (true) { - val entry = input.nextJarEntry ?: break + for (entry in input.entries) { output.putNextEntry(entry) input.copyTo(output) } diff --git a/util/src/main/java/dev/openrs2/util/io/JarInputStreamExtensions.kt b/util/src/main/java/dev/openrs2/util/io/JarInputStreamExtensions.kt new file mode 100644 index 0000000000..afea6f9069 --- /dev/null +++ b/util/src/main/java/dev/openrs2/util/io/JarInputStreamExtensions.kt @@ -0,0 +1,6 @@ +package dev.openrs2.util.io + +import java.util.jar.JarInputStream + +val JarInputStream.entries + get() = generateSequence { nextJarEntry }