[jboss-cvs] jboss-portal/common/src/main/org/jboss/portal/common/net ...
Julien Viet
julien at jboss.com
Thu Jul 20 11:16:58 EDT 2006
User: julien
Date: 06/07/20 11:16:58
Added: common/src/main/org/jboss/portal/common/net
Application_XWWWFormURLEncodedFormat.java
Log:
added lookup based application/x-www-formurlencoded class for fast encoding
Revision Changes Path
1.1 date: 2006/07/20 15:16:58; author: julien; state: Exp;jboss-portal/common/src/main/org/jboss/portal/common/net/Application_XWWWFormURLEncodedFormat.java
Index: Application_XWWWFormURLEncodedFormat.java
===================================================================
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.portal.common.net;
import java.io.IOException;
/**
* Provide fast encoding to the application/x-www-form-urlencoded MIME format for a specified
* charset.
*
* @author <a href="mailto:julien at jboss.org">Julien Viet</a>
* @version $Revision: 1.1 $
*/
public class Application_XWWWFormURLEncodedFormat
{
public static final Application_XWWWFormURLEncodedFormat UTF_8 = safeCreate("UTF-8");
private static Application_XWWWFormURLEncodedFormat safeCreate(String charset)
{
try
{
return new Application_XWWWFormURLEncodedFormat(charset);
}
catch (IOException e)
{
throw new Error(e);
}
}
private final char[][] table;
public Application_XWWWFormURLEncodedFormat(String charset) throws IOException
{
StringBuffer sb = new StringBuffer();
table = new char[Character.MAX_VALUE + 1][0];
for (int i = 0; i <= Character.MAX_VALUE; i++)
{
char c = (char)i;
if (c >= 'a' && c <= 'z')
{
table[c] = new char[]{c};
}
else if (c >= 'A' && c <= 'Z')
{
table[c] = new char[]{c};
}
else if (c >= '0' && c <= '9')
{
table[c] = new char[]{c};
}
else if (c == '-' || c == '_' || c == '.' || c == '*')
{
table[c] = new char[]{c};
}
else if (c == ' ')
{
table[c] = new char[]{'+'};
}
else
{
String s = new String(new char[]{c});
byte[] bytes = s.getBytes(charset);
for (int j = 0; j < bytes.length; j++)
{
byte b = bytes[j];
int bh = (b >> 4) & 0xF;
int bl = b & 0xF;
char ch = Character.toUpperCase(Character.forDigit(bh, 16));
char cl = Character.toUpperCase(Character.forDigit(bl, 16));
sb.append('%').append(ch).append(cl);
}
table[c] = new char[sb.length()];
sb.getChars(0, sb.length(), table[c], 0);
sb.setLength(0);
}
}
}
public void encode(char c, StringBuffer sb)
{
sb.append(table[c]);
}
public void encode(String s, StringBuffer sb)
{
encode(s, 0, s.length(), sb);
}
public void encode(String s, int from, int to, StringBuffer sb)
{
for (int i = from; i < to; i++)
{
char c = s.charAt(i);
char[] chars = table[c];
sb.append(chars);
}
}
public String encode(char c)
{
return new String(table[c]);
}
public String encode(String s, int from, int to)
{
StringBuffer sb = new StringBuffer();
encode(s, from, to, sb);
return sb.toString();
}
public String encode(String s)
{
return encode(s, 0, s.length());
}
}
More information about the jboss-cvs-commits
mailing list