diff options
| author | 2018-07-10 17:17:55 +0800 | |
|---|---|---|
| committer | 2018-07-10 17:29:04 +0800 | |
| commit | d980311d214c3e1e0234eb011d5ced6701ce3a5a (patch) | |
| tree | 31d28744416472094384efcccb58b871b9e31d3c /src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java | |
| parent | Merge pull request #6 from thiakil/speedy-getsources (diff) | |
| download | enigma-fork-d980311d214c3e1e0234eb011d5ced6701ce3a5a.tar.gz enigma-fork-d980311d214c3e1e0234eb011d5ced6701ce3a5a.tar.xz enigma-fork-d980311d214c3e1e0234eb011d5ced6701ce3a5a.zip | |
Allow reader/writer subclasses to provide the underlying file operations
Diffstat (limited to 'src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java index a53793d..ddbee76 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; | |||
| 5 | import cuchaz.enigma.throwables.MappingConflict; | 5 | import cuchaz.enigma.throwables.MappingConflict; |
| 6 | import cuchaz.enigma.throwables.MappingParseException; | 6 | import cuchaz.enigma.throwables.MappingParseException; |
| 7 | 7 | ||
| 8 | import java.io.*; | 8 | import java.io.BufferedReader; |
| 9 | import java.io.File; | ||
| 10 | import java.io.FileInputStream; | ||
| 11 | import java.io.IOException; | ||
| 12 | import java.io.InputStream; | ||
| 13 | import java.io.InputStreamReader; | ||
| 9 | import java.util.Deque; | 14 | import java.util.Deque; |
| 15 | import java.util.function.Supplier; | ||
| 10 | 16 | ||
| 11 | public class MappingsEnigmaReader { | 17 | public class MappingsEnigmaReader { |
| 12 | 18 | ||
| @@ -39,7 +45,11 @@ public class MappingsEnigmaReader { | |||
| 39 | } | 45 | } |
| 40 | 46 | ||
| 41 | public Mappings readFile(Mappings mappings, File file) throws IOException, MappingParseException { | 47 | public Mappings readFile(Mappings mappings, File file) throws IOException, MappingParseException { |
| 42 | try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charsets.UTF_8))) { | 48 | return readFileStream(mappings, new FileInputStream(file), file::getAbsolutePath); |
| 49 | } | ||
| 50 | |||
| 51 | public Mappings readFileStream(Mappings mappings, InputStream stream, Supplier<String> filenameSupplier) throws IOException, MappingParseException { | ||
| 52 | try (BufferedReader in = new BufferedReader(new InputStreamReader(stream, Charsets.UTF_8))) { | ||
| 43 | Deque<Object> mappingStack = Queues.newArrayDeque(); | 53 | Deque<Object> mappingStack = Queues.newArrayDeque(); |
| 44 | 54 | ||
| 45 | int lineNumber = 0; | 55 | int lineNumber = 0; |
| @@ -87,7 +97,7 @@ public class MappingsEnigmaReader { | |||
| 87 | 97 | ||
| 88 | // inner class | 98 | // inner class |
| 89 | if (!(mappingStack.peek() instanceof ClassMapping)) { | 99 | if (!(mappingStack.peek() instanceof ClassMapping)) { |
| 90 | throw new MappingParseException(file, lineNumber, "Unexpected CLASS entry here!"); | 100 | throw new MappingParseException(filenameSupplier, lineNumber, "Unexpected CLASS entry here!"); |
| 91 | } | 101 | } |
| 92 | 102 | ||
| 93 | classMapping = readClass(parts, true); | 103 | classMapping = readClass(parts, true); |
| @@ -96,26 +106,26 @@ public class MappingsEnigmaReader { | |||
| 96 | mappingStack.push(classMapping); | 106 | mappingStack.push(classMapping); |
| 97 | } else if (token.equalsIgnoreCase("FIELD")) { | 107 | } else if (token.equalsIgnoreCase("FIELD")) { |
| 98 | if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof ClassMapping)) { | 108 | if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof ClassMapping)) { |
| 99 | throw new MappingParseException(file, lineNumber, "Unexpected FIELD entry here!"); | 109 | throw new MappingParseException(filenameSupplier, lineNumber, "Unexpected FIELD entry here!"); |
| 100 | } | 110 | } |
| 101 | ((ClassMapping) mappingStack.peek()).addFieldMapping(readField(parts)); | 111 | ((ClassMapping) mappingStack.peek()).addFieldMapping(readField(parts)); |
| 102 | } else if (token.equalsIgnoreCase("METHOD")) { | 112 | } else if (token.equalsIgnoreCase("METHOD")) { |
| 103 | if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof ClassMapping)) { | 113 | if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof ClassMapping)) { |
| 104 | throw new MappingParseException(file, lineNumber, "Unexpected METHOD entry here!"); | 114 | throw new MappingParseException(filenameSupplier, lineNumber, "Unexpected METHOD entry here!"); |
| 105 | } | 115 | } |
| 106 | MethodMapping methodMapping = readMethod(parts); | 116 | MethodMapping methodMapping = readMethod(parts); |
| 107 | ((ClassMapping) mappingStack.peek()).addMethodMapping(methodMapping); | 117 | ((ClassMapping) mappingStack.peek()).addMethodMapping(methodMapping); |
| 108 | mappingStack.push(methodMapping); | 118 | mappingStack.push(methodMapping); |
| 109 | } else if (token.equalsIgnoreCase("ARG")) { | 119 | } else if (token.equalsIgnoreCase("ARG")) { |
| 110 | if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof MethodMapping)) { | 120 | if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof MethodMapping)) { |
| 111 | throw new MappingParseException(file, lineNumber, "Unexpected ARG entry here!"); | 121 | throw new MappingParseException(filenameSupplier, lineNumber, "Unexpected ARG entry here!"); |
| 112 | } | 122 | } |
| 113 | ((MethodMapping) mappingStack.peek()).addArgumentMapping(readArgument(parts)); | 123 | ((MethodMapping) mappingStack.peek()).addArgumentMapping(readArgument(parts)); |
| 114 | } | 124 | } |
| 115 | } catch (ArrayIndexOutOfBoundsException | IllegalArgumentException ex) { | 125 | } catch (ArrayIndexOutOfBoundsException | IllegalArgumentException ex) { |
| 116 | throw new MappingParseException(file, lineNumber, "Malformed line:\n" + line); | 126 | throw new MappingParseException(filenameSupplier, lineNumber, "Malformed line:\n" + line); |
| 117 | } catch (MappingConflict e) { | 127 | } catch (MappingConflict e) { |
| 118 | throw new MappingParseException(file, lineNumber, e.getMessage()); | 128 | throw new MappingParseException(filenameSupplier, lineNumber, e.getMessage()); |
| 119 | } | 129 | } |
| 120 | } | 130 | } |
| 121 | return mappings; | 131 | return mappings; |