Stathis Andronikos
Stathis Andronikos

Reputation: 1259

PrintStream doesn't print correctly unicode characters ( UTF-16)

I want to print correctly unicode (let's say greek characters ) but I have problems. For example :

PrintStream oStream = new PrintStream(client.getOutputStream(), true, "UTF-8");
oStream.write(" Customer    : Γειά σου\r\n".getBytes());
oStream.write(" ΚΩΔΙΚΟΣ     : 00000234242\r\n".getBytes());
oStream.flush();
oStream.close();

                             OR
 OutputStreamWriter oStream = new OutputStreamWriter(client.getOutputStream(), "UTF-16");
    oStream.write(" Customer    : Γειά σου\r\n");
oStream.write(" ΚΩΔΙΚΟΣ     : 00000234242\r\n");
oStream.flush();
oStream.close();

The question is if there is any solution to print correctly all of the caharacters. I think for Greek characters UTF-16 is ok.

Upvotes: 1

Views: 4881

Answers (2)

ppeterka
ppeterka

Reputation: 20736

Never rely on the default encoding, much better to always specify, if you want to stick with the redundant way of creating a byte array to print, instead of the much more elegant, intuitive, and in every way superior way of using the String directly...

oStream.write(" Customer    : Γειά σου\r\n".getBytes("UTF-8"));
oStream.write(" ΚΩΔΙΚΟΣ     : 00000234242\r\n".getBytes("UTF-8"));

From comment of OP

client is an output stream & more aspecific a socket to output the result to a printer

That might be an issue! I wouldn't trust printers to get the UTF-8 plain string correctly. Be sure to test it with plain, ASCII strings to make sure everything else is OK.

EDIT

Xerox WorkCentre 24 PCL specific issues

From the PDL Reference Guide for the Xerox WorkCentre/WorkCentre Pro Series:

This document is not intended as a PS or PCL reference manual, but a guide for using the extended features WorkCentre’s in PS, PCL or ASCII print jobs.

There is not a single mention of UTF-8 anywhere...

From this doc at SAP: Xerox_SAP_Device_Types

To enable the Xerox® printer for Unicode printing, obtain the Unicode Printing Solution for models which support the UTF-8 character set, where an optional kit is available called Xerox® International Unicode Printing.

This doesn't sound good...

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503859

This is quite possibly the issue:

oStream.write(" Customer    : Γειά σου\r\n".getBytes());
oStream.write(" ΚΩΔΙΚΟΣ     : 00000234242\r\n".getBytes());

You're calling String.getBytes() with no encoding, to get a byte array using the platform default encoding. That's almost always a bad idea anyway, and it means that the fact that you specified UTF-8 earlier is irrelevant to these two lines. By the time the PrintStream gets the data, it's already in binary.

Try this instead:

oStream.print(" Customer    : Γειά σου\r\n");
oStream.print(" ΚΩΔΙΚΟΣ     : 00000234242\r\n");

Notes:

  • I would advise against using either PrintStream or PrintWriter. They swallow exceptions.
  • If you're only writing text, you should use a Writer subclass rather than an OutputStream subclass
  • It's unclear whether your source code is even being handled correctly: you need to check that whatever you're using to compile your code knows what encoding your source file is using.

I suggest you wrap your output stream in an OutputStreamWriter... that will allow you to specify the encoding, you won't have to worry about accidentally writing binary data (as the API doesn't allow it) and you won't see exceptions getting swallowed.

Upvotes: 3

Related Questions