Skip to content

Commit 116d7ac

Browse files
committed
fix(exec): 同步 3.0.x UTF-8 解码修复(Jackson 2 适配)
1 parent 7c78fcc commit 116d7ac

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

‎src/main/java/io/github/easy4j/kimi/cli/KimiCliExecutor.java‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,10 @@ private KimiCliResult runProcess(String stdin, String... args) {
168168
long startNanos = System.nanoTime();
169169
try {
170170
int exitCode = executor.execute(cmd);
171-
String out = stdout.toString().trim();
172-
String err = stderr.toString().trim();
171+
// The CLI emits UTF-8; decoding with the platform default charset
172+
// corrupts non-ASCII output on C-locale environments.
173+
String out = stdout.toString(StandardCharsets.UTF_8).trim();
174+
String err = stderr.toString(StandardCharsets.UTF_8).trim();
173175
log.debug("kimi CLI executed: exitCode={}, stdout.len={}", exitCode, out.length());
174176
if (watchdog.killedProcess()) {
175177
return new KimiCliResult(-1, out, "kimi CLI timed out after " + timeoutMs + " ms\n" + err);
@@ -182,8 +184,8 @@ private KimiCliResult runProcess(String stdin, String... args) {
182184
// with the real exit code instead of discarding the output. The
183185
// deadline check makes the timeout verdict race-free even when
184186
// {@code watchdog.killedProcess()} has not observed the kill yet.
185-
String out = stdout.toString().trim();
186-
String err = stderr.toString().trim();
187+
String out = stdout.toString(StandardCharsets.UTF_8).trim();
188+
String err = stderr.toString(StandardCharsets.UTF_8).trim();
187189
boolean timedOut = watchdog.killedProcess()
188190
|| System.nanoTime() - startNanos >= timeoutMs * 1_000_000L;
189191
if (timedOut) {

‎src/test/java/io/github/easy4j/kimi/cli/KimiCliExecutorTest.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ void shouldReturnIoExceptionMessageWhenExecutableMissing() {
8888
assertTrue(result.getStderr() != null && !result.getStderr().isEmpty());
8989
}
9090

91+
@Test
92+
void shouldDecodeUtf8OutputRegardlessOfPlatformCharset() {
93+
// POSIX printf octal escapes emit 你好 as raw UTF-8 bytes; with a
94+
// platform-default-charset decode this corrupts on C-locale JVMs.
95+
KimiCliExecutor executor = new KimiCliExecutor(configFor("/bin/sh"));
96+
97+
KimiCliResult result = executor.execute("-c", "printf '\\344\\275\\240\\345\\245\\275'");
98+
99+
assertEquals("你好", result.getStdout());
100+
}
101+
91102
@Test
92103
void shouldIgnoreNullArguments() {
93104
KimiCliExecutor executor = new KimiCliExecutor(configFor(ECHO));

0 commit comments

Comments
 (0)