Character string conversions in IBM MQ classes for Java

From IBM® MQ 8.0, some of the default behavior regarding character string conversion in the IBM MQ classes for Java has changed.

Before IBM MQ 8.0, string conversions in IBM MQ classes for Java was done by calling the java.nio.charset.Charset.decode(ByteBuffer) and Charset.encode(CharBuffer) methods.

Using either of these methods results in a default replacement ( REPLACE) of malformed or untranslatable data.

This behavior can obscure errors in applications, and lead to unexpected characters, for example ?, in translated data. From IBM MQ 8.0, to detect such issues earlier and more effectively, the IBM MQ classes for Java use CharsetEncoders and CharsetDecoders directly and configure the handling of malformed and untranslatable data explicitly.

From IBM MQ 8.0, the default behavior is to REPORT such issues by throwing a suitable MQException.

Configuring

Translating from UTF-16 (the character representation used in Java) to a native character set, such as UTF-8, is termed encoding, while translating in the opposite direction is termed decoding.

Currently, decoding takes the default behavior for CharsetDecoders, reporting errors by throwing an exception.

One setting is used to specify a java.nio.charset.CodingErrorAction to control error handling on both encoding and decoding. One other setting is used to control the replacement byte, or bytes, when encoding. The default Java replacement String is used in decoding operations.

IBM MQ Classes for Java

com.ibm.mq.MQMD

Two new fields have been added:
byte[] unMappableReplacement
The byte sequence that will be written to an encoded string if an input character cannot be translated, and you have specified REPLACE.
Default: "?".getBytes()
The default Java replacement String is used in decoding operations.
java.nio.charset.CodingErrorAction unmappableAction
Specifies the action to be taken for untranslatable data on encoding and decoding:
Default: CodingErrorAction.REPORT;

Setting system defaults

From IBM MQ 8.0, the following two Java system properties are available to configure default behavior regarding character string conversion.
com.ibm.mq.cfg.jmqi.UnmappableCharacterAction
Specifies the action to be taken for untranslatable data on encoding and decoding. The value can be REPORT, REPLACE, or IGNORE.
com.ibm.mq.cfg.jmqi.UnmappableCharacterReplacement
Sets or gets the replacement bytes to apply when a character cannot be mapped in an encoding operation The default Java replacement string is used in decoding operations.

To avoid confusion between Java character and native byte representations, you should specify com.ibm.mq.cfg.jmqi.UnmappableCharacterReplacement as a decimal number representing the replacement byte in the native character set.

For example, the decimal value of ?, as a native byte, is 63 if the native character set is ASCII-based, such as ISO-8859-1, while it is 111 if the native character set is EBCDIC.
Note: Note that if an MQMD or MQMessage object has either the unmappableAction or unMappableReplacement fields set, then the values of these fields take precedence over the Java system properties. This allows the values specified by the Java system properties to be overridden for each message if required.