Skip to content
Advertisement

How to properly decode and encode characters from JTextArea

I have a program that works on a console, and i want to make a custom console for it. Current command line interface can be started with a method that takes an InputStream and PrintStream as arguments.

I have two text areas (JTextArea), one of which i want to use for input and the other one for output. I’ve extended InputStream and OutputStreams to provide streams to my starting method:

JavaScript

And for the output:

JavaScript

Start the program:

JavaScript

(ui is an instance of a class that extends JFrame, the getConsoleIn() and getConsoleOut() return an instance of ConsoleInputStream and ConsoleOutputStream)

Inside of which i use scanner to read the input stream:

JavaScript

And this works fine, untill I try to use the local characters: ś ć ó ż ź etc.

I have tried many diffrent solutions, none worked for me. Then I tried to figure it out myself. Every time I read a char I also printed it to standard output (my IDE), which I know can display those characters correctly. I found out that they are being read correctly, but thre are characters (UTF-8 65535) inbetween them (not in a regular pattern but in pairs), for reasons that are unclear to me. I also tried:

JavaScript

with diffrent charsets, but couldn’t get them do display correctly.

What is the proper way to display those (and other UTF-8) characters?

Advertisement

Answer

I’m not sure whether you have done anything else wrong, but I know you at least need to fix this:

read and write methods don’t work with characters, they work with bytes. One character != one byte.

I’m talking about these:

JavaScript

You need to turn the char into a byte array using an encoding that the Scanner can understand. Then turn each of those bytes to unsigned ints, as opposed to treating each character as a single byte.

JavaScript

For write, you can’t treat each byte as a single character either. One way to handle this is to subclass PrintStream directly. See solution 2 in this answer for an example.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement