Skip to content

Commit 7e3a071

Browse files
authored
QFJ-684 - added charset support to BytesField (#289)
1 parent 9bdd8b8 commit 7e3a071

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919

2020
package quickfix;
2121

22-
import java.io.UnsupportedEncodingException;
23-
24-
import org.quickfixj.QFJException;
22+
import org.quickfixj.CharsetSupport;
2523

2624
/**
2725
* BytesField enables better handling of binary data. With BytesFields binary data can
@@ -47,11 +45,6 @@ public byte[] getValue() {
4745

4846
@Override
4947
protected String objectAsString() {
50-
try {
51-
return new String(getObject(), "UTF8");
52-
} catch (UnsupportedEncodingException e) {
53-
throw new QFJException(e);
54-
}
48+
return new String(getObject(), CharsetSupport.getCharsetInstance());
5549
}
56-
5750
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,25 @@ public void testBytesField() {
250250
assertEquals("96=rawdata", sb.toString());
251251
}
252252

253+
@Test
254+
public void testBytesFieldFullRange() {
255+
byte[] data = new byte[256];
256+
257+
for (int i = 0; i < 256; i++) {
258+
data[i] = (byte) i;
259+
}
260+
261+
BytesField field = new BytesField(RawData.FIELD);
262+
field.setValue(data);
263+
264+
byte[] tagPrefixedData = field.toString().getBytes(CharsetSupport.getCharsetInstance());
265+
assertEquals(256 + 3, tagPrefixedData.length);
266+
267+
for (int i = 0; i < data.length; ++i) {
268+
assertEquals(data[i], tagPrefixedData[i + 3]);
269+
}
270+
}
271+
253272
@Test
254273
public void testFieldhashCode() throws Exception {
255274
assertEqualsAndHash(new IntField(11, 100), new IntField(11, 100));

0 commit comments

Comments
 (0)