001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/HttpConstants.java,v 1.15 2004/04/18 23:51:35 jsdever Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient;
032:
033: import java.io.UnsupportedEncodingException;
034:
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037:
038: /**
039: * HTTP content conversion routines.
040: *
041: * @author Oleg Kalnichevski
042: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
043: *
044: * @deprecated use EncodingUtil class
045: */
046: public class HttpConstants {
047:
048: /** Character set used to encode HTTP protocol elements */
049: public static final String HTTP_ELEMENT_CHARSET = "US-ASCII";
050:
051: /** Default content encoding chatset */
052: public static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1";
053:
054: /** Log object for this class. */
055: private static final Log LOG = LogFactory
056: .getLog(HttpConstants.class);
057:
058: /**
059: * Converts the specified string to a byte array of HTTP element characters.
060: * This method is to be used when encoding content of HTTP elements (such as
061: * request headers)
062: *
063: * @param data the string to be encoded
064: * @return The resulting byte array.
065: */
066: public static byte[] getBytes(final String data) {
067: if (data == null) {
068: throw new IllegalArgumentException(
069: "Parameter may not be null");
070: }
071:
072: try {
073: return data.getBytes(HTTP_ELEMENT_CHARSET);
074: } catch (UnsupportedEncodingException e) {
075:
076: if (LOG.isWarnEnabled()) {
077: LOG.warn("Unsupported encoding: "
078: + HTTP_ELEMENT_CHARSET
079: + ". System default encoding used");
080: }
081:
082: return data.getBytes();
083: }
084: }
085:
086: /**
087: * Converts the byte array of HTTP element characters to a string This
088: * method is to be used when decoding content of HTTP elements (such as
089: * response headers)
090: *
091: * @param data the byte array to be encoded
092: * @param offset the index of the first byte to encode
093: * @param length the number of bytes to encode
094: * @return The resulting string.
095: */
096: public static String getString(final byte[] data, int offset,
097: int length) {
098:
099: if (data == null) {
100: throw new IllegalArgumentException(
101: "Parameter may not be null");
102: }
103:
104: try {
105: return new String(data, offset, length,
106: HTTP_ELEMENT_CHARSET);
107: } catch (UnsupportedEncodingException e) {
108:
109: if (LOG.isWarnEnabled()) {
110: LOG.warn("Unsupported encoding: "
111: + HTTP_ELEMENT_CHARSET
112: + ". System default encoding used");
113: }
114:
115: return new String(data, offset, length);
116: }
117: }
118:
119: /**
120: * Converts the byte array of HTTP element characters to a string This
121: * method is to be used when decoding content of HTTP elements (such as
122: * response headers)
123: *
124: * @param data the byte array to be encoded
125: * @return The resulting string.
126: */
127: public static String getString(final byte[] data) {
128: return getString(data, 0, data.length);
129: }
130:
131: /**
132: * Converts the specified string to a byte array of HTTP content charachetrs
133: * This method is to be used when encoding content of HTTP request/response
134: * If the specified charset is not supported, default HTTP content encoding
135: * (ISO-8859-1) is applied
136: *
137: * @param data the string to be encoded
138: * @param charset the desired character encoding
139: * @return The resulting byte array.
140: */
141: public static byte[] getContentBytes(final String data,
142: String charset) {
143:
144: if (data == null) {
145: throw new IllegalArgumentException(
146: "Parameter may not be null");
147: }
148:
149: if ((charset == null) || (charset.equals(""))) {
150: charset = DEFAULT_CONTENT_CHARSET;
151: }
152:
153: try {
154: return data.getBytes(charset);
155: } catch (UnsupportedEncodingException e) {
156:
157: if (LOG.isWarnEnabled()) {
158: LOG.warn("Unsupported encoding: " + charset
159: + ". HTTP default encoding used");
160: }
161:
162: try {
163: return data.getBytes(DEFAULT_CONTENT_CHARSET);
164: } catch (UnsupportedEncodingException e2) {
165:
166: if (LOG.isWarnEnabled()) {
167: LOG.warn("Unsupported encoding: "
168: + DEFAULT_CONTENT_CHARSET
169: + ". System encoding used");
170: }
171:
172: return data.getBytes();
173: }
174: }
175: }
176:
177: /**
178: * Converts the byte array of HTTP content characters to a string This
179: * method is to be used when decoding content of HTTP request/response If
180: * the specified charset is not supported, default HTTP content encoding
181: * (ISO-8859-1) is applied
182: *
183: * @param data the byte array to be encoded
184: * @param offset the index of the first byte to encode
185: * @param length the number of bytes to encode
186: * @param charset the desired character encoding
187: * @return The result of the conversion.
188: */
189: public static String getContentString(final byte[] data,
190: int offset, int length, String charset) {
191:
192: if (data == null) {
193: throw new IllegalArgumentException(
194: "Parameter may not be null");
195: }
196:
197: if ((charset == null) || (charset.equals(""))) {
198: charset = DEFAULT_CONTENT_CHARSET;
199: }
200:
201: try {
202: return new String(data, offset, length, charset);
203: } catch (UnsupportedEncodingException e) {
204:
205: if (LOG.isWarnEnabled()) {
206: LOG.warn("Unsupported encoding: " + charset
207: + ". Default HTTP encoding used");
208: }
209:
210: try {
211: return new String(data, offset, length,
212: DEFAULT_CONTENT_CHARSET);
213: } catch (UnsupportedEncodingException e2) {
214:
215: if (LOG.isWarnEnabled()) {
216: LOG.warn("Unsupported encoding: "
217: + DEFAULT_CONTENT_CHARSET
218: + ". System encoding used");
219: }
220:
221: return new String(data, offset, length);
222: }
223: }
224: }
225:
226: /**
227: * Converts the byte array of HTTP content characters to a string This
228: * method is to be used when decoding content of HTTP request/response If
229: * the specified charset is not supported, default HTTP content encoding
230: * (ISO-8859-1) is applied
231: *
232: * @param data the byte array to be encoded
233: * @param charset the desired character encoding
234: * @return The result of the conversion.
235: */
236: public static String getContentString(final byte[] data,
237: String charset) {
238: return getContentString(data, 0, data.length, charset);
239: }
240:
241: /**
242: * Converts the specified string to a byte array of HTTP content characters
243: * using default HTTP content encoding (ISO-8859-1) This method is to be
244: * used when encoding content of HTTP request/response
245: *
246: * @param data the string to be encoded
247: * @return The byte array as above.
248: */
249: public static byte[] getContentBytes(final String data) {
250: return getContentBytes(data, null);
251: }
252:
253: /**
254: * Converts the byte array of HTTP content characters to a string using
255: * default HTTP content encoding (ISO-8859-1) This method is to be used when
256: * decoding content of HTTP request/response
257: *
258: * @param data the byte array to be encoded
259: * @param offset the index of the first byte to encode
260: * @param length the number of bytes to encode
261: * @return The string representation of the byte array.
262: */
263: public static String getContentString(final byte[] data,
264: int offset, int length) {
265: return getContentString(data, offset, length, null);
266: }
267:
268: /**
269: * Converts the byte array of HTTP content characters to a string using
270: * default HTTP content encoding (ISO-8859-1) This method is to be used when
271: * decoding content of HTTP request/response
272: *
273: * @param data the byte array to be encoded
274: * @return The string representation of the byte array.
275: */
276: public static String getContentString(final byte[] data) {
277: return getContentString(data, null);
278: }
279:
280: /**
281: * Converts the specified string to byte array of ASCII characters.
282: *
283: * @param data the string to be encoded
284: * @return The string as a byte array.
285: */
286: public static byte[] getAsciiBytes(final String data) {
287:
288: if (data == null) {
289: throw new IllegalArgumentException(
290: "Parameter may not be null");
291: }
292:
293: try {
294: return data.getBytes("US-ASCII");
295: } catch (UnsupportedEncodingException e) {
296: throw new RuntimeException(
297: "HttpClient requires ASCII support");
298: }
299: }
300:
301: /**
302: * Converts the byte array of ASCII characters to a string. This method is
303: * to be used when decoding content of HTTP elements (such as response
304: * headers)
305: *
306: * @param data the byte array to be encoded
307: * @param offset the index of the first byte to encode
308: * @param length the number of bytes to encode
309: * @return The string representation of the byte array
310: */
311: public static String getAsciiString(final byte[] data, int offset,
312: int length) {
313:
314: if (data == null) {
315: throw new IllegalArgumentException(
316: "Parameter may not be null");
317: }
318:
319: try {
320: return new String(data, offset, length, "US-ASCII");
321: } catch (UnsupportedEncodingException e) {
322: throw new RuntimeException(
323: "HttpClient requires ASCII support");
324: }
325: }
326:
327: /**
328: * Converts the byte array of ASCII characters to a string. This method is
329: * to be used when decoding content of HTTP elements (such as response
330: * headers)
331: *
332: * @param data the byte array to be encoded
333: * @return The string representation of the byte array
334: */
335: public static String getAsciiString(final byte[] data) {
336: return getAsciiString(data, 0, data.length);
337: }
338: }
|