diff --git a/asm/src/main/java/dev/openrs2/asm/classpath/Library.kt b/asm/src/main/java/dev/openrs2/asm/classpath/Library.kt index 6393201e45..61c32bc356 100644 --- a/asm/src/main/java/dev/openrs2/asm/classpath/Library.kt +++ b/asm/src/main/java/dev/openrs2/asm/classpath/Library.kt @@ -13,6 +13,7 @@ import org.objectweb.asm.commons.Remapper import org.objectweb.asm.tree.ClassNode import org.objectweb.asm.util.CheckClassAdapter import java.io.ByteArrayInputStream +import java.io.OutputStream import java.io.SequenceInputStream import java.nio.file.Files import java.nio.file.Path @@ -81,31 +82,32 @@ class Library constructor() : Iterable { fun writeJar(path: Path) { logger.info { "Writing jar $path" } - DeterministicJarOutputStream(Files.newOutputStream(path)).use { out -> + Files.newOutputStream(path).use(::writeJar) + } + + fun writeJar(out: OutputStream) { + DeterministicJarOutputStream(out).use { jar -> for (clazz in classes.values) { val writer = ClassWriter(0) clazz.accept(CheckClassAdapter(writer, true)) - out.putNextEntry(JarEntry(clazz.name + CLASS_SUFFIX)) - out.write(writer.toByteArray()) + jar.putNextEntry(JarEntry(clazz.name + CLASS_SUFFIX)) + jar.write(writer.toByteArray()) } } } - fun writePack(path: Path) { - logger.info { "Writing pack $path" } - + fun writePack(out: OutputStream) { val temp = Files.createTempFile(TEMP_PREFIX, JAR_SUFFIX) try { writeJar(temp) JarInputStream(Files.newInputStream(temp)).use { `in` -> - val data = Files.newOutputStream(path) val headerSize = GZIP_HEADER.size.toLong() - GZIPOutputStream(SkipOutputStream(data, headerSize)).use { out -> - Pack200.newPacker().pack(`in`, out) + GZIPOutputStream(SkipOutputStream(out, headerSize)).use { gzip -> + Pack200.newPacker().pack(`in`, gzip) } } } finally {