home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / Chip_1997-10_cd.bin / tema / sybase / powerj / java.z / URLEncoder.java < prev    next >
Text File  |  1996-05-03  |  3KB  |  95 lines

  1. /*
  2.  * @(#)URLEncoder.java    1.4 95/12/18 Herb Jellinek
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  8.  * without fee is hereby granted.
  9.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  10.  * for further important copyright and trademark information and to
  11.  * http://java.sun.com/licensing.html for further important licensing
  12.  * information for the Java (tm) Technology.
  13.  *
  14.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  15.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  16.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  18.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  19.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  20.  *
  21.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  22.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  23.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  24.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  25.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  26.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  27.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  28.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  29.  * HIGH RISK ACTIVITIES.
  30.  */
  31.  
  32. package java.net;
  33.  
  34. import java.io.ByteArrayOutputStream;
  35. import java.util.BitSet;
  36.  
  37.  
  38. /**
  39.  * Turns Strings of text into x-www-form-urlencoded format.
  40.  *
  41.  * @version 1.4, 18 Dec 1995
  42.  * @author Herb Jellinek
  43.  */
  44.  
  45. public class URLEncoder {
  46.  
  47.     static BitSet dontNeedEncoding;
  48.  
  49.     static {
  50.     dontNeedEncoding = new BitSet(256);
  51.     int i;
  52.     for (i = 'a'; i <= 'z'; i++) {
  53.         dontNeedEncoding.set(i);
  54.     }
  55.     for (i = 'A'; i <= 'Z'; i++) {
  56.         dontNeedEncoding.set(i);
  57.     }
  58.     for (i = '0'; i <= '9'; i++) {
  59.         dontNeedEncoding.set(i);
  60.     }
  61.     dontNeedEncoding.set('_');
  62.     dontNeedEncoding.set(' ');
  63.     }
  64.  
  65.     /**
  66.      * You can't call the constructor.
  67.      */
  68.     private URLEncoder() { }
  69.  
  70.     /**
  71.      * Translates String into x-www-form-urlencoded format.
  72.      * @param s String to be translated
  73.      * @return the translated String.
  74.      */
  75.     public static String encode(String s) {
  76.     ByteArrayOutputStream out = new ByteArrayOutputStream(s.length());
  77.     
  78.     for (int i = 0; i < s.length(); i++) {
  79.         int c = (int)s.charAt(i);
  80.         if (dontNeedEncoding.get(c)) {
  81.         if (c == ' ') {
  82.             c = '+';
  83.         }
  84.         out.write(c);
  85.         } else {
  86.         out.write('%');
  87.         out.write(Character.forDigit(c >> 4, 16));
  88.         out.write(Character.forDigit(c & 0xF, 16));
  89.         }
  90.     }
  91.  
  92.     return out.toString();
  93.     }
  94. }
  95.