diff --git a/src/main/java/net/minecraftforge/fart/Main.java b/src/main/java/net/minecraftforge/fart/Main.java index f31949c..92c1898 100644 --- a/src/main/java/net/minecraftforge/fart/Main.java +++ b/src/main/java/net/minecraftforge/fart/Main.java @@ -15,6 +15,7 @@ import java.util.Arrays; import java.util.List; import java.util.function.Consumer; +import java.util.stream.Stream; import joptsimple.OptionException; import joptsimple.OptionParser; @@ -167,11 +168,11 @@ private static String[] expandArgs(String[] args) throws IOException { if (x + 1 == args.length) throw new IllegalArgumentException("No value specified for '--cfg'"); - try (var lines = Files.lines(Paths.get(args[++x]))) { + try (Stream lines = Files.lines(Paths.get(args[++x]))) { lines.forEach(ret::add); } } else if (args[x].startsWith("--cfg=")) { - try (var lines = Files.lines(Paths.get(args[x].substring(6)))) { + try (Stream lines = Files.lines(Paths.get(args[x].substring(6)))) { lines.forEach(ret::add); } } else { diff --git a/src/main/java/net/minecraftforge/fart/internal/ClassProviderImpl.java b/src/main/java/net/minecraftforge/fart/internal/ClassProviderImpl.java index bd329e6..2dc9d9f 100644 --- a/src/main/java/net/minecraftforge/fart/internal/ClassProviderImpl.java +++ b/src/main/java/net/minecraftforge/fart/internal/ClassProviderImpl.java @@ -128,7 +128,7 @@ static class ClassInfo implements IClassInfo { this.name = nameToBytecode(node); this.access = new Access(node.getModifiers()); this.superName = nameToBytecode(node.getSuperclass()); - this.interfaces = Arrays.stream(node.getInterfaces()).map(ClassInfo::nameToBytecode).toList(); + this.interfaces = Arrays.stream(node.getInterfaces()).map(ClassInfo::nameToBytecode).collect(Collectors.toList()); Map mtds = Stream.concat( Arrays.stream(node.getConstructors()).map(MethodInfo::new), diff --git a/src/main/java/net/minecraftforge/fart/internal/EnhancedRemapper.java b/src/main/java/net/minecraftforge/fart/internal/EnhancedRemapper.java index 9b07f9d..e77c7ae 100644 --- a/src/main/java/net/minecraftforge/fart/internal/EnhancedRemapper.java +++ b/src/main/java/net/minecraftforge/fart/internal/EnhancedRemapper.java @@ -13,6 +13,7 @@ import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Consumer; +import java.util.stream.Stream; import org.jetbrains.annotations.Nullable; import org.objectweb.asm.Handle; @@ -71,8 +72,8 @@ public Optional mapJavadocMember(final String owner, final String name, } private Optional findMethod(final String owner, final String name, final int paramCount) { - return getClass(owner).stream() - .flatMap(c -> c.getMethods().stream().flatMap(Optional::stream)) + return stream(getClass(owner)) + .flatMap(c -> c.getMethods().stream().flatMap(EnhancedRemapper::stream)) .filter(m -> m.getName().equals(name) && Type.getMethodType(m.getDescriptor()).getArgumentTypes().length == paramCount) .findFirst(); } @@ -141,11 +142,15 @@ private IMappingFile getMap() { private Optional computeClass(String cls) { Optional icls = this.getClassProvider().getClass(cls); IMappingFile.IClass mcls = this.map.getClass(cls); - if (icls.isEmpty() && mcls == null) + if (!icls.isPresent() && mcls == null) return Optional.empty(); return Optional.of(new MClass(icls.orElse(null), mcls)); } + private static Stream stream(Optional optional) { + return optional.isPresent() ? Stream.of(optional.get()) : Stream.empty(); + } + private class MClass { private final IClassInfo icls; private final IMappingFile.IClass mcls; @@ -183,12 +188,12 @@ private class MClass { for (MClass parentCls : parents) { for (Optional fldOpt : parentCls.getFields()) { - if (fldOpt.isEmpty()) + if (!fldOpt.isPresent()) continue; MField fld = fldOpt.get(); Optional existing = this.fields.get(fld.getKey()); - if (existing == null || existing.isEmpty()) { + if (existing == null || !existing.isPresent()) { /* There are some weird cases where a field will be referenced as if it were owned by the current class, * but it needs a field from the parent. So lets follow the linking spec and pull * down fields from parents. @@ -208,7 +213,7 @@ private class MClass { } for (Optional mtdOpt : parentCls.getMethods()) { - if (mtdOpt.isEmpty()) + if (!mtdOpt.isPresent()) continue; MMethod mtd = mtdOpt.get(); @@ -241,7 +246,7 @@ private class MClass { Optional existingOpt = this.methods.get(mtd.getKey()); - if (existingOpt == null || existingOpt.isEmpty()) { + if (existingOpt == null || !existingOpt.isPresent()) { /* If there is none existing, then we pull in what we have found from the parents. * This intentionally uses the same object as the parents so that if we have weird edge * cases, we can migrate the mapping transitively. diff --git a/src/main/java/net/minecraftforge/fart/internal/JavadoctorRemapper.java b/src/main/java/net/minecraftforge/fart/internal/JavadoctorRemapper.java index 653def1..7ac5b95 100644 --- a/src/main/java/net/minecraftforge/fart/internal/JavadoctorRemapper.java +++ b/src/main/java/net/minecraftforge/fart/internal/JavadoctorRemapper.java @@ -113,7 +113,7 @@ private String[] mapParams(String containedClass, String[] params) { } private String replaceLinks(String containedClass, Matcher matcher, Function prefix) { - StringBuilder sb = new StringBuilder(); + StringBuffer sb = new StringBuffer(); while (matcher.find()) { final String matchedOwner = matcher.group("owner"); final String owner = references.getInternalName((matchedOwner == null || matchedOwner.isEmpty()) ? containedClass : matchedOwner); diff --git a/src/main/java/net/minecraftforge/fart/internal/RenamerImpl.java b/src/main/java/net/minecraftforge/fart/internal/RenamerImpl.java index 381d1d0..b9f686d 100644 --- a/src/main/java/net/minecraftforge/fart/internal/RenamerImpl.java +++ b/src/main/java/net/minecraftforge/fart/internal/RenamerImpl.java @@ -6,8 +6,10 @@ package net.minecraftforge.fart.internal; import java.io.BufferedOutputStream; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.nio.file.Files; import java.util.ArrayList; @@ -103,7 +105,7 @@ public void run(File input, File output) { if (e.isDirectory()) continue; String name = e.getName(); - byte[] data = in.getInputStream(e).readAllBytes(); + byte[] data = readAllBytes(in.getInputStream(e), e.getSize()); if (name.endsWith(".class")) oldEntries.add(ClassEntry.create(name, e.getTime(), data)); @@ -214,6 +216,19 @@ else if (name.equals("javadoctor.json")) } } + private byte[] readAllBytes(InputStream in, long size) throws IOException { + // This program w ill crash if size exceeds MAX_INT anyway since arrays are limited to 32-bit indices + ByteArrayOutputStream tmp = new ByteArrayOutputStream(size >= 0 ? (int) size : 0); + + byte[] buffer = new byte[8192]; + int read; + while ((read = in.read(buffer)) != -1) { + tmp.write(buffer, 0, read); + } + + return tmp.toByteArray(); + } + // Tho Directory entries are not strictly necessary, we add them because some bad implementations of Zip extractors // attempt to extract files without making sure the parents exist. private void addDirectory(ZipOutputStream zos, Set seen, String path) throws IOException {