From c633725c556fa59a3204b7bff4bfcdfabc61e4d1 Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 23 Dec 2019 21:02:24 +0000 Subject: [PATCH] Convert DecompilerIo to Kotlin --- .../dev/openrs2/decompiler/Decompiler.java | 4 +- .../dev/openrs2/decompiler/DecompilerIo.java | 97 ------------------- .../dev/openrs2/decompiler/DecompilerIo.kt | 85 ++++++++++++++++ 3 files changed, 87 insertions(+), 99 deletions(-) delete mode 100644 decompiler/src/main/java/dev/openrs2/decompiler/DecompilerIo.java create mode 100644 decompiler/src/main/java/dev/openrs2/decompiler/DecompilerIo.kt diff --git a/decompiler/src/main/java/dev/openrs2/decompiler/Decompiler.java b/decompiler/src/main/java/dev/openrs2/decompiler/Decompiler.java index eb1efc3f13..e7a28028ce 100644 --- a/decompiler/src/main/java/dev/openrs2/decompiler/Decompiler.java +++ b/decompiler/src/main/java/dev/openrs2/decompiler/Decompiler.java @@ -4,10 +4,10 @@ import java.io.Closeable; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.function.Function; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import kotlin.jvm.functions.Function1; import org.jetbrains.java.decompiler.main.Fernflower; import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences; @@ -55,7 +55,7 @@ public final class Decompiler implements Closeable { private final Fernflower fernflower; private final ImmutableList sources; - public Decompiler(ImmutableList sources, Function destination) { + public Decompiler(ImmutableList sources, Function1 destination) { this.io = new DecompilerIo(destination); this.fernflower = new Fernflower(io, io, OPTIONS, Slf4jFernflowerLogger.INSTANCE); this.sources = sources; diff --git a/decompiler/src/main/java/dev/openrs2/decompiler/DecompilerIo.java b/decompiler/src/main/java/dev/openrs2/decompiler/DecompilerIo.java deleted file mode 100644 index 69f95b67e8..0000000000 --- a/decompiler/src/main/java/dev/openrs2/decompiler/DecompilerIo.java +++ /dev/null @@ -1,97 +0,0 @@ -package dev.openrs2.decompiler; - -import java.io.Closeable; -import java.io.IOException; -import java.io.UncheckedIOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.HashMap; -import java.util.Map; -import java.util.function.Function; -import java.util.jar.JarFile; -import java.util.jar.Manifest; - -import com.google.common.io.ByteStreams; -import org.jetbrains.java.decompiler.main.extern.IBytecodeProvider; -import org.jetbrains.java.decompiler.main.extern.IResultSaver; - -public final class DecompilerIo implements IBytecodeProvider, IResultSaver, Closeable { - private final Map inputJars = new HashMap<>(); - private final Function destination; - - public DecompilerIo(Function destination) { - this.destination = destination; - } - - @Override - public byte[] getBytecode(String externalPath, String internalPath) throws IOException { - if (internalPath == null) { - throw new UnsupportedOperationException(); - } - - var jar = inputJars.get(externalPath); - if (jar == null) { - jar = new JarFile(externalPath); - inputJars.put(externalPath, jar); - } - - try (var in = jar.getInputStream(jar.getJarEntry(internalPath))) { - return ByteStreams.toByteArray(in); - } - } - - @Override - public void saveFolder(String path) { - /* ignore */ - } - - @Override - public void copyFile(String source, String path, String entryName) { - throw new UnsupportedOperationException(); - } - - @Override - public void saveClassFile(String path, String qualifiedName, String entryName, String content, int[] mapping) { - throw new UnsupportedOperationException(); - } - - @Override - public void createArchive(String path, String archiveName, Manifest manifest) { - /* ignore */ - } - - @Override - public void saveDirEntry(String path, String archiveName, String entryName) { - /* ignore */ - } - - @Override - public void copyEntry(String source, String path, String archiveName, String entry) { - /* ignore */ - } - - @Override - public void saveClassEntry(String path, String archiveName, String qualifiedName, String entryName, String content) { - var p = destination.apply(archiveName).resolve(entryName); - try { - Files.createDirectories(p.getParent()); - try (var writer = Files.newBufferedWriter(p)) { - writer.write(content); - } - } catch (IOException ex) { - throw new UncheckedIOException(ex); - } - } - - @Override - public void closeArchive(String path, String archiveName) { - /* ignore */ - } - - @Override - public void close() throws IOException { - for (var jar : inputJars.values()) { - jar.close(); - } - } -} diff --git a/decompiler/src/main/java/dev/openrs2/decompiler/DecompilerIo.kt b/decompiler/src/main/java/dev/openrs2/decompiler/DecompilerIo.kt new file mode 100644 index 0000000000..d0c6c350f3 --- /dev/null +++ b/decompiler/src/main/java/dev/openrs2/decompiler/DecompilerIo.kt @@ -0,0 +1,85 @@ +package dev.openrs2.decompiler + +import com.google.common.io.ByteStreams +import org.jetbrains.java.decompiler.main.extern.IBytecodeProvider +import org.jetbrains.java.decompiler.main.extern.IResultSaver +import java.io.Closeable +import java.io.IOException +import java.nio.file.Files +import java.nio.file.Path +import java.util.jar.JarFile +import java.util.jar.Manifest + +class DecompilerIo(private val destination: (String) -> Path) : IBytecodeProvider, IResultSaver, Closeable { + private val inputJars = mutableMapOf() + + @Throws(IOException::class) + override fun getBytecode(externalPath: String, internalPath: String?): ByteArray { + if (internalPath == null) { + throw UnsupportedOperationException() + } + + val jar = inputJars.computeIfAbsent(externalPath) { + JarFile(it) + } + + jar.getInputStream(jar.getJarEntry(internalPath)).use { + return ByteStreams.toByteArray(it) + } + } + + override fun saveFolder(path: String) { + // ignore + } + + override fun copyFile(source: String, path: String, entryName: String) { + throw UnsupportedOperationException() + } + + override fun saveClassFile( + path: String, + qualifiedName: String, + entryName: String, + content: String, + mapping: IntArray + ) { + throw UnsupportedOperationException() + } + + override fun createArchive(path: String, archiveName: String, manifest: Manifest?) { + // ignore + } + + override fun saveDirEntry(path: String, archiveName: String, entryName: String) { + // ignore + } + + override fun copyEntry(source: String, path: String, archiveName: String, entry: String) { + // ignore + } + + override fun saveClassEntry( + path: String, + archiveName: String, + qualifiedName: String, + entryName: String, + content: String + ) { + val p = destination(archiveName).resolve(entryName) + Files.createDirectories(p.parent) + Files.newBufferedWriter(p).use { + it.write(content) + } + } + + override fun closeArchive(path: String, archiveName: String) { + // ignore + } + + @Throws(IOException::class) + override fun close() { + for (jar in inputJars.values) { + jar.close() + } + } +}