Skip to content

Commit 1fbfc11

Browse files
committed
fix(exec): 同步 UTF-8 输出解码修复——JDK 8 线经 toString("UTF-8") + 兜底 helper;附 POSIX printf 八进制转义回归测试
1 parent 9631444 commit 1fbfc11

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

src/main/java/io/github/easy4j/codex/cli/CodexCliExecutor.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ private CodexCliResult runProcess(String stdin, String... args) {
143143
long startNanos = System.nanoTime();
144144
try {
145145
int exitCode = executor.execute(cmd);
146-
String out = stdout.toString().trim();
147-
String err = stderr.toString().trim();
146+
String out = utf8(stdout).trim();
147+
String err = utf8(stderr).trim();
148148
log.debug("codex CLI executed: exitCode={}, stdout.len={}", exitCode, out.length());
149149
if (watchdog.killedProcess()) {
150150
return new CodexCliResult(-1, out, "codex CLI timed out after " + timeoutMs + " ms\n" + err);
@@ -157,8 +157,8 @@ private CodexCliResult runProcess(String stdin, String... args) {
157157
// with the real exit code instead of discarding the output. The
158158
// deadline check makes the timeout verdict race-free even when
159159
// {@code watchdog.killedProcess()} has not observed the kill yet.
160-
String out = stdout.toString().trim();
161-
String err = stderr.toString().trim();
160+
String out = utf8(stdout).trim();
161+
String err = utf8(stderr).trim();
162162
boolean timedOut = watchdog.killedProcess()
163163
|| System.nanoTime() - startNanos >= timeoutMs * 1_000_000L;
164164
if (timedOut) {
@@ -172,6 +172,21 @@ private CodexCliResult runProcess(String stdin, String... args) {
172172
}
173173
}
174174

175+
/**
176+
* Decodes the captured buffer as UTF-8 — the CLIs emit UTF-8 regardless of
177+
* platform, and the platform default charset would mojibake the output on
178+
* GBK-default Windows. {@code ByteArrayOutputStream.toString(Charset)}
179+
* only exists since Java 10, so the JDK 8 line goes through the String
180+
* name variant with an unreachable fallback (UTF-8 is guaranteed).
181+
*/
182+
private static String utf8(ByteArrayOutputStream buffer) {
183+
try {
184+
return buffer.toString("UTF-8");
185+
} catch (java.io.UnsupportedEncodingException e) {
186+
return buffer.toString();
187+
}
188+
}
189+
175190
/**
176191
* Lightweight reachability probe used by {@code CodexClient#isAvailable()}.
177192
*

0 commit comments

Comments
 (0)