Convert DecompilerIo to Kotlin

master
Graham 5 years ago
parent aab286cf13
commit c633725c55
  1. 4
      decompiler/src/main/java/dev/openrs2/decompiler/Decompiler.java
  2. 97
      decompiler/src/main/java/dev/openrs2/decompiler/DecompilerIo.java
  3. 85
      decompiler/src/main/java/dev/openrs2/decompiler/DecompilerIo.kt

@ -4,10 +4,10 @@ import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.function.Function;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import kotlin.jvm.functions.Function1;
import org.jetbrains.java.decompiler.main.Fernflower; import org.jetbrains.java.decompiler.main.Fernflower;
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences; import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;
@ -55,7 +55,7 @@ public final class Decompiler implements Closeable {
private final Fernflower fernflower; private final Fernflower fernflower;
private final ImmutableList<Path> sources; private final ImmutableList<Path> sources;
public Decompiler(ImmutableList<Path> sources, Function<String, Path> destination) { public Decompiler(ImmutableList<Path> sources, Function1<String, Path> destination) {
this.io = new DecompilerIo(destination); this.io = new DecompilerIo(destination);
this.fernflower = new Fernflower(io, io, OPTIONS, Slf4jFernflowerLogger.INSTANCE); this.fernflower = new Fernflower(io, io, OPTIONS, Slf4jFernflowerLogger.INSTANCE);
this.sources = sources; this.sources = sources;

@ -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<String, JarFile> inputJars = new HashMap<>();
private final Function<String, Path> destination;
public DecompilerIo(Function<String, Path> 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();
}
}
}

@ -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<String, JarFile>()
@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()
}
}
}
Loading…
Cancel
Save