Skip to content

Commit 046f0b7

Browse files
authored
Merge pull request #59 from irockel/fix/Issue54_DetectUtf16
fix (#54): added detection for utf16
2 parents 794b4e2 + 910a01f commit 046f0b7

File tree

4 files changed

+60
-13
lines changed

4 files changed

+60
-13
lines changed

tda/dependency-reduced-pom.xml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<plugins>
2929
<plugin>
3030
<artifactId>maven-dependency-plugin</artifactId>
31-
<version>3.6.0</version>
31+
<version>3.10.0</version>
3232
<executions>
3333
<execution>
3434
<id>unpack-licenses</id>
@@ -41,15 +41,15 @@
4141
<artifactItem>
4242
<groupId>com.formdev</groupId>
4343
<artifactId>flatlaf</artifactId>
44-
<version>3.3</version>
44+
<version>3.7.1</version>
4545
<type>jar</type>
4646
<includes>META-INF/LICENSE</includes>
4747
<outputDirectory>${project.build.directory}/licenses/flatlaf</outputDirectory>
4848
</artifactItem>
4949
<artifactItem>
5050
<groupId>com.formdev</groupId>
5151
<artifactId>flatlaf-extras</artifactId>
52-
<version>3.3</version>
52+
<version>3.7.1</version>
5353
<type>jar</type>
5454
<includes>META-INF/LICENSE</includes>
5555
<outputDirectory>${project.build.directory}/licenses/flatlaf-extras</outputDirectory>
@@ -69,7 +69,7 @@
6969
</plugin>
7070
<plugin>
7171
<artifactId>maven-antrun-plugin</artifactId>
72-
<version>3.1.0</version>
72+
<version>3.2.0</version>
7373
<executions>
7474
<execution>
7575
<id>rename-licenses</id>
@@ -89,7 +89,7 @@
8989
</plugin>
9090
<plugin>
9191
<artifactId>maven-compiler-plugin</artifactId>
92-
<version>3.8.1</version>
92+
<version>3.15.0</version>
9393
<configuration>
9494
<compilerArgs>
9595
<arg>--add-modules</arg>
@@ -100,7 +100,7 @@
100100
</plugin>
101101
<plugin>
102102
<artifactId>maven-surefire-plugin</artifactId>
103-
<version>3.0.0-M5</version>
103+
<version>3.5.5</version>
104104
<configuration>
105105
<systemPropertyVariables>
106106
<java.awt.headless>true</java.awt.headless>
@@ -109,7 +109,7 @@
109109
</plugin>
110110
<plugin>
111111
<artifactId>maven-jar-plugin</artifactId>
112-
<version>3.2.0</version>
112+
<version>3.5.0</version>
113113
<configuration>
114114
<archive>
115115
<manifest>
@@ -125,7 +125,7 @@
125125
</plugin>
126126
<plugin>
127127
<artifactId>maven-shade-plugin</artifactId>
128-
<version>3.5.0</version>
128+
<version>3.6.2</version>
129129
<executions>
130130
<execution>
131131
<phase>package</phase>
@@ -161,7 +161,7 @@
161161
<dependency>
162162
<groupId>org.junit.jupiter</groupId>
163163
<artifactId>junit-jupiter</artifactId>
164-
<version>6.0.2</version>
164+
<version>6.0.3</version>
165165
<scope>test</scope>
166166
<exclusions>
167167
<exclusion>

tda/src/main/java/de/grimmfrost/tda/parser/DumpParserFactory.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@
2424
import de.grimmfrost.tda.utils.DateMatcher;
2525
import de.grimmfrost.tda.utils.LogManager;
2626
import de.grimmfrost.tda.utils.PrefManager;
27+
import java.io.BufferedInputStream;
2728
import java.io.BufferedReader;
2829
import java.io.IOException;
2930
import java.io.InputStream;
3031
import java.io.InputStreamReader;
32+
import java.nio.charset.Charset;
33+
import java.nio.charset.StandardCharsets;
3134
import java.util.Map;
3235
import java.util.logging.Level;
3336
import java.util.logging.Logger;
@@ -74,10 +77,12 @@ public DumpParser getDumpParserForLogfile(InputStream dumpFileStream, Map<String
7477
int readAheadLimit = PrefManager.get().getStreamResetBuffer();
7578
int lineCounter = 0;
7679
DumpParser currentDumpParser = null;
77-
80+
7881
try {
79-
bis = new BufferedReader(new InputStreamReader(dumpFileStream));
80-
82+
BufferedInputStream bufferedStream = new BufferedInputStream(dumpFileStream);
83+
Charset charset = detectCharset(bufferedStream);
84+
bis = new BufferedReader(new InputStreamReader(bufferedStream, charset));
85+
8186
// reset current dump parser
8287
DateMatcher dm = new DateMatcher();
8388
while (bis.ready() && (currentDumpParser == null)) {
@@ -121,5 +126,32 @@ public DumpParser getDumpParserForLogfile(InputStream dumpFileStream, Map<String
121126
LOGGER.log(Level.SEVERE, "IO error detecting parser for logfile", ex);
122127
}
123128
return currentDumpParser;
124-
}
129+
}
130+
131+
private Charset detectCharset(BufferedInputStream bis) throws IOException {
132+
bis.mark(4);
133+
byte[] bom = new byte[4];
134+
int read = bis.read(bom);
135+
bis.reset();
136+
137+
if (read >= 2) {
138+
if (bom[0] == (byte) 0xFF && bom[1] == (byte) 0xFE) {
139+
return StandardCharsets.UTF_16LE;
140+
} else if (bom[0] == (byte) 0xFE && bom[1] == (byte) 0xFF) {
141+
return StandardCharsets.UTF_16BE;
142+
}
143+
}
144+
145+
// if no BOM, check for UTF-16LE/BE by looking for null bytes
146+
// IBM VMs often don't have BOM but use UTF-16LE
147+
if (read >= 4) {
148+
if (bom[1] == 0 && bom[3] == 0) {
149+
return StandardCharsets.UTF_16LE;
150+
} else if (bom[0] == 0 && bom[2] == 0) {
151+
return StandardCharsets.UTF_16BE;
152+
}
153+
}
154+
155+
return Charset.defaultCharset();
156+
}
125157
}

tda/src/test/java/de/grimmfrost/tda/parser/DumpParserFactoryTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,19 @@ public void testGetDumpParserForJSONLogfile() throws FileNotFoundException {
8282

8383
assertInstanceOf(JCmdJSONParser.class, result);
8484
}
85+
86+
/**
87+
* Test of getDumpParserForVersion method, of class de.grimmfrost.tda.DumpParserFactory.
88+
*/
89+
@Test
90+
public void testGetDumpParserForUTF16Logfile() throws FileNotFoundException {
91+
InputStream dumpFileStream = new FileInputStream("src/test/resources/java21dump_utf16.log");
92+
Map<String, Map<String, String>> threadStore = new HashMap<>();
93+
DumpParserFactory instance = DumpParserFactory.get();
94+
95+
DumpParser result = instance.getDumpParserForLogfile(dumpFileStream, threadStore, false, 0);
96+
assertNotNull(result);
97+
98+
assertInstanceOf(SunJDKParser.class, result);
99+
}
85100
}
18.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)