1 31 32 package org.apache.commons.httpclient; 33 34 import java.io.UnsupportedEncodingException ; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 40 46 public class HttpConstants { 47 48 49 public static final String HTTP_ELEMENT_CHARSET = "US-ASCII"; 50 51 52 public static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1"; 53 54 55 private static final Log LOG = LogFactory.getLog(HttpConstants.class); 56 57 65 public static byte[] getBytes(final String data) { 66 if (data == null) { 67 throw new IllegalArgumentException ("Parameter may not be null"); 68 } 69 70 try { 71 return data.getBytes(HTTP_ELEMENT_CHARSET); 72 } catch (UnsupportedEncodingException e) { 73 74 if (LOG.isWarnEnabled()) { 75 LOG.warn("Unsupported encoding: " 76 + HTTP_ELEMENT_CHARSET 77 + ". System default encoding used"); 78 } 79 80 return data.getBytes(); 81 } 82 } 83 84 94 public static String getString(final byte[] data, int offset, int length) { 95 96 if (data == null) { 97 throw new IllegalArgumentException ("Parameter may not be null"); 98 } 99 100 try { 101 return new String (data, offset, length, HTTP_ELEMENT_CHARSET); 102 } catch (UnsupportedEncodingException e) { 103 104 if (LOG.isWarnEnabled()) { 105 LOG.warn("Unsupported encoding: " 106 + HTTP_ELEMENT_CHARSET 107 + ". System default encoding used"); 108 } 109 110 return new String (data, offset, length); 111 } 112 } 113 114 122 public static String getString(final byte[] data) { 123 return getString(data, 0, data.length); 124 } 125 126 136 public static byte[] getContentBytes(final String data, String charset) { 137 138 if (data == null) { 139 throw new IllegalArgumentException ("Parameter may not be null"); 140 } 141 142 if ((charset == null) || (charset.equals(""))) { 143 charset = DEFAULT_CONTENT_CHARSET; 144 } 145 146 try { 147 return data.getBytes(charset); 148 } catch (UnsupportedEncodingException e) { 149 150 if (LOG.isWarnEnabled()) { 151 LOG.warn("Unsupported encoding: " 152 + charset 153 + ". HTTP default encoding used"); 154 } 155 156 try { 157 return data.getBytes(DEFAULT_CONTENT_CHARSET); 158 } catch (UnsupportedEncodingException e2) { 159 160 if (LOG.isWarnEnabled()) { 161 LOG.warn("Unsupported encoding: " 162 + DEFAULT_CONTENT_CHARSET 163 + ". System encoding used"); 164 } 165 166 return data.getBytes(); 167 } 168 } 169 } 170 171 183 public static String getContentString( 184 final byte[] data, 185 int offset, 186 int length, 187 String charset 188 ) { 189 190 if (data == null) { 191 throw new IllegalArgumentException ("Parameter may not be null"); 192 } 193 194 if ((charset == null) || (charset.equals(""))) { 195 charset = DEFAULT_CONTENT_CHARSET; 196 } 197 198 try { 199 return new String (data, offset, length, charset); 200 } catch (UnsupportedEncodingException e) { 201 202 if (LOG.isWarnEnabled()) { 203 LOG.warn("Unsupported encoding: " + charset + ". Default HTTP encoding used"); 204 } 205 206 try { 207 return new String (data, offset, length, DEFAULT_CONTENT_CHARSET); 208 } catch (UnsupportedEncodingException e2) { 209 210 if (LOG.isWarnEnabled()) { 211 LOG.warn("Unsupported encoding: " 212 + DEFAULT_CONTENT_CHARSET 213 + ". System encoding used"); 214 } 215 216 return new String (data, offset, length); 217 } 218 } 219 } 220 221 222 232 public static String getContentString(final byte[] data, String charset) { 233 return getContentString(data, 0, data.length, charset); 234 } 235 236 244 public static byte[] getContentBytes(final String data) { 245 return getContentBytes(data, null); 246 } 247 248 258 public static String getContentString(final byte[] data, int offset, int length) { 259 return getContentString(data, offset, length, null); 260 } 261 262 270 public static String getContentString(final byte[] data) { 271 return getContentString(data, null); 272 } 273 274 280 public static byte[] getAsciiBytes(final String data) { 281 282 if (data == null) { 283 throw new IllegalArgumentException ("Parameter may not be null"); 284 } 285 286 try { 287 return data.getBytes("US-ASCII"); 288 } catch (UnsupportedEncodingException e) { 289 throw new RuntimeException ("HttpClient requires ASCII support"); 290 } 291 } 292 293 303 public static String getAsciiString(final byte[] data, int offset, int length) { 304 305 if (data == null) { 306 throw new IllegalArgumentException ("Parameter may not be null"); 307 } 308 309 try { 310 return new String (data, offset, length, "US-ASCII"); 311 } catch (UnsupportedEncodingException e) { 312 throw new RuntimeException ("HttpClient requires ASCII support"); 313 } 314 } 315 316 324 public static String getAsciiString(final byte[] data) { 325 return getAsciiString(data, 0, data.length); 326 } 327 } 328 | Popular Tags |