From 02ed6d74551e5f31edc5e49b7dd0ced076c28325 Mon Sep 17 00:00:00 2001 From: Graham Date: Tue, 14 Jan 2020 21:15:07 +0000 Subject: [PATCH] Make writeJar and writePack accept an OutputStream --- .../java/dev/openrs2/asm/classpath/Library.kt | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) 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 6393201e..61c32bc3 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 {