Skip to content

Commit

Permalink
Don't close reader when detecting format. (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 authored Nov 6, 2023
1 parent 9480165 commit b4f5fbd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
67 changes: 34 additions & 33 deletions src/main/java/net/fabricmc/mappingio/MappingReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,45 +58,46 @@ public static MappingFormat detectFormat(Reader reader) throws IOException {
int pos = 0;
int len;

try (BufferedReader br = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader)) {
br.mark(DETECT_HEADER_LEN);
// Be careful not to close the reader, thats upto the caller.
BufferedReader br = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader);

while (pos < buffer.length
&& (len = br.read(buffer, pos, buffer.length - pos)) >= 0) {
pos += len;
}

br.reset();
if (pos < 3) return null;

switch (String.valueOf(buffer, 0, 3)) {
case "v1\t":
return MappingFormat.TINY_FILE;
case "tin":
return MappingFormat.TINY_2_FILE;
case "tsr": // tsrg2 <nsA> <nsB> ..<nsN>
return MappingFormat.TSRG_2_FILE;
case "CLA":
return MappingFormat.ENIGMA_FILE;
case "PK:":
case "CL:":
case "MD:":
case "FD:":
return detectSrgOrXsrg(br);
}
br.mark(DETECT_HEADER_LEN);

String headerStr = String.valueOf(buffer, 0, pos);
while (pos < buffer.length
&& (len = br.read(buffer, pos, buffer.length - pos)) >= 0) {
pos += len;
}

if (headerStr.contains(" -> ")) {
return MappingFormat.PROGUARD_FILE;
} else if (headerStr.contains("\n\t")) {
return MappingFormat.TSRG_FILE;
}
br.reset();
if (pos < 3) return null;

switch (String.valueOf(buffer, 0, 3)) {
case "v1\t":
return MappingFormat.TINY_FILE;
case "tin":
return MappingFormat.TINY_2_FILE;
case "tsr": // tsrg2 <nsA> <nsB> ..<nsN>
return MappingFormat.TSRG_2_FILE;
case "CLA":
return MappingFormat.ENIGMA_FILE;
case "PK:":
case "CL:":
case "MD:":
case "FD:":
return detectSrgOrXsrg(br);
}

// TODO: CSRG
String headerStr = String.valueOf(buffer, 0, pos);

return null; // unknown format or corrupted
if (headerStr.contains(" -> ")) {
return MappingFormat.PROGUARD_FILE;
} else if (headerStr.contains("\n\t")) {
return MappingFormat.TSRG_FILE;
}

// TODO: CSRG

return null; // unknown format or corrupted
}

private static MappingFormat detectSrgOrXsrg(BufferedReader reader) throws IOException {
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/net/fabricmc/mappingio/read/DetectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.TestHelper;
import net.fabricmc.mappingio.format.MappingFormat;
import net.fabricmc.mappingio.tree.MemoryMappingTree;

public class DetectionTest {
private static Path dir;
Expand All @@ -54,11 +55,13 @@ public void enigmaDirectory() throws Exception {
@Test
public void tinyFile() throws Exception {
check("tiny.tiny", MappingFormat.TINY_FILE);
read("tiny.tiny");
}

@Test
public void tinyV2File() throws Exception {
check("tinyV2.tiny", MappingFormat.TINY_2_FILE);
read("tinyV2.tiny");
}

@Test
Expand Down Expand Up @@ -96,4 +99,15 @@ private void check(String file, MappingFormat format) throws Exception {
assertEquals(format, MappingReader.detectFormat(reader));
}
}

private void read(String file) throws Exception {
Path path = dir.resolve(file);

// Make sure we can read the mappings
MemoryMappingTree mappingTree = new MemoryMappingTree();

try (Reader reader = new InputStreamReader(Files.newInputStream(path), StandardCharsets.UTF_8)) {
MappingReader.read(reader, mappingTree);
}
}
}

0 comments on commit b4f5fbd

Please sign in to comment.