Skip to content

Commit 72635a6

Browse files
amichairchrjohn
authored andcommitted
QFJ-965 Fixed regression when using non-default charset (#218)
(cherry picked from commit dc264ad)
1 parent 180fbd4 commit 72635a6

3 files changed

Lines changed: 15 additions & 7 deletions

File tree

quickfixj-core/src/main/java/org/quickfixj/CharsetSupport.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class CharsetSupport {
2929

3030
private static String charset = getDefaultCharset();
3131
private static Charset charsetInstance = Charset.forName(charset);
32+
private static boolean isStringEquivalent = isStringEquivalent(charsetInstance);
3233

3334
public static String getDefaultCharset() {
3435
return "ISO-8859-1";
@@ -47,9 +48,20 @@ public static boolean isStringEquivalent(Charset charset) {
4748
return charset.equals(CHARSET_ISO_8859_1) || charset.equals(CHARSET_ASCII);
4849
}
4950

51+
/**
52+
* Returns whether the current charset's byte representation of a string
53+
* is equivalent (as unsigned values) to the string characters themselves.
54+
*
55+
* @return whether the charset encoding is string-equivalent
56+
*/
57+
public static boolean isStringEquivalent() {
58+
return isStringEquivalent;
59+
}
60+
5061
public static void setCharset(String charset) throws UnsupportedEncodingException {
5162
CharsetSupport.charset = validate(charset);
5263
CharsetSupport.charsetInstance = Charset.forName(charset);
64+
CharsetSupport.isStringEquivalent = isStringEquivalent(charsetInstance);
5365
}
5466

5567
public static String getCharset() {

quickfixj-core/src/main/java/quickfix/Message.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,6 @@ protected Context initialValue() {
142142
}
143143
};
144144

145-
protected static boolean IS_STRING_EQUIVALENT = CharsetSupport.isStringEquivalent(CharsetSupport.getCharsetInstance());
146-
147145
/**
148146
* Do not call this method concurrently while modifying the contents of the message.
149147
* This is likely to produce unexpected results or will fail with a ConcurrentModificationException
@@ -156,7 +154,7 @@ protected Context initialValue() {
156154
@Override
157155
public String toString() {
158156
Context context = stringContexts.get();
159-
if (IS_STRING_EQUIVALENT) { // length & checksum can easily be calculated after message is built
157+
if (CharsetSupport.isStringEquivalent()) { // length & checksum can easily be calculated after message is built
160158
header.setField(context.bodyLength);
161159
trailer.setField(context.checkSum);
162160
} else {
@@ -168,7 +166,7 @@ public String toString() {
168166
header.calculateString(stringBuilder, null, null);
169167
calculateString(stringBuilder, null, null);
170168
trailer.calculateString(stringBuilder, null, null);
171-
if (IS_STRING_EQUIVALENT) {
169+
if (CharsetSupport.isStringEquivalent()) {
172170
setBodyLength(stringBuilder);
173171
setChecksum(stringBuilder);
174172
}

quickfixj-core/src/test/java/quickfix/MessageTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,16 +303,14 @@ public void testEmbeddedMessage() throws Exception {
303303

304304
private void doTestMessageWithEncodedField(String charset, String text) throws Exception {
305305
CharsetSupport.setCharset(charset);
306-
NewOrderSingle order = createNewOrderSingle();
307-
NewOrderSingle.IS_STRING_EQUIVALENT = CharsetSupport.isStringEquivalent(CharsetSupport.getCharsetInstance());
308306
try {
307+
NewOrderSingle order = createNewOrderSingle();
309308
order.set(new EncodedTextLen(MessageUtils.length(CharsetSupport.getCharsetInstance(), text)));
310309
order.set(new EncodedText(text));
311310
final Message msg = new Message(order.toString(), DataDictionaryTest.getDictionary());
312311
assertEquals(charset + " encoded field", text, msg.getString(EncodedText.FIELD));
313312
} finally {
314313
CharsetSupport.setCharset(CharsetSupport.getDefaultCharset());
315-
NewOrderSingle.IS_STRING_EQUIVALENT = CharsetSupport.isStringEquivalent(CharsetSupport.getCharsetInstance());
316314
}
317315
}
318316

0 commit comments

Comments
 (0)