content: package com.alphabugx.util;
  
  import java.io.*;
  import java.nio.charset.Charset;
  import java.nio.charset.StandardCharsets;
  import java.util.Map;
  
  /**
   * 编码助手类 - 统一处理字符编码问题
   * 解决控制台输出乱码的问题
   */
  public class EncodingHelper {
      
      // 统一的字符编码
      public static final Charset UNIFIED_CHARSET = StandardCharsets.UTF_8;
      
      // 控制台输出编码（根据系统自动选择）
      public static final Charset CONSOLE_CHARSET = getConsoleCharset();
      
      /**
       * 获取控制台字符编码
       */
      private static Charset getConsoleCharset() {
          String osName = System.getProperty("os.name", "").toLowerCase();
          String encoding = System.getProperty("file.encoding", "UTF-8");
          String consoleEncoding = System.getProperty("console.encoding", encoding);
          
          try {
              // Windows系统特殊处理
              if (osName.contains("windows")) {
                  // 尝试使用GBK编码（中文Windows默认）
                  String windowsEncoding = System.getProperty("sun.jnu.encoding", "GBK");
                  return Charset.forName(windowsEncoding);
              } else {
                  // Unix/Linux系统使用UTF-8
                  return StandardCharsets.UTF_8;
              }
          } catch (Exception e) {
              // 降级到系统默认编码
              return Charset.defaultCharset();
          }
      }
      
      /**
       * 安全打印到控制台（自动处理编码）
       */
      public static void safePrintln(String message) {
          try {
              // 尝试使用正确的编码输出
              byte[] bytes = message.getBytes(CONSOLE_CHARSET);
              String safeMassage = new String(bytes, CONSOLE_CHARSET);
              System.out.println(safeMassage);
          } catch (Exception e) {
              // 降级到直接输出
              System.out.println(message);
          }
      }
      
      /**
       * 安全打印错误信息到控制台
       */
      public static void safeErrorPrintln(String message) {
          try {
              byte[] bytes = message.getBytes(CONSOLE_CHARSET);
              String safeMassage = new String(bytes, CONSOLE_CHARSET);
              System.err.println(safeMassage);
          } catch (Exception e) {
              System.err.println(message);
          }
      }
      
      /**
       * 为ProcessBuilder设置正确的编码环境
       */
      public static void setupProcessBuilderEncoding(ProcessBuilder pb) {
          Map<String, String> env = pb.environment();
          
          // 设置统一的字符编码环境变量
          env.put("LANG", "zh_CN.UTF-8");
          env.put("LC_ALL", "zh_CN.UTF-8");
          env.put("JAVA_TOOL_OPTIONS", "-Dfile.encoding=UTF-8 -Dconsole.encoding=UTF-8");
          
          // Windows特殊设置
          String osName = System.getProperty("os.name", "").toLowerCase();
          if (osName.contains("windows")) {
              env.put("CHCP", "65001"); // UTF-8 code page
          }
      }
      
      /**
       * 读取进程输出流并正确处理编码
       */
      public static String readProcessOutput(InputStream inputStream) throws IOException {
          StringBuilder output = new StringBuilder();
          
          try (BufferedReader reader = new BufferedReader(
                  new InputStreamReader(inputStream, CONSOLE_CHARSET))) {
              String line;
              while ((line = reader.readLine()) != null) {
                  if (output.length() > 0) {
                      output.append(System.lineSeparator());
                  }
                  output.append(line);
              }
          }
          
          return output.toString();
      }
      
      /**
       * 创建支持正确编码的PrintWriter
       */
      public static PrintWriter createEncodedPrintWriter(OutputStream out) {
          try {
              return new PrintWriter(new OutputStreamWriter(out, UNIFIED_CHARSET), true);
          } catch (Exception e) {
              return new PrintWriter(out, true);
          }
      }
      
      /**
       * 获取当前编码信息（用于调试）
       */
      public static void printEncodingInfo() {
          safePrintln("=== 编码信息 ===");
          safePrintln("系统默认编码: " + Charset.defaultCharset());
          safePrintln("file.encoding: " + System.getProperty("file.encoding"));
          safePrintln("console.encoding: " + System.getProperty("console.encoding", "未设置"));
          safePrintln("sun.jnu.encoding: " + System.getProperty("sun.jnu.encoding", "未设置"));
          safePrintln("操作系统: " + System.getProperty("os.name"));
          safePrintln("统一字符编码: " + UNIFIED_CHARSET);
          safePrintln("控制台编码: " + CONSOLE_CHARSET);
          safePrintln("===============");
      }
  } 