home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / net / URLDecoder.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  2.4 KB  |  76 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)URLDecoder.java    1.2 98/06/29
  3.  *
  4.  * Copyright 1998 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.*;
  18.  
  19. /**
  20.  * The class contains a utility method for converting from
  21.  * a MIME format called "<code>x-www-form-urlencoded</code>"
  22.  * to a <code>String</code>
  23.  * <p>
  24.  * To convert to a <code>String</code>, each character is examined in turn:
  25.  * <ul>
  26.  * <li>The ASCII characters '<code>a</code>' through '<code>z</code>',
  27.  * '<code>A</code>' through '<code>Z</code>', and '<code>0</code>'
  28.  * through '<code>9</code>' remain the same.
  29.  * <li>The plus sign '<code>+</code>'is converted into a
  30.  * space character '<code> </code>'.
  31.  * <li>The remaining characters are represented by 3-character
  32.  * strings which begin with the percent sign,
  33.  * "<code>%<i>xy</i></code>", where <i>xy</i> is the two-digit
  34.  * hexadecimal representation of the lower 8-bits of the character.
  35.  * </ul>
  36.  *
  37.  * @author  Mark Chamness
  38.  * @author  Michael McCloskey
  39.  * @version 1.2 06/29/98
  40.  * @since   JDK1.2
  41.  */
  42.  
  43. public class URLDecoder {
  44.  
  45.     public static String decode(String s) throws Exception {
  46.         StringBuffer sb = new StringBuffer();
  47.         for(int i=0; i<s.length(); i++) {
  48.             char c = s.charAt(i);
  49.             switch (c) {
  50.                 case '+':
  51.                     sb.append(' ');
  52.                     break;
  53.                 case '%':
  54.                     try {
  55.                         sb.append((char)Integer.parseInt(
  56.                                         s.substring(i+1,i+3),16));
  57.                     }
  58.                     catch (NumberFormatException e) {
  59.                         throw new IllegalArgumentException();
  60.                     }
  61.                     i += 2;
  62.                     break;
  63.                 default:
  64.                     sb.append(c);
  65.                     break;
  66.             }
  67.         }
  68.         // Undo conversion to external encoding
  69.         String result = sb.toString();
  70.         byte[] inputBytes = result.getBytes("8859_1");
  71.         return new String(inputBytes);
  72.     }
  73. }
  74.  
  75.  
  76.