// Collection of code snippets by Arne VajhÃ¸j 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.net.*;

public class EUTest {
   private final static int N = 100000;
   public static String myurlencoder(String s) {
      char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
      StringBuffer sb = new StringBuffer("");
      for (int i = 0; i < s.length(); i++) {
         char c = s.charAt(i);
         if (('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')) {
            sb.append(c);
         } else {
            sb.append('%');
            sb.append(hexdigit[(c >> 4) & 0x0F]);
            sb.append(hexdigit[c & 0x0F]);
         }
      }
      return sb.toString();
   }
   public static void main(String[] args) throws Exception {
      long t1 = System.currentTimeMillis();
      for(int i = 0; i < N; i++) {
         String s = myurlencoder("Dette er ÆØÅ");
      }
      long t2 = System.currentTimeMillis();
      System.out.println("home made : " + (t2-t1));
      long t3 = System.currentTimeMillis();
      for(int i = 0; i < N; i++) {
         String s = URLEncoder.encode("Dette er ÆØÅ", "ISO-8859-1");
      }
      long t4 = System.currentTimeMillis();
      System.out.println("builtin : " + (t4-t3));
      long t5 = System.currentTimeMillis();
      for(int i = 0; i < N; i++) {
         String s = URLEncoder.encode("Dette er ÆØÅ", "ISO-8859-1").replaceAll("\\+", "%20");
      }
      long t6 = System.currentTimeMillis();
      System.out.println("builtin with mod : " + (t6-t5));
   }
}
