4153167: separate between ANSI and OEM code pages on Windows

To use OEM code page for System.out&err when not redirected

Reviewed-by: alanb
This commit is contained in:
Xueming Shen 2012-02-16 22:13:10 -08:00
parent ac69a5f84a
commit 1b32c44aa3
4 changed files with 54 additions and 2 deletions

View File

@ -1099,6 +1099,19 @@ public final class System {
*/
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.
*/
@ -1139,8 +1152,9 @@ public final class System {
FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
setIn0(new BufferedInputStream(fdIn));
setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));
setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true));
setOut0(newPrintStream(fdOut, props.getProperty("sun.stdout.encoding")));
setErr0(newPrintStream(fdErr, props.getProperty("sun.stderr.encoding")));
// Load the zip library now in order to keep java.util.zip.ZipFile
// from trying to use itself to load this library later.
loadLibrary("zip");

View File

@ -235,7 +235,14 @@ Java_java_lang_System_initProperties(JNIEnv *env, jclass cla, jobject props)
}
PUTPROP(props, "file.encoding", sprops->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");
/* unicode_encoding specifies the default endianness */
PUTPROP(props, "sun.io.unicode.encoding", sprops->unicode_encoding);
PUTPROP(props, "sun.cpu.isalist",

View File

@ -66,6 +66,8 @@ typedef struct {
char *display_variant;
char *encoding;
char *sun_jnu_encoding;
char *sun_stdout_encoding;
char *sun_stderr_encoding;
char *timezone;
char *printerJob;

View File

@ -31,6 +31,9 @@
#include <sys/timeb.h>
#include <tchar.h>
#include <stdlib.h>
#include <Wincon.h>
#include "locale_str.h"
#include "java_props.h"
@ -123,6 +126,17 @@ getEncodingInternal(LCID lcid)
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
DllExport const char *
getEncodingFromLangID(LANGID langID)
@ -562,6 +576,7 @@ GetJavaProperties(JNIEnv* env)
{
char * display_encoding;
HANDLE hStdOutErr;
// Windows UI Language selection list only cares "language"
// information of the UI Language. For example, the list
@ -606,6 +621,20 @@ GetJavaProperties(JNIEnv* env)
sprops.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();
}
}
}