diff --git a/src/main/java/com/ctc/wstx/sr/BasicStreamReader.java b/src/main/java/com/ctc/wstx/sr/BasicStreamReader.java index 4ae8d9af..c28f5e2b 100644 --- a/src/main/java/com/ctc/wstx/sr/BasicStreamReader.java +++ b/src/main/java/com/ctc/wstx/sr/BasicStreamReader.java @@ -5317,58 +5317,54 @@ private int readAndWriteCData(Writer w) quick_loop: while (true) { - if (c > CHAR_CR_LF_OR_NULL) { + if (c >= CHAR_SPACE) { if (c == ']') { break quick_loop; } - } else { - if (c < CHAR_SPACE) { - if (c == '\n') { - markLF(); - } else if (c == '\r') { - char d; - if (mInputPtr >= mInputEnd) { - /* If we can't peek easily, let's flush past stuff - * and load more... (have to flush, since new read - * will overwrite inbut buffers) - */ - int len = mInputPtr - start; - if (len > 0) { - w.write(mInputBuffer, start, len); - count += len; - } - d = getNextChar(SUFFIX_IN_CDATA); - start = mInputPtr; // to mark 'no past content' - } else { - d = mInputBuffer[mInputPtr++]; - } - if (d == '\n') { - if (mNormalizeLFs) { - /* Let's flush content prior to 2-char LF, and - * start the new segment on the second char... - * this way, no mods are needed for the buffer, - * AND it'll also work on split 2-char lf! - */ - int len = mInputPtr - 2 - start; - if (len > 0) { - w.write(mInputBuffer, start, len); - count += len; - } - start = mInputPtr-1; // so '\n' is the first char - } else { - // otherwise it's good as is - } - } else { // not 2-char... need to replace? - --mInputPtr; - if (mNormalizeLFs) { - mInputBuffer[mInputPtr-1] = '\n'; - } + } else if (c == '\n') { + markLF(); + } else if (c == '\r') { + char d; + if (mInputPtr >= mInputEnd) { + /* If we can't peek easily, let's flush past stuff + * and load more... (have to flush, since new read + * will overwrite inbut buffers) + */ + int len = mInputPtr - start; + if (len > 0) { + w.write(mInputBuffer, start, len); + count += len; + } + d = getNextChar(SUFFIX_IN_CDATA); + start = mInputPtr; // to mark 'no past content' + } else { + d = mInputBuffer[mInputPtr++]; + } + if (d == '\n') { + if (mNormalizeLFs) { + /* Let's flush content prior to 2-char LF, and + * start the new segment on the second char... + * this way, no mods are needed for the buffer, + * AND it'll also work on split 2-char lf! + */ + int len = mInputPtr - 2 - start; + if (len > 0) { + w.write(mInputBuffer, start, len); + count += len; } - markLF(); - } else if (c != '\t') { - throwInvalidSpace(c); + start = mInputPtr-1; // so '\n' is the first char + } else { + // otherwise it's good as is + } + } else { // not 2-char... need to replace? + --mInputPtr; + if (mNormalizeLFs) { + mInputBuffer[mInputPtr-1] = '\n'; } } + markLF(); + } else if (c != '\t') { + throwInvalidSpace(c); } // Reached the end of buffer? Need to flush, then if (mInputPtr >= mInputEnd) { @@ -5386,9 +5382,8 @@ private int readAndWriteCData(Writer w) // Anything to flush once we hit ']'? { - /* -1 since the last char in there (a '[') is NOT to be - * output at this point - */ + // -1 since the last char in there (a '[') is NOT to be + // output at this point int len = mInputPtr - start - 1; if (len > 0) { w.write(mInputBuffer, start, len); diff --git a/src/test/java/wstxtest/stream/TestStreaming.java b/src/test/java/wstxtest/stream/TestStreaming.java index 2030c877..095078fb 100644 --- a/src/test/java/wstxtest/stream/TestStreaming.java +++ b/src/test/java/wstxtest/stream/TestStreaming.java @@ -65,30 +65,36 @@ public void testTextStreaming() public void testCDataStreaming() throws IOException, XMLStreamException { - String CONTENT_INOUT = - "Some content\nthat will be stored in a\n" - +"CDATA Block <[*]>\n" + // 05-Jan-2021, tatu: let's quickly verify LF-normalization too + // (changed for [woodstox-core#121] verification) + final String CONTENT_IN = + "Some content\r\nthat will be stored in a\n" + +"CDATA Block <[*]>\r" +" yet not be split in any way...." ; + + final String CONTENT_OUT = CONTENT_IN.replace("\r\n", "\n") + .replace("\r", "\n"); + /* Let's also add trailing text, to ensure no coalescing is done * when not requested */ - String XML = "some text!"; + String XML = "some text!"; XMLStreamReader2 sr = getReader(XML, false); assertTokenType(START_ELEMENT, sr.next()); assertTokenType(CDATA, sr.next()); StringWriter sw = new StringWriter(); sr.getText(sw, false); String act = sw.toString(); - if (!act.equals(CONTENT_INOUT)) { - if (CONTENT_INOUT.startsWith(act)) { + if (!act.equals(CONTENT_OUT)) { + if (CONTENT_OUT.startsWith(act)) { fail("Streaming text accessors returned partial match; first " +act.length()+" chars of the expected " - +CONTENT_INOUT.length()+" chars"); + +CONTENT_OUT.length()+" chars"); } fail("Content accessed using streaming text accessor (len " - +act.length()+"; exp "+CONTENT_INOUT.length()+" chars) wrong: " - +"expected ["+CONTENT_INOUT+"], got ["+act+"]"); + +act.length()+"; exp "+CONTENT_OUT.length()+" chars) wrong: " + +"expected ["+CONTENT_OUT+"], got ["+act+"]"); } // And should get the following CHARACTERS then: