myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tandrasc...@apache.org
Subject [myfaces] branch master updated: MYFACES-4266 optimized performance
Date Mon, 26 Nov 2018 10:35:30 GMT
This is an automated email from the ASF dual-hosted git repository.

tandraschko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/myfaces.git


The following commit(s) were added to refs/heads/master by this push:
     new bbd8e63  MYFACES-4266 optimized performance
bbd8e63 is described below

commit bbd8e63bae89a9f3f94efe11ccc61123dbcec294
Author: Thomas Andraschko <tandraschko@apache.org>
AuthorDate: Mon Nov 26 11:35:34 2018 +0100

    MYFACES-4266 optimized performance
---
 .../util/IllegalXmlCharacterFilterWriter.java      | 67 +++++++++++++++++-----
 1 file changed, 54 insertions(+), 13 deletions(-)

diff --git a/impl/src/main/java/org/apache/myfaces/util/IllegalXmlCharacterFilterWriter.java
b/impl/src/main/java/org/apache/myfaces/util/IllegalXmlCharacterFilterWriter.java
index f7011e3..f107988 100644
--- a/impl/src/main/java/org/apache/myfaces/util/IllegalXmlCharacterFilterWriter.java
+++ b/impl/src/main/java/org/apache/myfaces/util/IllegalXmlCharacterFilterWriter.java
@@ -31,6 +31,9 @@ import java.io.Writer;
  */
 public class IllegalXmlCharacterFilterWriter extends FilterWriter
 {
+    private static final char[] EMPTY_CHAR_ARRAY = new char[0];
+    private static final char BLANK_CHAR = ' ';
+    
     public IllegalXmlCharacterFilterWriter(Writer out)
     {
         super(out);
@@ -39,48 +42,86 @@ public class IllegalXmlCharacterFilterWriter extends FilterWriter
     @Override
     public void write(int c) throws IOException 
     {
-        super.write(xmlEncode((char) c));
+        if (isInvalidChar((char) c))
+        {
+            super.write((int) BLANK_CHAR);
+        }
+        else
+        {
+            super.write(c);
+        }
     }
 
     @Override
     public void write(char[] cbuf, int off, int len) throws IOException 
     {
-        super.write(xmlEncode(cbuf), off, len);
+        super.write(encodeCharArray(cbuf, off, len), off, len);
     }
 
     @Override
     public void write(String str, int off, int len) throws IOException 
     {
-        super.write(new String(xmlEncode(str.toCharArray())), off, len);
+        super.write(encodeString(str, off, len), off, len);
     }
 
-    private char[] xmlEncode(char[] ca)
+    private static String encodeString(String str, int off, int len)
+    {
+        boolean containsInvalidChar = false;
+        char[] encodedCharArray = EMPTY_CHAR_ARRAY;
+        
+        int to = off + len;
+        for (int i = off; i < to; i++)
+        {
+            if (isInvalidChar(str.charAt(i)))
+            {
+                if (!containsInvalidChar)
+                {
+                    containsInvalidChar = true;
+                    encodedCharArray = str.toCharArray();
+                }
+                encodedCharArray[i] = BLANK_CHAR;
+            }
+        }
+
+        if (containsInvalidChar)
+        {
+            return String.valueOf(encodedCharArray);
+        }
+
+        return str;
+    }
+    
+    private static char[] encodeCharArray(char[] cbuf, int off, int len)
     {
-        for (int i = 0; i < ca.length; i++)
+        int to = off + len;
+        for (int i = off; i < to; i++)
         {
-            ca[i] = xmlEncode(ca[i]);
+            if (isInvalidChar(cbuf[i]))
+            {
+                cbuf[i] = BLANK_CHAR;
+            }
         }
-        return ca;
+        return cbuf;
     }
 
-    private char xmlEncode(char c)
+    private static boolean isInvalidChar(char c)
     {
         if (Character.isSurrogate(c)) 
         {
-            return ' ';
+            return true;
         }
         if (c == '\u0009' || c == '\n' || c == '\r') 
         {
-            return c;
+            return false;
         }
         if (c > '\u0020' && c < '\uD7FF') 
         {
-            return c;
+            return false;
         }
         if (c > '\uE000' && c < '\uFFFD') 
         {
-            return c;
+            return false;
         }
-        return ' ';
+        return true;
     }
 }


Mime
View raw message