Make writeJar and writePack accept an OutputStream

bzip2
Graham 5 years ago
parent c65087ad35
commit 02ed6d7455
  1. 20
      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.tree.ClassNode
import org.objectweb.asm.util.CheckClassAdapter import org.objectweb.asm.util.CheckClassAdapter
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
import java.io.OutputStream
import java.io.SequenceInputStream import java.io.SequenceInputStream
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.Path import java.nio.file.Path
@ -81,31 +82,32 @@ class Library constructor() : Iterable<ClassNode> {
fun writeJar(path: Path) { fun writeJar(path: Path) {
logger.info { "Writing jar $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) { for (clazz in classes.values) {
val writer = ClassWriter(0) val writer = ClassWriter(0)
clazz.accept(CheckClassAdapter(writer, true)) clazz.accept(CheckClassAdapter(writer, true))
out.putNextEntry(JarEntry(clazz.name + CLASS_SUFFIX)) jar.putNextEntry(JarEntry(clazz.name + CLASS_SUFFIX))
out.write(writer.toByteArray()) jar.write(writer.toByteArray())
} }
} }
} }
fun writePack(path: Path) { fun writePack(out: OutputStream) {
logger.info { "Writing pack $path" }
val temp = Files.createTempFile(TEMP_PREFIX, JAR_SUFFIX) val temp = Files.createTempFile(TEMP_PREFIX, JAR_SUFFIX)
try { try {
writeJar(temp) writeJar(temp)
JarInputStream(Files.newInputStream(temp)).use { `in` -> JarInputStream(Files.newInputStream(temp)).use { `in` ->
val data = Files.newOutputStream(path)
val headerSize = GZIP_HEADER.size.toLong() val headerSize = GZIP_HEADER.size.toLong()
GZIPOutputStream(SkipOutputStream(data, headerSize)).use { out -> GZIPOutputStream(SkipOutputStream(out, headerSize)).use { gzip ->
Pack200.newPacker().pack(`in`, out) Pack200.newPacker().pack(`in`, gzip)
} }
} }
} finally { } finally {

Loading…
Cancel
Save