home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / net / URLEncoder.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  3.6 KB  |  125 lines

  1. /*
  2.  * @(#)URLEncoder.java    1.13 98/03/18
  3.  *
  4.  * Copyright 1995-1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.net;
  16.  
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.OutputStreamWriter;
  19. import java.io.IOException;
  20. import java.util.BitSet;
  21.  
  22. /**
  23.  * The class contains a utility method for converting a 
  24.  * <code>String</code> into a MIME format called 
  25.  * "<code>x-www-form-urlencoded</code>" format. 
  26.  * <p>
  27.  * To convert a <code>String</code>, each character is examined in turn:
  28.  * <ul>
  29.  * <li>The ASCII characters '<code>a</code>' through '<code>z</code>', 
  30.  *     '<code>A</code>' through '<code>Z</code>', and '<code>0</code>' 
  31.  *     through '<code>9</code>' remain the same. 
  32.  * <li>The space character '<code> </code>' is converted into a 
  33.  *     plus sign '<code>+</code>'. 
  34.  * <li>All other characters are converted into the 3-character string 
  35.  *     "<code>%<i>xy</i></code>", where <i>xy</i> is the two-digit
  36.  *     hexadecimal representation of the lower 8-bits of the character.
  37.  * </ul>
  38.  *
  39.  * @author  Herb Jellinek
  40.  * @version 1.13, 03/18/98
  41.  * @since   JDK1.0
  42.  */
  43. public class URLEncoder {
  44.     static BitSet dontNeedEncoding;
  45.     static final int caseDiff = ('a' - 'A');
  46.  
  47.     /* The list of characters that are not encoded have been determined by 
  48.        referencing O'Reilly's "HTML: The Definitive Guide" (page 164). */
  49.        
  50.     static {
  51.     dontNeedEncoding = new BitSet(256);
  52.     int i;
  53.     for (i = 'a'; i <= 'z'; i++) {
  54.         dontNeedEncoding.set(i);
  55.     }
  56.     for (i = 'A'; i <= 'Z'; i++) {
  57.         dontNeedEncoding.set(i);
  58.     }
  59.     for (i = '0'; i <= '9'; i++) {
  60.         dontNeedEncoding.set(i);
  61.     }
  62.     dontNeedEncoding.set(' '); /* encoding a space to a + is done in the encode() method */
  63.     dontNeedEncoding.set('-');
  64.     dontNeedEncoding.set('_');
  65.     dontNeedEncoding.set('.');
  66.     dontNeedEncoding.set('*');
  67.     }
  68.  
  69.     /**
  70.      * You can't call the constructor.
  71.      */
  72.     private URLEncoder() { }
  73.  
  74.     /**
  75.      * Translates a string into <code>x-www-form-urlencoded</code> format.
  76.      *
  77.      * @param   s   <code>String</code> to be translated.
  78.      * @return  the translated <code>String</code>.
  79.      */
  80.     public static String encode(String s) {
  81.     int maxBytesPerChar = 10;
  82.     ByteArrayOutputStream out = new ByteArrayOutputStream(s.length());
  83.     ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar);
  84.     OutputStreamWriter writer = new OutputStreamWriter(buf);
  85.  
  86.     for (int i = 0; i < s.length(); i++) {
  87.         int c = (int)s.charAt(i);
  88.         if (dontNeedEncoding.get(c)) {
  89.         if (c == ' ') {
  90.             c = '+';
  91.         }
  92.         out.write(c);
  93.         } else {
  94.         // convert to external encoding before hex conversion
  95.         try {
  96.             writer.write(c);
  97.             writer.flush();
  98.         } catch(IOException e) {
  99.             buf.reset();
  100.             continue;
  101.         }
  102.         byte[] ba = buf.toByteArray();
  103.         for (int j = 0; j < ba.length; j++) {
  104.             out.write('%');
  105.             char ch = Character.forDigit((ba[j] >> 4) & 0xF, 16);
  106.             // converting to use uppercase letter as part of
  107.             // the hex value if ch is a letter.
  108.             if (Character.isLetter(ch)) {
  109.             ch -= caseDiff;
  110.             }
  111.             out.write(ch);
  112.             ch = Character.forDigit(ba[j] & 0xF, 16);
  113.             if (Character.isLetter(ch)) {
  114.             ch -= caseDiff;
  115.             }
  116.             out.write(ch);
  117.         }
  118.         buf.reset();
  119.         }
  120.     }
  121.  
  122.     return out.toString();
  123.     }
  124. }
  125.