提交 399cf87d 编写于 作者: S sherman

4153167: separate between ANSI and OEM code pages on Windows

Summary: To use OEM code page for System.out&err when not redirected
Reviewed-by: alanb
上级 f17c9899
...@@ -1099,6 +1099,19 @@ public final class System { ...@@ -1099,6 +1099,19 @@ public final class System {
*/ */
public static native String mapLibraryName(String libname); public static native String mapLibraryName(String libname);
/**
* Create PrintStream for stdout/err based on encoding.
*/
private static PrintStream newPrintStream(FileOutputStream fos, String enc) {
if (enc != null) {
try {
return new PrintStream(new BufferedOutputStream(fos, 128), true, enc);
} catch (UnsupportedEncodingException uee) {}
}
return new PrintStream(new BufferedOutputStream(fos, 128), true);
}
/** /**
* Initialize the system class. Called after thread initialization. * Initialize the system class. Called after thread initialization.
*/ */
...@@ -1139,8 +1152,9 @@ public final class System { ...@@ -1139,8 +1152,9 @@ public final class System {
FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out); FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err); FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
setIn0(new BufferedInputStream(fdIn)); setIn0(new BufferedInputStream(fdIn));
setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true)); setOut0(newPrintStream(fdOut, props.getProperty("sun.stdout.encoding")));
setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true)); setErr0(newPrintStream(fdErr, props.getProperty("sun.stderr.encoding")));
// Load the zip library now in order to keep java.util.zip.ZipFile // Load the zip library now in order to keep java.util.zip.ZipFile
// from trying to use itself to load this library later. // from trying to use itself to load this library later.
loadLibrary("zip"); loadLibrary("zip");
......
...@@ -235,7 +235,14 @@ Java_java_lang_System_initProperties(JNIEnv *env, jclass cla, jobject props) ...@@ -235,7 +235,14 @@ Java_java_lang_System_initProperties(JNIEnv *env, jclass cla, jobject props)
} }
PUTPROP(props, "file.encoding", sprops->encoding); PUTPROP(props, "file.encoding", sprops->encoding);
PUTPROP(props, "sun.jnu.encoding", sprops->sun_jnu_encoding); PUTPROP(props, "sun.jnu.encoding", sprops->sun_jnu_encoding);
if (sprops->sun_stdout_encoding != NULL) {
PUTPROP(props, "sun.stdout.encoding", sprops->sun_stdout_encoding);
}
if (sprops->sun_stderr_encoding != NULL) {
PUTPROP(props, "sun.stderr.encoding", sprops->sun_stderr_encoding);
}
PUTPROP(props, "file.encoding.pkg", "sun.io"); PUTPROP(props, "file.encoding.pkg", "sun.io");
/* unicode_encoding specifies the default endianness */ /* unicode_encoding specifies the default endianness */
PUTPROP(props, "sun.io.unicode.encoding", sprops->unicode_encoding); PUTPROP(props, "sun.io.unicode.encoding", sprops->unicode_encoding);
PUTPROP(props, "sun.cpu.isalist", PUTPROP(props, "sun.cpu.isalist",
......
...@@ -66,6 +66,8 @@ typedef struct { ...@@ -66,6 +66,8 @@ typedef struct {
char *display_variant; char *display_variant;
char *encoding; char *encoding;
char *sun_jnu_encoding; char *sun_jnu_encoding;
char *sun_stdout_encoding;
char *sun_stderr_encoding;
char *timezone; char *timezone;
char *printerJob; char *printerJob;
......
...@@ -31,6 +31,9 @@ ...@@ -31,6 +31,9 @@
#include <sys/timeb.h> #include <sys/timeb.h>
#include <tchar.h> #include <tchar.h>
#include <stdlib.h>
#include <Wincon.h>
#include "locale_str.h" #include "locale_str.h"
#include "java_props.h" #include "java_props.h"
...@@ -123,6 +126,17 @@ getEncodingInternal(LCID lcid) ...@@ -123,6 +126,17 @@ getEncodingInternal(LCID lcid)
return ret; return ret;
} }
static char* getConsoleEncoding()
{
char* buf = malloc(16);
int cp = GetConsoleCP();
if (cp >= 874 && cp <= 950)
sprintf(buf, "ms%d", cp);
else
sprintf(buf, "cp%d", cp);
return buf;
}
// Exported entries for AWT // Exported entries for AWT
DllExport const char * DllExport const char *
getEncodingFromLangID(LANGID langID) getEncodingFromLangID(LANGID langID)
...@@ -562,6 +576,7 @@ GetJavaProperties(JNIEnv* env) ...@@ -562,6 +576,7 @@ GetJavaProperties(JNIEnv* env)
{ {
char * display_encoding; char * display_encoding;
HANDLE hStdOutErr;
// Windows UI Language selection list only cares "language" // Windows UI Language selection list only cares "language"
// information of the UI Language. For example, the list // information of the UI Language. For example, the list
...@@ -606,6 +621,20 @@ GetJavaProperties(JNIEnv* env) ...@@ -606,6 +621,20 @@ GetJavaProperties(JNIEnv* env)
sprops.encoding = "MS950_HKSCS"; sprops.encoding = "MS950_HKSCS";
sprops.sun_jnu_encoding = "MS950_HKSCS"; sprops.sun_jnu_encoding = "MS950_HKSCS";
} }
hStdOutErr = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdOutErr != INVALID_HANDLE_VALUE &&
GetFileType(hStdOutErr) == FILE_TYPE_CHAR) {
sprops.sun_stdout_encoding = getConsoleEncoding();
}
hStdOutErr = GetStdHandle(STD_ERROR_HANDLE);
if (hStdOutErr != INVALID_HANDLE_VALUE &&
GetFileType(hStdOutErr) == FILE_TYPE_CHAR) {
if (sprops.sun_stdout_encoding != NULL)
sprops.sun_stderr_encoding = sprops.sun_stdout_encoding;
else
sprops.sun_stderr_encoding = getConsoleEncoding();
}
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册