Created
January 25, 2014 19:16
-
-
Save melwil/8621884 to your computer and use it in GitHub Desktop.
UTF-8 printing in windows console
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.sun.jna.Native; | |
import com.sun.jna.Pointer; | |
import com.sun.jna.ptr.IntByReference; | |
import com.sun.jna.win32.StdCallLibrary; | |
/** | |
* This class handles output for the ConsoleUI. | |
* Special handling for this is needed as UTF-8 output in Windows is unreliable | |
* due to a broken unicode page. | |
* | |
* @author Håvard Slettvold | |
*/ | |
public class Console { | |
private Kernel32 INSTANCE = null; | |
public Console() { | |
String os = System.getProperty("os.name").toLowerCase(); | |
if (os.startsWith("win")) { | |
INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class); | |
} | |
} | |
public interface Kernel32 extends StdCallLibrary { | |
public Pointer GetStdHandle(int nStdHandle); | |
public boolean WriteConsoleW(Pointer hConsoleOutput, char[] lpBuffer, int nNumberOfCharsToWrite, | |
IntByReference lpNumberOfCharsWritten, Pointer lpReserved); | |
} | |
public void print(String message) { | |
if (!attemptWindowsprint(message)) { | |
System.out.print(message); | |
} | |
} | |
public void println(String message) { | |
if (attemptWindowsprint(message)) { | |
System.out.println(); | |
} | |
else { | |
System.out.println(message); | |
} | |
} | |
/** | |
* Attempts to print text to the Windows console. | |
* | |
* @param message Message to print | |
* @return True if text was printed to a windows console | |
*/ | |
private boolean attemptWindowsprint(String message) { | |
boolean successful = false; | |
if (INSTANCE != null) { | |
Pointer handle = INSTANCE.GetStdHandle(-11); | |
char[] buffer = message.toCharArray(); | |
IntByReference lpNumberOfCharsWritten = new IntByReference(); | |
successful = INSTANCE.WriteConsoleW(handle, buffer, buffer.length, lpNumberOfCharsWritten, null); | |
} | |
return successful; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment