pnicolucci closed pull request #10: MYFACES-4244:Use StringBuilder rather than 
calling write multiple times
URL: https://github.com/apache/myfaces/pull/10
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/impl/src/main/java/org/apache/myfaces/util/CDataEndEscapeFilterWriter.java 
b/impl/src/main/java/org/apache/myfaces/util/CDataEndEscapeFilterWriter.java
index 155d17882..63c3cc55c 100644
--- a/impl/src/main/java/org/apache/myfaces/util/CDataEndEscapeFilterWriter.java
+++ b/impl/src/main/java/org/apache/myfaces/util/CDataEndEscapeFilterWriter.java
@@ -59,22 +59,35 @@ public void write(int c) throws IOException
     public void write(char[] cbuf, int off, int len) throws IOException
     {
         int index = off;
+        StringBuilder sb = null;
         for (int i = 0; i < len; i++)
         {
             char c = cbuf[off+i];
             if (c1 == ']' && c2 == ']' && c == '>')
             {
-                super.write(cbuf, index, i+1 - ( index - off ) ); 
+                if (sb == null)
+                {
+                    sb = new StringBuilder(len + 16);
+                }
+                sb.append(cbuf, index, i+1 - ( index - off ));
                 index = off+i+1;
-                out.write("<![CDATA[]]]]><![CDATA[>");
+                sb.append("<![CDATA[]]]]><![CDATA[>");
             }
             c1 = c2;
-            c2 = (char) cbuf[off+i];
+            c2 = c;
             pos++;
         }
-        if (index < off+len)
+        if (sb != null)
+        {
+            if (index < off+len)
+            {
+                sb.append(cbuf, index, off+len-index);
+            }
+            out.write(sb.toString());
+        }
+        else
         {
-            super.write(cbuf, index, off+len-index);
+            out.write(cbuf, off, len);
         }
     }
 
@@ -82,22 +95,35 @@ public void write(char[] cbuf, int off, int len) throws 
IOException
     public void write(String str, int off, int len) throws IOException
     {
         int index = off;
+        StringBuilder sb = null;
         for (int i = 0; i < len; i++)
         {
             char c = str.charAt(off+i);
             if (c1 == ']' && c2 == ']' && c == '>')
             {
-                super.write(str, index, i+1 - ( index - off ) );
+                if (sb == null)
+                {
+                    sb = new StringBuilder(len + 16);
+                }
+                sb.append(str, index, off+i+1);
                 index = off+i+1;
-                out.write("<![CDATA[]]]]><![CDATA[>");
+                sb.append("<![CDATA[]]]]><![CDATA[>");
             }
             c1 = c2;
-            c2 = (char) str.charAt(off+i);
+            c2 = c;
             pos++;
         }
-        if (index < off+len)
+        if (sb != null)
+        {
+            if (index < off+len)
+            {
+                sb.append(str, index, off+len);
+            }
+            out.write(sb.toString());
+        }
+        else
         {
-            super.write(str, index, off+len-index);
+            out.write(str, off, len);
         }
     }
 }
diff --git 
a/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImpl.java
 
b/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImpl.java
index e60eabd32..02cff0f19 100644
--- 
a/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImpl.java
+++ 
b/shared/src/main/java/org/apache/myfaces/shared/renderkit/html/HtmlResponseWriterImpl.java
@@ -320,7 +320,6 @@ public void startElement(String name, UIComponent 
uiComponent) throws IOExceptio
         }
 
         closeStartTagIfNecessary();
-        _currentWriter.write('<');
 
         resetStartedElement();
 
@@ -330,6 +329,8 @@ public void startElement(String name, UIComponent 
uiComponent) throws IOExceptio
         _passThroughAttributesMap = (_startElementUIComponent != null) ?
             _startElementUIComponent.getPassThroughAttributes(false) : null;
 
+        String startElementNameToWrite = name;    
+
         if (_passThroughAttributesMap != null)
         {
             Object value = _passThroughAttributesMap.get(
@@ -349,17 +350,15 @@ public void startElement(String name, UIComponent 
uiComponent) throws IOExceptio
                     _startedChangedElements.add(elementName);
                     _startedElementsCount.add(0);
                 }
-                _currentWriter.write((String) elementName);
-            }
-            else
-            {
-                _currentWriter.write(name);
+                startElementNameToWrite = elementName;
             }
         }
-        else
-        {
-            _currentWriter.write(name);
-        }
+
+        StringBuilder sb = new StringBuilder(startElementNameToWrite.length() 
+ 1);
+        sb.append('<');
+        sb.append(startElementNameToWrite);
+
+        _currentWriter.write(sb.toString());
 
         if (!_startedElementsCount.isEmpty())
         {
@@ -805,9 +804,11 @@ else if (isStyle(name))
             _isStyle = false;
         }
 
