From d980311d214c3e1e0234eb011d5ced6701ce3a5a Mon Sep 17 00:00:00 2001 From: Thiakil Date: Tue, 10 Jul 2018 17:17:55 +0800 Subject: Allow reader/writer subclasses to provide the underlying file operations --- .../enigma/mapping/MappingsEnigmaReader.java | 26 +++++++++++++++------- .../enigma/mapping/MappingsEnigmaWriter.java | 4 ++-- .../enigma/throwables/MappingParseException.java | 7 ++++++ 3 files changed, 27 insertions(+), 10 deletions(-) (limited to 'src/main/java/cuchaz') diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java index a53793d3..ddbee76f 100644 --- a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java +++ b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java @@ -5,8 +5,14 @@ import com.google.common.collect.Queues; import cuchaz.enigma.throwables.MappingConflict; import cuchaz.enigma.throwables.MappingParseException; -import java.io.*; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.util.Deque; +import java.util.function.Supplier; public class MappingsEnigmaReader { @@ -39,7 +45,11 @@ public class MappingsEnigmaReader { } public Mappings readFile(Mappings mappings, File file) throws IOException, MappingParseException { - try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charsets.UTF_8))) { + return readFileStream(mappings, new FileInputStream(file), file::getAbsolutePath); + } + + public Mappings readFileStream(Mappings mappings, InputStream stream, Supplier filenameSupplier) throws IOException, MappingParseException { + try (BufferedReader in = new BufferedReader(new InputStreamReader(stream, Charsets.UTF_8))) { Deque mappingStack = Queues.newArrayDeque(); int lineNumber = 0; @@ -87,7 +97,7 @@ public class MappingsEnigmaReader { // inner class if (!(mappingStack.peek() instanceof ClassMapping)) { - throw new MappingParseException(file, lineNumber, "Unexpected CLASS entry here!"); + throw new MappingParseException(filenameSupplier, lineNumber, "Unexpected CLASS entry here!"); } classMapping = readClass(parts, true); @@ -96,26 +106,26 @@ public class MappingsEnigmaReader { mappingStack.push(classMapping); } else if (token.equalsIgnoreCase("FIELD")) { if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof ClassMapping)) { - throw new MappingParseException(file, lineNumber, "Unexpected FIELD entry here!"); + throw new MappingParseException(filenameSupplier, lineNumber, "Unexpected FIELD entry here!"); } ((ClassMapping) mappingStack.peek()).addFieldMapping(readField(parts)); } else if (token.equalsIgnoreCase("METHOD")) { if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof ClassMapping)) { - throw new MappingParseException(file, lineNumber, "Unexpected METHOD entry here!"); + throw new MappingParseException(filenameSupplier, lineNumber, "Unexpected METHOD entry here!"); } MethodMapping methodMapping = readMethod(parts); ((ClassMapping) mappingStack.peek()).addMethodMapping(methodMapping); mappingStack.push(methodMapping); } else if (token.equalsIgnoreCase("ARG")) { if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof MethodMapping)) { - throw new MappingParseException(file, lineNumber, "Unexpected ARG entry here!"); + throw new MappingParseException(filenameSupplier, lineNumber, "Unexpected ARG entry here!"); } ((MethodMapping) mappingStack.peek()).addArgumentMapping(readArgument(parts)); } } catch (ArrayIndexOutOfBoundsException | IllegalArgumentException ex) { - throw new MappingParseException(file, lineNumber, "Malformed line:\n" + line); + throw new MappingParseException(filenameSupplier, lineNumber, "Malformed line:\n" + line); } catch (MappingConflict e) { - throw new MappingParseException(file, lineNumber, e.getMessage()); + throw new MappingParseException(filenameSupplier, lineNumber, e.getMessage()); } } return mappings; diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java index 1e1c4ddc..b29990f5 100644 --- a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java +++ b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaWriter.java @@ -85,7 +85,7 @@ public class MappingsEnigmaWriter { } } - private void write(PrintWriter out, ClassMapping classMapping, int depth) throws IOException { + protected void write(PrintWriter out, ClassMapping classMapping, int depth) throws IOException { if (classMapping.getDeobfName() == null) { out.format("%sCLASS %s%s\n", getIndent(depth), classMapping.getObfFullName(), classMapping.getModifier() == Mappings.EntryModifier.UNCHANGED ? "" : classMapping.getModifier().getFormattedName()); @@ -134,7 +134,7 @@ public class MappingsEnigmaWriter { out.format("%sARG %d %s\n", getIndent(depth), localVariableMapping.getIndex(), localVariableMapping.getName()); } - private > List sorted(Iterable classes) { + protected > List sorted(Iterable classes) { List out = new ArrayList<>(); for (T t : classes) { out.add(t); diff --git a/src/main/java/cuchaz/enigma/throwables/MappingParseException.java b/src/main/java/cuchaz/enigma/throwables/MappingParseException.java index cc5f650a..b7e6d426 100644 --- a/src/main/java/cuchaz/enigma/throwables/MappingParseException.java +++ b/src/main/java/cuchaz/enigma/throwables/MappingParseException.java @@ -12,6 +12,7 @@ package cuchaz.enigma.throwables; import java.io.File; +import java.util.function.Supplier; public class MappingParseException extends Exception { @@ -25,6 +26,12 @@ public class MappingParseException extends Exception { filePath = file.getAbsolutePath(); } + public MappingParseException(Supplier filenameProvider, int line, String message) { + this.line = line; + this.message = message; + filePath = filenameProvider.get(); + } + @Override public String getMessage() { return "Line " + line + ": " + message + " in file " + filePath; -- cgit v1.2.3