home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / URLEncoder.java < prev    next >
Text File  |  1997-10-01  |  4KB  |  134 lines

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