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

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Network Protocol » Utilities 




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 = (ints.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((charc);
      else {
        try {
          writer.write(c);

          if (c >= 0xD800 && c <= 0xDBFF && i < length - 1) {
            int d = (ints.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>> 40xF16);
      if (Character.isLetter(ch)) {
        ch -= 32;
      }
      buffer.append(ch);

      ch = Character.forDigit(b[i0xF16);
      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 = (ints.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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.