DBCS, Unicode, and Java Previous
Previous
Introduction
Introduction
Next
Next

Microsoft VM for Java Has Working Conversion Methods

Using the getLocalizedInputStream Method , Using the getLocalizedOutputStream Method

Microsoft's Java VM Win32 Reference Implementation contains working versions of the Runtime methods getLocalizedInputStream and getLocalizedOutputStream. These methods are part of the Java language specification. Using these methods, you can write Java programs that can convert DBCS to Unicode and vice-versa.


Using the getLocalizedInputStream Method

The getLocalizedInputStream method converts DBCS to Unicode. To use it, you must first call Runtime.getRuntime() to get a Runtime object. Then, you call Runtime's getLocalizedInputStream method, passing the input stream object that you want to have converted. getLocalizedInputStream returns a DataInputStream object that does DBCS to Unicode conversion on the input stream. The following example demonstrates this:

Runtime rt = Runtime.getRuntime();
FileInputStream fisDBCS;
DataInputStream disUnicode;
String sInput;

fisDBCS = new FileInputStream("DBCS.TXT");
disUnicode = (DataInputStream)rt.getLocalizedInputStream(fisDBCS);
sInput = disUnicode.readLine(); 

In the code fragment above, DBCS.TXT is a file containing DBCS text strings. fisDBCS is a file input stream object that opens the DBCS.TXT file. disUnicode is a DBCS-to-Unicode input stream that is created by passing fisDBCS through getLocalizedInputStream. The call to disUnicode.readLine() gets a line of DBCS text from DBCS.TXT, converts it to Unicode, and places the resulting Unicode string in the sInput String object.


Using the getLocalizedOutputStream Method

The getLocalizedOutputStream method converts Unicode to DBCS. To use it, you must first call Runtime.getRuntime() to get a Runtime object. Then, you call Runtime's getLocalizedOutputStream method, passing the output stream object that you want to have converted. getLocalizedOutputStream returns a DataOutputStream object that does Unicode to DBCS conversion on the output stream. Example:

Runtime rt = Runtime.getRuntime();
FileOutputStream fosUnicode;
DataOutputStream dosDBCS;
String sOutput = "This is a Unicode string";

fosUnicode = new FileOutputStream("OUTPUT.TXT");
dosDBCS = (DataOutputStream)rt.getLocalizedOutputStream(fosUnicode);
dosDBCS.writeChars(sOutput); 

In the code fragment above, OUTPUT.TXT is a new text file that is going to contain DBCS text strings. fosUnicode is a file output stream object that opens the OUTPUT.TXT file. dosDBCS is a Unicode-to-DBCS output stream that is created by passing fosUnicode through getLocalizedOutputStream. The call to dosDBCS.writeChars() converts the Unicode string sOutput to DBCS, and places the resulting DBCS string in the OUTPUT.TXT file.

Top© 1996 Microsoft Corporation. All rights reserved.