001: /*
002: * @(#)URLEncoder.java 1.15 98/06/29
003: *
004: * Copyright 1995-1998 by Sun Microsystems, Inc.,
005: * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
006: * All rights reserved.
007: *
008: * This software is the confidential and proprietary information
009: * of Sun Microsystems, Inc. ("Confidential Information"). You
010: * shall not disclose such Confidential Information and shall use
011: * it only in accordance with the terms of the license agreement
012: * you entered into with Sun.
013: */
014:
015: //package java.net;
016: package com.sun.portal.desktop.util;
017:
018: import java.io.ByteArrayOutputStream;
019: import java.util.BitSet;
020:
021: /**
022: * The class contains a utility method for converting a
023: * <code>String</code> into a MIME format called
024: * "<code>x-www-form-urlencoded</code>" format.
025: * <p>
026: * To convert a <code>String</code>, each character is examined in turn:
027: * <ul>
028: * <li>The ASCII characters '<code>a</code>' through '<code>z</code>',
029: * '<code>A</code>' through '<code>Z</code>', and '<code>0</code>'
030: * through '<code>9</code>' remain the same.
031: * <li>The space character '<code> </code>' is converted into a
032: * plus sign '<code>+</code>'.
033: * <li>All other characters are converted into the 3-character string
034: * "<code>%<i>xy</i></code>", where <i>xy</i> is the two-digit
035: * hexadecimal representation of the lower 8-bits of the character.
036: * </ul>
037: *
038: * Fix for OutputStreamEncoding problem, code reworked to not use
039: * OutputStreamWriter by Tom Mueller, 7/24/01
040: *
041: * @author Herb Jellinek
042: * @version 1.15, 06/29/98
043: * @since JDK1.0
044: */
045:
046: public class IURLEncoder {
047: static BitSet dontNeedEncoding;
048: static final int caseDiff = ('a' - 'A');
049:
050: /* The list of characters that are not encoded have been determined by
051: referencing O'Reilly's "HTML: The Definitive Guide" (page 164). */
052:
053: static {
054: dontNeedEncoding = new BitSet(256);
055: int i;
056: for (i = 'a'; i <= 'z'; i++) {
057: dontNeedEncoding.set(i);
058: }
059: for (i = 'A'; i <= 'Z'; i++) {
060: dontNeedEncoding.set(i);
061: }
062: for (i = '0'; i <= '9'; i++) {
063: dontNeedEncoding.set(i);
064: }
065: dontNeedEncoding.set(' '); /* encoding a space to a + is done in the encode() method */
066: dontNeedEncoding.set('-');
067: dontNeedEncoding.set('_');
068: dontNeedEncoding.set('.');
069: dontNeedEncoding.set('*');
070: dontNeedEncoding.set('+');
071: }
072:
073: /**
074: * Translates a string into <code>x-www-form-urlencoded</code> format.
075: *
076: * @param s <code>String</code> to be translated.
077: * @return the translated <code>String</code>.
078: */
079:
080: public static String encode(String s) {
081: int maxBytesPerChar = 10;
082: StringBuffer out = new StringBuffer(s.length());
083: ByteArrayOutputStream buf = new ByteArrayOutputStream(
084: maxBytesPerChar);
085:
086: for (int i = 0; i < s.length(); i++) {
087: int c = (int) s.charAt(i);
088: if (dontNeedEncoding.get(c)) {
089: if (c == ' ') {
090: c = '+';
091: }
092: out.append((char) c);
093: } else {
094: int lowbyte = (c & 0xff);
095: out.append('%');
096: char ch = Character.forDigit((lowbyte >> 4) & 0xF, 16);
097: // converting to use uppercase letter as part of
098: // the hex value if ch is a letter.
099: if (Character.isLetter(ch)) {
100: ch -= caseDiff;
101: }
102: out.append(ch);
103: ch = Character.forDigit(lowbyte & 0xF, 16);
104: if (Character.isLetter(ch)) {
105: ch -= caseDiff;
106: }
107: out.append(ch);
108: }
109: }
110:
111: return out.toString();
112: }
113:
114: }
|