Skip to content

Commit 14b58b9

Browse files
authored
Fixing the issue where multi-byte characters are split in writeCData() if first byte sits right at the end of the buffer (#87)
1 parent e45c2eb commit 14b58b9

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/main/java/com/fasterxml/aalto/out/ByteXmlWriter.java

+7
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,13 @@ public int writeCData(char[] cbuf, int offset, int len)
792792
protected int writeCDataContents(char[] cbuf, int offset, int len)
793793
throws IOException, XMLStreamException
794794
{
795+
if (_surrogate != 0) {
796+
outputSurrogates(_surrogate, cbuf[offset]);
797+
// reset the temporary surrogate storage
798+
_surrogate = 0;
799+
++offset;
800+
--len;
801+
}
795802
/* Unlike with writeCharacters() and fastWriteName(), let's not
796803
* worry about split buffers here: this is unlikely to become
797804
* performance bottleneck. This allows keeping it simple; and

src/test/java/com/fasterxml/aalto/sax/TestSaxWriter.java

+25
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,29 @@ public void testSurrogateMemory2() throws Exception {
5454
writer.writeEndTag(writer.constructName("testelement"));
5555
writer.close(false);
5656
}
57+
58+
public void testSurrogateMemory3() throws Exception {
59+
// This test aims to produce the
60+
// javax.xml.stream.XMLStreamException: Incomplete surrogate pair in content: first char 0xdfce, second 0x78
61+
// error message. The issue was similar to the one described in testSurrogateMemory1(), except it happened in
62+
// ByteXmlWriter#writeCDataContents(), where check for existing _surrogate was missing prior to the fix,
63+
// as opposed to ByteXmlWriter#writeCharacters().
64+
StringBuilder testText = new StringBuilder();
65+
for (int i = 0; i < 511; i++)
66+
testText.append('x');
67+
testText.append("\uD835\uDFCE");
68+
for (int i = 0; i < 512; i++)
69+
testText.append('x');
70+
71+
WriterConfig writerConfig = new WriterConfig();
72+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
73+
Utf8XmlWriter writer = new Utf8XmlWriter(writerConfig, byteArrayOutputStream);
74+
writer.writeStartTagStart(writer.constructName("testelement"));
75+
writer.writeCData(testText.toString());
76+
writer.writeStartTagEnd();
77+
writer.writeEndTag(writer.constructName("testelement"));
78+
writer.close(false);
79+
80+
}
81+
5782
}

0 commit comments

Comments
 (0)