-        _currentWriter.write("</");
-        _currentWriter.write(name);
-        _currentWriter.write('>');
+        StringBuilder sb = new StringBuilder(name.length() + 3);
+        sb.append("</");
+        sb.append(name);
+        sb.append('>');
+        _currentWriter.write(sb.toString());
     }
 
     public void writeAttribute(String name, Object value, String 
componentPropertyName) throws IOException
@@ -833,34 +834,42 @@ public void writeAttribute(String name, Object value, 
String componentPropertyNa
             if (((Boolean)value).booleanValue())
             {
                 // name as value for XHTML compatibility
-                _currentWriter.write(' ');
-                _currentWriter.write(name);
-                _currentWriter.write("=\"");
-                _currentWriter.write(name);
-                _currentWriter.write('"');
+                StringBuilder sb = new StringBuilder(name.length() * 2 + 4);
+                sb.append(' ');
+                sb.append(name);
+                sb.append("=\"");
+                sb.append(name);
+                sb.append('"');
+                _currentWriter.write(sb.toString());
             }
         }
         else
         {
             String strValue = (value==null)?"":value.toString();
-            _currentWriter.write(' ');
-            _currentWriter.write(name);
-            _currentWriter.write("=\"");
-            
org.apache.myfaces.shared.renderkit.html.util.HTMLEncoder.encode(_currentWriter,
+            String encodedStr = 
org.apache.myfaces.shared.renderkit.html.util.HTMLEncoder.encode(
                     strValue, false, false, !_isUTF8);
-            _currentWriter.write('"');
+            StringBuilder sb = new StringBuilder(name.length() + 
encodedStr.length() + 4);
+            sb.append(' ');
+            sb.append(name);
+            sb.append("=\"");
+            sb.append(encodedStr);
+            sb.append('"');
+            _currentWriter.write(sb.toString());
         }
     }
     
     private void encodeAndWriteAttribute(String name, Object value) throws 
IOException
     {
         String strValue = (value==null)?"":value.toString();
-        _currentWriter.write(' ');
-        _currentWriter.write(name);
-        _currentWriter.write("=\"");
-        
org.apache.myfaces.shared.renderkit.html.util.HTMLEncoder.encode(_currentWriter,
+        String encodedStr = 
org.apache.myfaces.shared.renderkit.html.util.HTMLEncoder.encode(
                 strValue, false, false, !_isUTF8);
-        _currentWriter.write('"');
+        StringBuilder sb = new StringBuilder(name.length() + 
encodedStr.length() + 4);
+        sb.append(' ');
+        sb.append(name);
+        sb.append("=\"");
+        sb.append(encodedStr);
+        sb.append('"');
+        _currentWriter.write(sb.toString());
     }
 
     public void writeURIAttribute(String name, Object value, String 
componentPropertyName) throws IOException
@@ -887,12 +896,10 @@ public void writeURIAttribute(String name, Object value, 
String componentPropert
     private void encodeAndWriteURIAttribute(String name, Object value) throws 
IOException
     {
         String strValue = value.toString();
-        _currentWriter.write(' ');
-        _currentWriter.write(name);
-        _currentWriter.write("=\"");
+        String encodedStr;
         if (strValue.toLowerCase().startsWith("javascript:"))
         {
-            
org.apache.myfaces.shared.renderkit.html.util.HTMLEncoder.encode(_currentWriter,
+            encodedStr = 
org.apache.myfaces.shared.renderkit.html.util.HTMLEncoder.encode(
                     strValue, false, false, !_isUTF8);
         }
         else
@@ -925,10 +932,16 @@ private void encodeAndWriteURIAttribute(String name, 
Object value) throws IOExce
             }
             */
             //_writer.write(strValue);
-            
org.apache.myfaces.shared.renderkit.html.util.HTMLEncoder.encodeURIAttribute(_currentWriter,
+            encodedStr = 
org.apache.myfaces.shared.renderkit.html.util.HTMLEncoder.encodeURIAttribute(
                             strValue, _characterEncoding);
         }
-        _currentWriter.write('"');
+        StringBuilder sb = new StringBuilder(name.length() + 
encodedStr.length() + 4);
+        sb.append(' ');
+        sb.append(name);
+        sb.append("=\"");
+        sb.append(encodedStr);
+        sb.append('"');
+        _currentWriter.write(sb.toString());
     }
 
     public void writeComment(Object value) throws IOException
@@ -939,9 +952,12 @@ public void writeComment(Object value) throws IOException
         }
 
         closeStartTagIfNecessary();
-        _currentWriter.write("<!--");
-        _currentWriter.write(value.toString());    //TODO: Escaping: must not 
have "-->" inside!
-        _currentWriter.write("-->");
+        String strValue = value.toString();
+        StringBuilder sb = new StringBuilder(strValue.length() + 7);
+        sb.append("<!--");
+        sb.append(strValue);    //TODO: Escaping: must not have "-->" inside!
+        sb.append("-->");
+        _currentWriter.write(sb.toString());
     }
 
     public void writeText(Object value, String componentPropertyName) throws 
IOException


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to