Synchronized JarUtil ClassWriters

This should be re-evaluated, it's most likely not needed and it will slow down multi-pane decompilation

An alternative solution is generating the ClassNode List once, then storing it in a cache that can be quickly saved to disk. If a new file gets added regenerate the cache.
This commit is contained in:
Konloch 2021-07-04 01:56:25 -07:00
parent 854c4d622c
commit c0eb9e944e

View file

@ -17,7 +17,6 @@ import java.util.zip.ZipInputStream;
import me.konloch.kontainer.io.DiskWriter; import me.konloch.kontainer.io.DiskWriter;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile; import org.apache.commons.compress.archivers.zip.ZipFile;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter; import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.ClassNode;
import the.bytecode.club.bytecodeviewer.BytecodeViewer; import the.bytecode.club.bytecodeviewer.BytecodeViewer;
@ -51,8 +50,10 @@ import static the.bytecode.club.bytecodeviewer.Constants.*;
* @since 09/26/2011 * @since 09/26/2011
*/ */
public class JarUtils { public class JarUtils
{
public static Object LOCK = new Object();
/** /**
* Loads the classes and resources from the input jar file * Loads the classes and resources from the input jar file
* *
@ -245,7 +246,10 @@ public class JarUtils {
* @return the ClassNode instance * @return the ClassNode instance
*/ */
public static ClassNode getNode(final byte[] bytez) { public static ClassNode getNode(final byte[] bytez) {
return ASMUtil.getClassNode(bytez); synchronized (LOCK)
{
return ASMUtil.getClassNode(bytez);
}
} }
/** /**
@ -296,27 +300,35 @@ public class JarUtils {
* @param path the exact jar output path * @param path the exact jar output path
*/ */
public static void saveAsJarClassesOnly(ArrayList<ClassNode> nodeList, String path) { public static void saveAsJarClassesOnly(ArrayList<ClassNode> nodeList, String path) {
try { synchronized (LOCK)
JarOutputStream out = new JarOutputStream(new FileOutputStream(path)); {
ArrayList<String> noDupe = new ArrayList<>(); try
for (ClassNode cn : nodeList) { {
ClassWriter cw = new ClassWriter(0); JarOutputStream out = new JarOutputStream(new FileOutputStream(path));
cn.accept(cw); ArrayList<String> noDupe = new ArrayList<>();
for (ClassNode cn : nodeList)
String name = cn.name + ".class"; {
ClassWriter cw = new ClassWriter(0);
if (!noDupe.contains(name)) { cn.accept(cw);
noDupe.add(name);
out.putNextEntry(new ZipEntry(name)); String name = cn.name + ".class";
out.write(cw.toByteArray());
out.closeEntry(); if (!noDupe.contains(name))
{
noDupe.add(name);
out.putNextEntry(new ZipEntry(name));
out.write(cw.toByteArray());
out.closeEntry();
}
} }
noDupe.clear();
out.close();
}
catch (IOException e)
{
new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e);
} }
noDupe.clear();
out.close();
} catch (IOException e) {
new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e);
} }
} }