fix: error loading resource map file from bundled jar (#1020)

This commit is contained in:
Skylot
2020-11-19 13:07:26 +00:00
parent d3f5154c19
commit 4bc6007a4d
3 changed files with 46 additions and 12 deletions
@@ -1,5 +1,9 @@
package jadx.core.utils.android;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
@@ -13,13 +17,15 @@ import jadx.core.utils.exceptions.JadxRuntimeException;
public class TextResMapFile {
private static final int SPLIT_POS = 8;
public static Map<Integer, String> read(Path resMapFile) {
try {
public static Map<Integer, String> read(InputStream is) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
Map<Integer, String> resMap = new HashMap<>();
for (String line : Files.readAllLines(resMapFile)) {
int id = Integer.parseInt(line.substring(0, SPLIT_POS), 16);
String name = line.substring(SPLIT_POS + 1);
resMap.put(id, name);
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
parseLine(resMap, line);
}
return resMap;
} catch (Exception e) {
@@ -27,6 +33,20 @@ public class TextResMapFile {
}
}
private static void parseLine(Map<Integer, String> resMap, String line) {
int id = Integer.parseInt(line.substring(0, SPLIT_POS), 16);
String name = line.substring(SPLIT_POS + 1);
resMap.put(id, name);
}
public static Map<Integer, String> read(Path resMapFile) {
try (InputStream in = Files.newInputStream(resMapFile)) {
return read(in);
} catch (Exception e) {
throw new JadxRuntimeException("Failed to read res-map file", e);
}
}
public static void write(Path resMapFile, Map<Integer, String> inputResMap) {
try {
Map<Integer, String> resMap = new TreeMap<>(inputResMap);
@@ -34,7 +54,7 @@ public class TextResMapFile {
for (Map.Entry<Integer, String> entry : resMap.entrySet()) {
lines.add(String.format("%08x=%s", entry.getKey(), entry.getValue()));
}
Files.write(resMapFile, lines);
Files.write(resMapFile, lines, StandardCharsets.UTF_8);
} catch (Exception e) {
throw new JadxRuntimeException("Failed to write res-map file", e);
}
@@ -1,7 +1,6 @@
package jadx.core.xmlgen.entry;
import java.net.URL;
import java.nio.file.Paths;
import java.io.InputStream;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
@@ -34,9 +33,8 @@ public class ValuesParser extends ParserConstants {
}
private static Map<Integer, String> loadAndroidResMap() {
try {
URL resMapUrl = ValuesParser.class.getResource("/android/res-map.txt");
return TextResMapFile.read(Paths.get(resMapUrl.toURI()));
try (InputStream is = ValuesParser.class.getResourceAsStream("/android/res-map.txt")) {
return TextResMapFile.read(is);
} catch (Exception e) {
throw new JadxRuntimeException("Failed to load android resource file", e);
}
@@ -0,0 +1,16 @@
package jadx.core.xmlgen.entry;
import java.util.Map;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class ValuesParserTest {
@Test
void testResMapLoad() {
Map<Integer, String> androidResMap = ValuesParser.getAndroidResMap();
assertThat(androidResMap).isNotNull().isNotEmpty();
}
}