URLEncoder.java revision 55392539fea537abfb6581b474918f9d611fba27
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package java.net; 19 20import java.io.UnsupportedEncodingException; 21 22/** 23 * This class is used to encode a string using the format required by 24 * {@code application/x-www-form-urlencoded} MIME content type. 25 */ 26public class URLEncoder { 27 28 static final String digits = "0123456789ABCDEF"; //$NON-NLS-1$ 29 30 /** 31 * Prevents this class from being instantiated. 32 */ 33 private URLEncoder() { 34 } 35 36 /** 37 * Encodes a given string {@code s} in a x-www-form-urlencoded string using 38 * the specified encoding scheme {@code enc}. 39 * <p> 40 * All characters except letters ('a'..'z', 'A'..'Z') and numbers ('0'..'9') 41 * and characters '.', '-', '*', '_' are converted into their hexadecimal 42 * value prepended by '%'. For example: '#' -> %23. In addition, spaces are 43 * substituted by '+' 44 * 45 * @param s 46 * the string to be encoded. 47 * @return the encoded string. 48 * @deprecated use {@link #encode(String, String)} instead. 49 */ 50 @Deprecated 51 public static String encode(String s) { 52 // Guess a bit bigger for encoded form 53 StringBuilder buf = new StringBuilder(s.length() + 16); 54 for (int i = 0; i < s.length(); i++) { 55 char ch = s.charAt(i); 56 if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') 57 || (ch >= '0' && ch <= '9') || ".-*_".indexOf(ch) > -1) { //$NON-NLS-1$ 58 buf.append(ch); 59 } else if (ch == ' ') { 60 buf.append('+'); 61 } else { 62 byte[] bytes = new String(new char[] { ch }).getBytes(); 63 for (int j = 0; j < bytes.length; j++) { 64 buf.append('%'); 65 buf.append(digits.charAt((bytes[j] & 0xf0) >> 4)); 66 buf.append(digits.charAt(bytes[j] & 0xf)); 67 } 68 } 69 } 70 return buf.toString(); 71 } 72 73 /** 74 * Encodes the given string {@code s} in a x-www-form-urlencoded string 75 * using the specified encoding scheme {@code enc}. 76 * <p> 77 * All characters except letters ('a'..'z', 'A'..'Z') and numbers ('0'..'9') 78 * and characters '.', '-', '*', '_' are converted into their hexadecimal 79 * value prepended by '%'. For example: '#' -> %23. In addition, spaces are 80 * substituted by '+' 81 * 82 * @param s 83 * the string to be encoded. 84 * @param enc 85 * the encoding scheme to be used. 86 * @return the encoded string. 87 * @throws UnsupportedEncodingException 88 * if the specified encoding scheme is invalid. 89 */ 90 public static String encode(String s, String enc) 91 throws UnsupportedEncodingException { 92 93 if (s == null || enc == null) { 94 throw new NullPointerException(); 95 } 96 // check for UnsupportedEncodingException 97 "".getBytes(enc); //$NON-NLS-1$ 98 99 // Guess a bit bigger for encoded form 100 StringBuffer buf = new StringBuffer(s.length() + 16); 101 int start = -1; 102 for (int i = 0; i < s.length(); i++) { 103 char ch = s.charAt(i); 104 if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') 105 || (ch >= '0' && ch <= '9') || " .-*_".indexOf(ch) > -1) { //$NON-NLS-1$ 106 if (start >= 0) { 107 convert(s.substring(start, i), buf, enc); 108 start = -1; 109 } 110 if (ch != ' ') { 111 buf.append(ch); 112 } else { 113 buf.append('+'); 114 } 115 } else { 116 if (start < 0) { 117 start = i; 118 } 119 } 120 } 121 if (start >= 0) { 122 convert(s.substring(start, s.length()), buf, enc); 123 } 124 return buf.toString(); 125 } 126 127 private static void convert(String s, StringBuffer buf, String enc) 128 throws UnsupportedEncodingException { 129 byte[] bytes = s.getBytes(enc); 130 for (int j = 0; j < bytes.length; j++) { 131 buf.append('%'); 132 buf.append(digits.charAt((bytes[j] & 0xf0) >> 4)); 133 buf.append(digits.charAt(bytes[j] & 0xf)); 134 } 135 } 136} 137