Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ Project: Stax2 API
= Releases
------------------------------------------------------------------------

4.3.2 (not yet released)
4.3.3 (not yet released)

#58: `TextBuffer` switch to `StringBuilder` for better performance
(contributed by @winfriedgerlach)

4.3.2 (2026-10-09)

#51: Fast path for BigInteger decoding
(contributed by @winfriedgerlach)
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/org/codehaus/stax2/ri/Stax2Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ public final static class TextBuffer
{
private String mText = null;

/* !!! JDK 1.5: when we can upgrade to Java 5, can convert
* to using <code>StringBuilder</code> instead.
*/
private StringBuffer mBuilder = null;
private StringBuilder mBuilder = null;

public TextBuffer() { }

Expand All @@ -128,7 +125,7 @@ public void append(String text)
if (len > 0) {
// Any prior text?
if (mText != null) {
mBuilder = new StringBuffer(mText.length() + len);
mBuilder = new StringBuilder(mText.length() + len);
mBuilder.append(mText);
mText = null;
}
Expand Down
69 changes: 69 additions & 0 deletions src/test/java/org/codehaus/stax2/ri/TextBufferTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.codehaus.stax2.ri;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Tests for {@link Stax2Util.TextBuffer}, which the DOM-backed reader uses
* to combine text of adjacent nodes: a single segment is kept as is, more
* are combined in a builder.
*/
class TextBufferTest
{
@Test
void empty()
{
Stax2Util.TextBuffer tb = new Stax2Util.TextBuffer();
assertTrue(tb.isEmpty());
assertEquals("", tb.get());

// Empty segments are ignored
tb.append("");
assertTrue(tb.isEmpty());
assertEquals("", tb.get());
}

@Test
void singleSegmentReturnedAsIs()
{
Stax2Util.TextBuffer tb = new Stax2Util.TextBuffer();
String text = "abc";
tb.append(text);
tb.append("");
assertFalse(tb.isEmpty());
assertSame(text, tb.get());
}

@Test
void combinesSegments()
{
Stax2Util.TextBuffer tb = new Stax2Util.TextBuffer();
tb.append("ab");
tb.append("");
tb.append("cde");
tb.append("f");
assertFalse(tb.isEmpty());
assertEquals("abcdef", tb.get());
// get() does not consume
assertEquals("abcdef", tb.get());
}

@Test
void reusableAfterReset()
{
Stax2Util.TextBuffer tb = new Stax2Util.TextBuffer();
tb.append("ab");
tb.append("cd");
tb.reset();
assertTrue(tb.isEmpty());
assertEquals("", tb.get());

tb.append("x");
tb.append("y");
assertEquals("xy", tb.get());
}
}
Loading