This is an automated email from the ASF dual-hosted git repository.

veithen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ws-axiom.git


The following commit(s) were added to refs/heads/master by this push:
     new 5699151f7 Avoid eager String allocation in XmlHandlerContentHandler 
via CharacterData
5699151f7 is described below

commit 5699151f71381e2fec4cb86622fc6bfaf3885fd6
Author: Copilot <[email protected]>
AuthorDate: Tue May 26 20:01:18 2026 +0100

    Avoid eager String allocation in XmlHandlerContentHandler via CharacterData
    
    Co-authored-by: Andreas Veithen-Knowles <[email protected]>
---
 .../axiom/core/stream/CharArrayCharacterData.java  | 71 ++++++++++++++++++++++
 .../stream/sax/input/XmlHandlerContentHandler.java | 13 +++-
 2 files changed, 81 insertions(+), 3 deletions(-)

diff --git 
a/components/core-streams/src/main/java/org/apache/axiom/core/stream/CharArrayCharacterData.java
 
b/components/core-streams/src/main/java/org/apache/axiom/core/stream/CharArrayCharacterData.java
new file mode 100644
index 000000000..4ab07fe60
--- /dev/null
+++ 
b/components/core-streams/src/main/java/org/apache/axiom/core/stream/CharArrayCharacterData.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.core.stream;
+
+import java.io.IOException;
+
+/**
+ * A reusable {@link CharacterData} implementation backed by a {@code char[]} 
slice. Instances are
+ * only valid for the duration of the {@link XmlHandler#processCharacterData} 
invocation in which
+ * they are passed.
+ */
+public final class CharArrayCharacterData implements CharacterData {
+    private char[] ch;
+    private int start;
+    private int length;
+
+    /**
+     * Updates the backing char array slice. Must be called before each {@link
+     * XmlHandler#processCharacterData} invocation.
+     */
+    public void set(char[] ch, int start, int length) {
+        this.ch = ch;
+        this.start = start;
+        this.length = length;
+    }
+
+    /**
+     * Invalidates this instance by clearing the backing array reference. Any 
subsequent attempt to
+     * use this instance will result in a {@link NullPointerException}, making 
it easier to detect
+     * bugs where an {@link XmlHandler} retains a reference beyond the allowed 
lifetime.
+     */
+    public void invalidate() {
+        this.ch = null;
+    }
+
+    @Override
+    public String toString() {
+        return new String(ch, start, length);
+    }
+
+    @Override
+    public void writeTo(CharacterDataSink sink) throws IOException {
+        sink.getWriter().write(ch, start, length);
+    }
+
+    @Override
+    public void appendTo(StringBuilder buffer) {
+        buffer.append(ch, start, length);
+    }
+
+    @Override
+    public Object retain() {
+        return new String(ch, start, length);
+    }
+}
diff --git 
a/components/core-streams/src/main/java/org/apache/axiom/core/stream/sax/input/XmlHandlerContentHandler.java
 
b/components/core-streams/src/main/java/org/apache/axiom/core/stream/sax/input/XmlHandlerContentHandler.java
index 268a8b2fd..48875e5ce 100644
--- 
a/components/core-streams/src/main/java/org/apache/axiom/core/stream/sax/input/XmlHandlerContentHandler.java
+++ 
b/components/core-streams/src/main/java/org/apache/axiom/core/stream/sax/input/XmlHandlerContentHandler.java
@@ -21,6 +21,7 @@ package org.apache.axiom.core.stream.sax.input;
 import java.io.StringWriter;
 import java.util.HashMap;
 import java.util.Map;
+import org.apache.axiom.core.stream.CharArrayCharacterData;
 import org.apache.axiom.core.stream.StreamException;
 import org.apache.axiom.core.stream.XmlHandler;
 import org.apache.axiom.core.stream.serializer.Serializer;
@@ -70,6 +71,8 @@ public final class XmlHandlerContentHandler implements 
ContentHandler, LexicalHa
     private boolean inEntityReference;
     private int entityReferenceDepth;
 
+    private final CharArrayCharacterData charData = new 
CharArrayCharacterData();
+
     public XmlHandlerContentHandler(XmlHandler handler, boolean 
expandEntityReferences) {
         this.handler = handler;
         this.expandEntityReferences = expandEntityReferences;
@@ -103,6 +106,7 @@ public final class XmlHandlerContentHandler implements 
ContentHandler, LexicalHa
         } catch (StreamException ex) {
             throw toSAXException(ex);
         }
+        charData.invalidate();
     }
 
     @Override
@@ -312,7 +316,8 @@ public final class XmlHandlerContentHandler implements 
ContentHandler, LexicalHa
     public void characters(char[] ch, int start, int length) throws 
SAXException {
         if (!inEntityReference) {
             try {
-                handler.processCharacterData(new String(ch, start, length), 
false);
+                charData.set(ch, start, length);
+                handler.processCharacterData(charData, false);
             } catch (StreamException ex) {
                 throw toSAXException(ex);
             }
@@ -323,7 +328,8 @@ public final class XmlHandlerContentHandler implements 
ContentHandler, LexicalHa
     public void ignorableWhitespace(char[] ch, int start, int length) throws 
SAXException {
         if (!inEntityReference) {
             try {
-                handler.processCharacterData(new String(ch, start, length), 
true);
+                charData.set(ch, start, length);
+                handler.processCharacterData(charData, true);
             } catch (StreamException ex) {
                 throw toSAXException(ex);
             }
@@ -348,7 +354,8 @@ public final class XmlHandlerContentHandler implements 
ContentHandler, LexicalHa
         if (!inEntityReference) {
             try {
                 handler.startComment();
-                handler.processCharacterData(new String(ch, start, length), 
false);
+                charData.set(ch, start, length);
+                handler.processCharacterData(charData, false);
                 handler.endComment();
             } catch (StreamException ex) {
                 throw toSAXException(ex);

Reply via email to