Fixed console output encoding (PY-2283).
authorDmitry Trofimov <dmitry.trofimov@jetbrains.com>
Mon, 15 Aug 2011 16:33:33 +0000 (20:33 +0400)
committerDmitry Trofimov <dmitry.trofimov@jetbrains.com>
Mon, 15 Aug 2011 16:33:33 +0000 (20:33 +0400)
platform/lang-impl/src/com/intellij/execution/impl/ConsoleViewRunningState.java

index 59773c09ce9f28799c06bc4166cd03879cd91243..3929dede96beb9dfb0d3d5a49de6c4b796736239 100644 (file)
 package com.intellij.execution.impl;
 
 import com.intellij.execution.ExecutionBundle;
+import com.intellij.execution.process.OSProcessHandler;
 import com.intellij.execution.process.ProcessAdapter;
 import com.intellij.execution.process.ProcessEvent;
 import com.intellij.execution.process.ProcessHandler;
 import com.intellij.execution.ui.ConsoleViewContentType;
 import com.intellij.openapi.util.Key;
+import com.intellij.openapi.vfs.encoding.EncodingManager;
 import org.jetbrains.annotations.NotNull;
 
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.Writer;
+import java.nio.charset.Charset;
 
 
 public class ConsoleViewRunningState extends ConsoleState {
@@ -58,12 +61,25 @@ public class ConsoleViewRunningState extends ConsoleState {
     // attach to process stdin
     if (attachToStdIn) {
       final OutputStream processInput = myProcessHandler.getProcessInput();
-      myUserInputWriter = processInput != null ? new OutputStreamWriter(processInput) : null;
-    } else {
+      myUserInputWriter = processInput != null ? createOutputStreamWriter(processInput, processHandler) : null;
+    }
+    else {
       myUserInputWriter = null;
     }
   }
 
+  private static OutputStreamWriter createOutputStreamWriter(OutputStream processInput, ProcessHandler processHandler) {
+    Charset charset;
+    if (processHandler instanceof OSProcessHandler) {
+      charset = ((OSProcessHandler)processHandler).getCharset();
+    }
+    else {
+      charset = EncodingManager.getInstance().getDefaultCharset();
+    }
+
+    return new OutputStreamWriter(processInput, charset);
+  }
+
   @NotNull
   public ConsoleState dispose() {
     if (myProcessHandler != null) {
@@ -81,8 +97,9 @@ public class ConsoleViewRunningState extends ConsoleState {
   }
 
   public void sendUserInput(final String input) throws IOException {
-    if (myUserInputWriter == null)
+    if (myUserInputWriter == null) {
       throw new IOException(ExecutionBundle.message("no.user.process.input.error.message"));
+    }
     myUserInputWriter.write(input);
     myUserInputWriter.flush();
   }