Utility class for URL encoding. : Utilities « Network Protocol « Java






Utility class for URL encoding.

      
/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2002 - 2007 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated
 * and its suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 **************************************************************************/
//package flex.messaging.util;

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

/**
 * Utility class for URL encoding.
 * 
 * @exclude
 */
public final class URLEncoder {
  public static String charset = "UTF8";

  private URLEncoder() {
  }

  public static String encode(String s) {
    try {
      return encode(s, charset);
    } catch (UnsupportedEncodingException ex) {
      throw new IllegalArgumentException(charset);
    }
  }

  public static String encode(String s, String enc)
      throws UnsupportedEncodingException {
    if (!needsEncoding(s)) {
      return s;
    }

    int length = s.length();

    StringBuffer out = new StringBuffer(length);

    ByteArrayOutputStream buf = new ByteArrayOutputStream(10); // why 10?
                                  // w3c says
                                  // so.

    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(buf,
        enc));

    for (int i = 0; i < length; i++) {
      int c = (int) s.charAt(i);
      if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0'
          && c <= '9' || c == ' ') {
        if (c == ' ') {
          c = '+';
        }

        toHex(out, buf.toByteArray());
        buf.reset();

        out.append((char) c);
      } else {
        try {
          writer.write(c);

          if (c >= 0xD800 && c <= 0xDBFF && i < length - 1) {
            int d = (int) s.charAt(i + 1);
            if (d >= 0xDC00 && d <= 0xDFFF) {
              writer.write(d);
              i++;
            }
          }

          writer.flush();
        } catch (IOException ex) {
          throw new IllegalArgumentException(s);
        }
      }
    }
    try {
      writer.close();
    } catch (IOException ioe) {
      // Ignore exceptions on close.
    }

    toHex(out, buf.toByteArray());

    return out.toString();
  }

  private static void toHex(StringBuffer buffer, byte[] b) {
    for (int i = 0; i < b.length; i++) {
      buffer.append('%');

      char ch = Character.forDigit((b[i] >> 4) & 0xF, 16);
      if (Character.isLetter(ch)) {
        ch -= 32;
      }
      buffer.append(ch);

      ch = Character.forDigit(b[i] & 0xF, 16);
      if (Character.isLetter(ch)) {
        ch -= 32;
      }
      buffer.append(ch);
    }
  }

  private static boolean needsEncoding(String s) {
    if (s == null)
      return false;

    for (int i = 0; i < s.length(); i++) {
      int c = (int) s.charAt(i);
      if (!(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0'
          && c <= '9'))
        return true;
      // Otherwise, keep going
    }

    return false;
  }
}

   
    
    
    
    
    
  








Related examples in the same category

1.A class that encodes URL parameter values for MIDP devices.
2.Get the listing of everyone logged on
3.Scan your computer for ports in useScan your computer for ports in use
4.Using the URL Class (GetURL.java)
5.TCP socket monitor
6.Create Socket helper
7.Implements a TCP/IP bounce utility (proxy)
8.URL utilities class that makes it easy to create new URLs based off of old URLs without having to assemble or parse them yourself
9.Download from URL
10.URL ParserURL Parser
11.A simple class to load an URL in a browser
12.Using the java API URL class, extract the http/https hostname.
13.Utility class for URL decoding.
14.Allows easy downloading of files from HTTP sites
15.enum Http Status