Another Urlencoder和LumaQQ的ec工程

lumaQQ编译起来的麻烦还是很多的,弄了2个小时才搞定。
https://penguinz.googlecode.com/svn/trunk/LumaQQ/lumaQQ
ec3.4,用subeclipse checkout出来。

另外附赠一个比较不错的urlencode
  1. package com.j2medev.httpme.tools;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.OutputStreamWriter;
  5. import java.io.UnsupportedEncodingException;
  6. /**
  7.  * Utility class for  form encoding.this class is modified form java.net.URLEncoder so that it can work well in cldc env.
  8.  * This class contains static methods
  9.  * for converting a String to the <CODE>application/x-www-form-urlencoded</CODE> MIME
  10.  * format. For more information about HTML form encoding, consult the HTML 
  11.  * <A HREF="http://www.w3.org/TR/html4/">specification</A>. 
  12.  *
  13.  * <p>
  14.  * When encoding a String, the following rules apply:
  15.  *
  16.  * <p>
  17.  * <ul>
  18.  * <li>The alphanumeric characters "<code>a</code>" through
  19.  *     "<code>z</code>", "<code>A</code>" through
  20.  *     "<code>Z</code>" and "<code>0</code>" 
  21.  *     through "<code>9</code>" remain the same.
  22.  * <li>The special characters "<code>.</code>",
  23.  *     "<code>-</code>", "<code>*</code>", and
  24.  *     "<code>_</code>" remain the same. 
  25.  * <li>The space character "<code> </code>" is
  26.  *     converted into a plus sign "<code>+</code>".
  27.  * <li>All other characters are unsafe and are first converted into
  28.  *     one or more bytes using some encoding scheme. Then each byte is
  29.  *     represented by the 3-character string
  30.  *     "<code>%<i>xy</i></code>", where <i>xy</i> is the
  31.  *     two-digit hexadecimal representation of the byte. 
  32.  *     The recommended encoding scheme to use is UTF-8. However, 
  33.  *     for compatibility reasons, if an encoding is not specified, 
  34.  *     then the default encoding of the platform is used.
  35.  * </ul>
  36.  *
  37.  * <p>
  38.  * For example using UTF-8 as the encoding scheme the string "The
  39.  * string ü@foo-bar" would get converted to
  40.  * "The+string+%C3%BC%40foo-bar" because in UTF-8 the character
  41.  * ü is encoded as two bytes C3 (hex) and BC (hex), and the
  42.  * character @ is encoded as one byte 40 (hex).
  43.  *
  44.  * @author  mingjava
  45.  * @version 0.1 05/06/2006
  46.  * @since   httpme 0.1
  47.  */
  48. public class URLEncoder {
  49.     
  50.     /** The characters which do not need to be encoded. */
  51.     private static boolean[] dontNeedEncoding;
  52.     private static String defaultEncName = "";
  53.     static final int caseDiff = ('a' - 'A');
  54.     static {
  55.         dontNeedEncoding = new boolean[256];
  56.         int i;
  57.         for (i = 'a'; i <= 'z'; i++) {
  58.             dontNeedEncoding[i] = true;
  59.         }
  60.         for (i = 'A'; i <= 'Z'; i++) {
  61.             dontNeedEncoding[i] = true;
  62.         }
  63.         for (i = '0'; i <= '9'; i++) {
  64.             dontNeedEncoding[i] = true;
  65.         }
  66.         dontNeedEncoding[' '] = true// encoding a space to a + is done in the encode() method
  67.         dontNeedEncoding['-'] = true;
  68.         dontNeedEncoding['_'] = true;
  69.         dontNeedEncoding['.'] = true;
  70.         dontNeedEncoding['*'] = true;
  71.         defaultEncName = System.getProperty("microedition.encoding");
  72.         if(defaultEncName == null || defaultEncName.trim().length() == 0){
  73.             defaultEncName = "UTF-8";
  74.         }
  75.     }
  76.     
  77.     public static final int MIN_RADIX = 2;
  78.     
  79.     /**
  80.      * The maximum radix available for conversion to and from strings.
  81.      */
  82.     public static final int MAX_RADIX = 36;
  83.     /**
  84.      * The class is not meant to be instantiated.
  85.      */
  86.     private URLEncoder() { }
  87.     
  88.     
  89.     /**
  90.      * Translates a string into "<CODE>x-www-form-urlencoded</CODE>"
  91.      * format.This method uses the platform's default encoding
  92.      * as the encoding scheme to obtain the bytes for unsafe characters.
  93.      *
  94.      * @param  s the string to be translated.
  95.      *
  96.      * @return The resulting string.
  97.      */
  98.     public static String encode(String s) {
  99.         String str = null;
  100.         str = encode(s, defaultEncName);
  101.         return str;
  102.     }
  103.        /**
  104.      * Translates a string into <code>application/x-www-form-urlencoded</code>
  105.      * format using a specific encoding scheme. This method uses the
  106.      * supplied encoding scheme to obtain the bytes for unsafe
  107.      * characters.
  108.      * <p>
  109.      * <em><strong>Note:</strong> The <a href=
  110.      * "http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars">
  111.      * World Wide Web Consortium Recommendation</a> states that
  112.      * UTF-8 should be used. Not doing so may introduce
  113.      * incompatibilites.</em>
  114.      *
  115.      * @param   s   <code>String</code> to be translated.
  116.      * @param   enc   The name of a supported character encoding such as UTF-8
  117.      * @return  the translated <code>String</code>.
  118.      */
  119.     public static String encode(String s, String enc) {
  120.         
  121.         boolean needToChange = false;
  122.         boolean wroteUnencodedChar = false;
  123.         int maxBytesPerChar = 10// rather arbitrary limit, but safe for now
  124.         StringBuffer out = new StringBuffer(s.length());
  125.         ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar);
  126.         OutputStreamWriter writer = null;
  127.         try {
  128.             writer = new OutputStreamWriter(buf, enc);
  129.         } catch (UnsupportedEncodingException ex) {
  130.             try {
  131.                 writer = new OutputStreamWriter(buf,defaultEncName);
  132.             } catch (UnsupportedEncodingException e) {
  133.                 //never reach
  134.             }
  135.         }
  136.         
  137.         for (int i = 0; i < s.length(); i++) {
  138.             int c = (int) s.charAt(i);
  139.             //System.out.println("Examining character: " + c);
  140.             if (c <256 && dontNeedEncoding[c]) {
  141.                 if (c == ' ') {
  142.                     c = '+';
  143.                     needToChange = true;
  144.                 }
  145.                 //System.out.println("Storing: " + c);
  146.                 out.append((char)c);
  147.                 wroteUnencodedChar = true;
  148.             } else {
  149.                 // convert to external encoding before hex conversion
  150.                 try {
  151.                     if (wroteUnencodedChar) { // Fix for 4407610
  152.                         writer = new OutputStreamWriter(buf, enc);
  153.                         wroteUnencodedChar = false;
  154.                     }
  155.                     if(writer != null)
  156.                         writer.write(c);
  157.                     /*
  158.                      * If this character represents the start of a Unicode
  159.                      * surrogate pair, then pass in two characters. It's not
  160.                      * clear what should be done if a bytes reserved in the
  161.                      * surrogate pairs range occurs outside of a legal
  162.                      * surrogate pair. For now, just treat it as if it were
  163.                      * any other character.
  164.                      */
  165.                     if (c >= 0xD800 && c <= 0xDBFF) {
  166.                         /*
  167.                           System.out.println(Integer.toHexString(c)
  168.                           + " is high surrogate");
  169.                          */
  170.                         if ( (i+1) < s.length()) {
  171.                             int d = (int) s.charAt(i+1);
  172.                             /*
  173.                               System.out.println("/tExamining "
  174.                               + Integer.toHexString(d));
  175.                              */
  176.                             if (d >= 0xDC00 && d <= 0xDFFF) {
  177.                                 /*
  178.                                   System.out.println("/t"
  179.                                   + Integer.toHexString(d)
  180.                                   + " is low surrogate");
  181.                                  */
  182.                                 writer.write(d);
  183.                                 i++;
  184.                             }
  185.                         }
  186.                     }
  187.                     writer.flush();
  188.                 } catch(IOException e) {
  189.                     buf.reset();
  190.                     continue;
  191.                 }
  192.                 byte[] ba = buf.toByteArray();
  193.                 for (int j = 0; j < ba.length; j++) {
  194.                     out.append('%');
  195.                     char ch = forDigit((ba[j] >> 4) & 0xF16);
  196.                     if (isLetter(ch)) {
  197.                         ch -= caseDiff;
  198.                  }
  199.                     out.append(ch);
  200.                    
  201.                   ch = forDigit((ba[j] & 0xF), 16);
  202.                    //ch = forDigit(ba[j] & 0xF, 16);
  203.                     if (isLetter(ch)) {
  204.                         ch -= caseDiff;
  205.                     }
  206.                     out.append(ch);
  207.                 }
  208.                 buf.reset();
  209.                 needToChange = true;
  210.             }
  211.         }
  212.         
  213.         return (needToChange? out.toString() : s);
  214.     }
  215.     
  216.     private static boolean isLetter(char c){
  217.         if( (c >= 'a' && c <= 'z') || (c >='A' && c <= 'Z'))
  218.             return true;
  219.         return false;
  220.     }
  221.     
  222.     private static char forDigit(int digit,int radix){
  223.         if ((digit >= radix) || (digit < 0)) {
  224.             return '/0';        }
  225.         if ((radix < MIN_RADIX) || (radix > MAX_RADIX)) {
  226.             return '/0';
  227.         }
  228.         if (digit < 10) {
  229.             return (char)('0' + digit);
  230.         }
  231.         return (char)('a' - 10 + digit);
  232.     }
  233. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值