Skip to content

Commit 9c3709a

Browse files
committed
Backport fix for #323 to 2.9(.8)
1 parent 8fd9463 commit 9c3709a

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Project: jackson-dataformat-xml
88

99
#270: Add support for `writeBinary()` with `InputStream` to `ToXMLGenerator`
1010
(requested by csbxvs@github; contributed by marc-christian-schulze@github)
11+
#323: Replace slow string concatenation with faster `StringBuilder` (for
12+
long text content)
1113

1214
2.9.7 (19-Sep-2018)
1315

src/main/java/com/fasterxml/jackson/dataformat/xml/deser/XmlTokenStream.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -377,38 +377,41 @@ private final String _collectUntilTag() throws XMLStreamException
377377
return "";
378378
}
379379

380-
String text = null;
380+
CharSequence chars = null;
381381
while (true) {
382382
switch (_xmlReader.next()) {
383383
case XMLStreamConstants.START_ELEMENT:
384-
return (text == null) ? "" : text;
384+
return (chars == null) ? "" : chars.toString();
385385

386386
case XMLStreamConstants.END_ELEMENT:
387387
case XMLStreamConstants.END_DOCUMENT:
388388
// 04-May-2018, tatu: We could easily make <tag></tag> ALSO report
389389
// as `null`, by below, but that breaks existing tests so not
390390
// done at least until 3.0.
391391
/*
392-
if (text == null) {
392+
if (chars == null) {
393393
if (FromXmlParser.Feature.EMPTY_ELEMENT_AS_NULL.enabledIn(_formatFeatures)) {
394394
return null;
395395
}
396396
return "";
397397
}
398-
return text;
398+
return chars;
399399
*/
400-
return (text == null) ? "" : text;
401-
400+
return (chars == null) ? "" : chars.toString();
401+
402402
// note: SPACE is ignorable (and seldom seen), not to be included
403403
case XMLStreamConstants.CHARACTERS:
404404
case XMLStreamConstants.CDATA:
405405
// 17-Jul-2017, tatu: as per [dataformat-xml#236], need to try to...
406406
{
407407
String str = _getText(_xmlReader);
408-
if (text == null) {
409-
text = str;
410-
} else {
411-
text += str;
408+
if (chars == null) {
409+
chars = str;
410+
} else {
411+
if (chars instanceof String) {
412+
chars = new StringBuilder(chars);
413+
}
414+
((StringBuilder)chars).append(str);
412415
}
413416
}
414417
break;

0 commit comments

Comments
 (0)