You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by jm...@apache.org on 2002/11/22 03:35:10 UTC
cvs commit: jakarta-turbine-3/src/java/org/apache/turbine DynamicURI.java
jmcnally 2002/11/21 18:35:10
Modified: src/java/org/apache/turbine DynamicURI.java
Added: src/test/org/apache/turbine DynamicURITest.java
Log:
delay conversion to bytes until it is determined that it is necessary. For
the common case IMO of not needing any encoding this is a considerable
advantage and it doesn't add that much overhead when encoding is required.
Revision Changes Path
1.1 jakarta-turbine-3/src/test/org/apache/turbine/DynamicURITest.java
Index: DynamicURITest.java
===================================================================
package org.apache.turbine;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Turbine" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Turbine", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.util.HashMap;
import java.util.Map;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Tests the DynamicURI class.
*
* @author <a href="mailto:jmcnally@apache.org">John McNally</a>
* @since 3.0
* @version $Id: DynamicURITest.java,v 1.1 2002/11/22 02:35:10 jmcnally Exp $
*/
public class DynamicURITest
extends TestCase
{
/**
* Creates a new instance.
*/
public DynamicURITest(String testName)
{
super(testName);
}
/**
* Return the Test
*/
public static Test suite()
{
return new TestSuite(DynamicURITest.class);
}
/**
* Setup the test.
*/
public void setUp()
{
// Nothing done here yet.
}
/**
* Tear down the test.
*/
public void tearDown()
{
// Nothing to do here yet.
}
/**
* Previous versions of DynamicURI immediately converted a pathinfo
* or query parameter to a byte array for url encoding. The method
* has been modified to only invoke the getBytes() method, if a
* character requires encoding. This test compares the results of
* the new method with the old.
*/
public void testFastEncoding()
{
char[] allchars = new char[65546];
allchars[0] = 's';
allchars[1] = 't';
allchars[2] = 'a';
allchars[3] = 'r';
allchars[4] = 't';
allchars[5] = ' ';
allchars[6] = 's';
allchars[7] = 'a';
allchars[8] = 'f';
allchars[9] = 'e';
for (int i=0; i<65536; i++)
{
allchars[i+10] = (char)i;
}
final String all = new String(allchars);
// get reference String
StringBuffer out = new StringBuffer();
DynamicURI.writeEncoded(all, out);
String reference = out.toString();
// compare to our changes
out = new StringBuffer();
DynamicURI.writeFastEncoded(all, out);
String s = out.toString();
assertEquals("Test of complete character set failed.", reference, s);
// colon is a special character
String in = ":colon as first char";
// get reference String
out = new StringBuffer();
DynamicURI.writeEncoded(in, out);
reference = out.toString();
// compare to our changes
out = new StringBuffer();
DynamicURI.writeFastEncoded(in, out);
s = out.toString();
assertEquals("Special character as first character failed.", reference, s);
// colon is a special character
in = "colon as last char:";
// get reference String
out = new StringBuffer();
DynamicURI.writeEncoded(in, out);
reference = out.toString();
// compare to our changes
out = new StringBuffer();
DynamicURI.writeFastEncoded(in, out);
s = out.toString();
assertEquals("Special character as last character failed.", reference, s);
// colon is a special character
in = "n0special_chars";
// get reference String
out = new StringBuffer();
DynamicURI.writeEncoded(in, out);
reference = out.toString();
// compare to our changes
out = new StringBuffer();
DynamicURI.writeFastEncoded(in, out);
s = out.toString();
assertEquals("String with no special character failed.", reference, s);
}
}
1.12 +49 -3 jakarta-turbine-3/src/java/org/apache/turbine/DynamicURI.java
Index: DynamicURI.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-3/src/java/org/apache/turbine/DynamicURI.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- DynamicURI.java 23 Jul 2002 18:08:22 -0000 1.11
+++ DynamicURI.java 22 Nov 2002 02:35:10 -0000 1.12
@@ -747,9 +747,9 @@
out.append(pairSep);
}
- writeEncoded((String) pair[0], out);
+ writeFastEncoded((String) pair[0], out);
out.append(keyValSep);
- writeEncoded((String) pair[1], out);
+ writeFastEncoded((String) pair[1], out);
}
}
@@ -1016,6 +1016,52 @@
output.append(request.getQueryString());
}
return output.toString();
+ }
+
+ /**
+ * URL encodes <code>in</code> and writes it to <code>out</code>. If the
+ * string is null, 'null' will be written.
+ * This method is faster if
+ * the string does not contain any characters needing encoding. It
+ * adds some penalty for strings which actually need to be encoded.
+ * for short strings ~20 characters the upside is a 75% decrease. while
+ * the penalty is a 10% increase. As many query parameters do not need
+ * encoding even in i18n applications it should be much better to
+ * delay the byte conversion.
+ *
+ * @param in String to write.
+ * @param out Buffer to write to.
+ */
+ protected static final void writeFastEncoded(String in, StringBuffer out)
+ {
+ if (in == null || in.length() == 0)
+ {
+ out.append("null");
+ return;
+ }
+
+ char[] chars = in.toCharArray();
+
+ for (int i = 0; i < chars.length; i++)
+ {
+ char c = chars[i];
+
+ if ( c < 128 && safe[ c ] )
+ {
+ out.append(c);
+ }
+ else if (c == ' ')
+ {
+ out.append('+');
+ }
+ else
+ {
+ // since we need to encode we will give up on
+ // doing it the fast way and convert to bytes.
+ writeEncoded(new String(chars, i, chars.length-i), out);
+ break;
+ }
+ }
}
/**
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>