在Kjava中书写URLEncode源码

本文介绍了一个简单的URL编码器实现,该编码器能够将指定字符串转换为URL编码格式,并支持自定义编码方式。通过分析源码,读者可以了解如何处理特殊字符及多字节字符编码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * URL Encode 类.
 *
 * @author Fox
 * @version 0.1
 */

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

public class URLEncoder {
  private static boolean[] dontNeedEncoding;
  private static String defaultEncName = "";
  static final int caseDiff = ('a' - 'A');
  static {
    dontNeedEncoding = new boolean[256];
    int i;
    for (i = 'a'; i <= 'z'; i++) {
      dontNeedEncoding[i] = true;
    }
    for (i = 'A'; i <= 'Z'; i++) {
      dontNeedEncoding[i] = true;
    }
    for (i = '0'; i <= '9'; i++) {
      dontNeedEncoding[i] = true;
    }
    dontNeedEncoding[' '] = true; 
    dontNeedEncoding['-'] = true;
    dontNeedEncoding['_'] = true;
    dontNeedEncoding['.'] = true;
    dontNeedEncoding['*'] = true;
    defaultEncName = System.getProperty("microedition.encoding");
    if (defaultEncName == null || defaultEncName.trim().length() == 0) {
      defaultEncName = "UTF-8";
    }
    defaultEncName = "UTF-8";
  }

  public static final int MIN_RADIX = 2;
  public static final int MAX_RADIX = 36;

  private URLEncoder() {
  }

  public static String encode(String s) {
    String str = null;
    str = encode(s, defaultEncName);
    return str;
  }

  public static String encode(String s, String enc) {

    boolean needToChange = false;
    boolean wroteUnencodedChar = false;
    int maxBytesPerChar = 10;
    StringBuffer out = new StringBuffer(s.length());
    ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar);
    OutputStreamWriter writer = null;
    try {
      writer = new OutputStreamWriter(buf, enc);
    }
    catch (UnsupportedEncodingException ex) {
      try {
        writer = new OutputStreamWriter(buf, defaultEncName);
      }
      catch (UnsupportedEncodingException e) {
      }
    }

    for (int i = 0; i < s.length(); i++) {
      int c = (int) s.charAt(i);
      //System.out.println("Examining character: " + c);
      if (c < 256 && dontNeedEncoding[c]) {
        if (c == ' ') {
          c = '+';
          needToChange = true;
        }
        //System.out.println("Storing: " + c);
        out.append( (char) c);
        wroteUnencodedChar = true;
      }
      else {
        try {
          if (wroteUnencodedChar) { // Fix for 4407610
            writer = new OutputStreamWriter(buf, enc);
            wroteUnencodedChar = false;
          }
          if (writer != null)
            writer.write(c);

          if (c >= 0xD800 && c <= 0xDBFF) {
            if ( (i + 1) < s.length()) {
              int d = (int) s.charAt(i + 1);
              if (d >= 0xDC00 && d <= 0xDFFF) {
                writer.write(d);
                i++;
              }
            }
          }
          writer.flush();
        }
        catch (IOException e) {
          buf.reset();
          continue;
        }
        byte[] ba = buf.toByteArray();
        for (int j = 0; j < ba.length; j++) {
          out.append('%');
          char ch = forDigit( (ba[j] >> 4) & 0xF, 16);
          if (isLetter(ch)) {
            ch -= caseDiff;
          }
          out.append(ch);

          ch = forDigit( (ba[j] & 0xF), 16);
          //ch = forDigit(ba[j] & 0xF, 16);
          if (isLetter(ch)) {
            ch -= caseDiff;
          }
          out.append(ch);
        }
        buf.reset();
        needToChange = true;
      }
    }

    return (needToChange ? out.toString() : s);
  }

  private static boolean isLetter(char c) {
    if ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
      return true;
    return false;
  }

  private static char forDigit(int digit, int radix) {
    if ( (digit >= radix) || (digit < 0)) {
      return '/0';
    }
    if ( (radix < MIN_RADIX) || (radix > MAX_RADIX)) {
      return '/0';
    }
    if (digit < 10) {
      return (char) ('0' + digit);
    }
    return (char) ('a' - 10 + digit);
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值