KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > HttpConstants


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConstants.java,v 1.10.2.2 2004/02/27 19:11:10 olegk Exp $
3  * $Revision: 1.10.2.2 $
4  * $Date: 2004/02/27 19:11:10 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ====================================================================
22  *
23  * This software consists of voluntary contributions made by many
24  * individuals on behalf of the Apache Software Foundation. For more
25  * information on the Apache Software Foundation, please see
26  * <http://www.apache.org/>.
27  *
28  * [Additional notices, if required by prior licensing conditions]
29  *
30  */

31
32 package org.apache.commons.httpclient;
33
34 import java.io.UnsupportedEncodingException JavaDoc;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39
40 /**
41  * HTTP content conversion routines.
42  *
43  * @author Oleg Kalnichevski
44  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
45  */

46 public class HttpConstants {
47
48     /** Character set used to encode HTTP protocol elements */
49     public static final String JavaDoc HTTP_ELEMENT_CHARSET = "US-ASCII";
50
51     /** Default content encoding chatset */
52     public static final String JavaDoc DEFAULT_CONTENT_CHARSET = "ISO-8859-1";
53
54     /** Log object for this class. */
55     private static final Log LOG = LogFactory.getLog(HttpConstants.class);
56
57     /**
58      * Converts the specified string to a byte array of HTTP element characters.
59      * This method is to be used when encoding content of HTTP elements (such as
60      * request headers)
61      *
62      * @param data the string to be encoded
63      * @return The resulting byte array.
64      */

65     public static byte[] getBytes(final String JavaDoc data) {
66         if (data == null) {
67             throw new IllegalArgumentException JavaDoc("Parameter may not be null");
68         }
69
70         try {
71             return data.getBytes(HTTP_ELEMENT_CHARSET);
72         } catch (UnsupportedEncodingException JavaDoc 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     /**
85      * Converts the byte array of HTTP element characters to a string This
86      * method is to be used when decoding content of HTTP elements (such as
87      * response headers)
88      *
89      * @param data the byte array to be encoded
90      * @param offset the index of the first byte to encode
91      * @param length the number of bytes to encode
92      * @return The resulting string.
93      */

94     public static String JavaDoc getString(final byte[] data, int offset, int length) {
95
96         if (data == null) {
97             throw new IllegalArgumentException JavaDoc("Parameter may not be null");
98         }
99
100         try {
101             return new String JavaDoc(data, offset, length, HTTP_ELEMENT_CHARSET);
102         } catch (UnsupportedEncodingException JavaDoc 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 JavaDoc(data, offset, length);
111         }
112     }
113
114     /**
115      * Converts the byte array of HTTP element characters to a string This
116      * method is to be used when decoding content of HTTP elements (such as
117      * response headers)
118      *
119      * @param data the byte array to be encoded
120      * @return The resulting string.
121      */

122     public static String JavaDoc getString(final byte[] data) {
123         return getString(data, 0, data.length);
124     }
125
126     /**
127      * Converts the specified string to a byte array of HTTP content charachetrs
128      * This method is to be used when encoding content of HTTP request/response
129      * If the specified charset is not supported, default HTTP content encoding
130      * (ISO-8859-1) is applied
131      *
132      * @param data the string to be encoded
133      * @param charset the desired character encoding
134      * @return The resulting byte array.
135      */

136     public static byte[] getContentBytes(final String JavaDoc data, String JavaDoc charset) {
137
138         if (data == null) {
139             throw new IllegalArgumentException JavaDoc("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 JavaDoc 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 JavaDoc 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     /**
172      * Converts the byte array of HTTP content characters to a string This
173      * method is to be used when decoding content of HTTP request/response If
174      * the specified charset is not supported, default HTTP content encoding
175      * (ISO-8859-1) is applied
176      *
177      * @param data the byte array to be encoded
178      * @param offset the index of the first byte to encode
179      * @param length the number of bytes to encode
180      * @param charset the desired character encoding
181      * @return The result of the conversion.
182      */

183     public static String JavaDoc getContentString(
184         final byte[] data,
185         int offset,
186         int length,
187         String JavaDoc charset
188     ) {
189
190         if (data == null) {
191             throw new IllegalArgumentException JavaDoc("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 JavaDoc(data, offset, length, charset);
200         } catch (UnsupportedEncodingException JavaDoc e) {
201
202             if (LOG.isWarnEnabled()) {
203                 LOG.warn("Unsupported encoding: " + charset + ". Default HTTP encoding used");
204             }
205
206             try {
207                 return new String JavaDoc(data, offset, length, DEFAULT_CONTENT_CHARSET);
208             } catch (UnsupportedEncodingException JavaDoc 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 JavaDoc(data, offset, length);
217             }
218         }
219     }
220
221
222     /**
223      * Converts the byte array of HTTP content characters to a string This
224      * method is to be used when decoding content of HTTP request/response If
225      * the specified charset is not supported, default HTTP content encoding
226      * (ISO-8859-1) is applied
227      *
228      * @param data the byte array to be encoded
229      * @param charset the desired character encoding
230      * @return The result of the conversion.
231      */

232     public static String JavaDoc getContentString(final byte[] data, String JavaDoc charset) {
233         return getContentString(data, 0, data.length, charset);
234     }
235
236     /**
237      * Converts the specified string to a byte array of HTTP content characters
238      * using default HTTP content encoding (ISO-8859-1) This method is to be
239      * used when encoding content of HTTP request/response
240      *
241      * @param data the string to be encoded
242      * @return The byte array as above.
243      */

244     public static byte[] getContentBytes(final String JavaDoc data) {
245         return getContentBytes(data, null);
246     }
247
248     /**
249      * Converts the byte array of HTTP content characters to a string using
250      * default HTTP content encoding (ISO-8859-1) This method is to be used when
251      * decoding content of HTTP request/response
252      *
253      * @param data the byte array to be encoded
254      * @param offset the index of the first byte to encode
255      * @param length the number of bytes to encode
256      * @return The string representation of the byte array.
257      */

258     public static String JavaDoc getContentString(final byte[] data, int offset, int length) {
259         return getContentString(data, offset, length, null);
260     }
261
262     /**
263      * Converts the byte array of HTTP content characters to a string using
264      * default HTTP content encoding (ISO-8859-1) This method is to be used when
265      * decoding content of HTTP request/response
266      *
267      * @param data the byte array to be encoded
268      * @return The string representation of the byte array.
269      */

270     public static String JavaDoc getContentString(final byte[] data) {
271         return getContentString(data, null);
272     }
273
274     /**
275      * Converts the specified string to byte array of ASCII characters.
276      *
277      * @param data the string to be encoded
278      * @return The string as a byte array.
279      */

280     public static byte[] getAsciiBytes(final String JavaDoc data) {
281
282         if (data == null) {
283             throw new IllegalArgumentException JavaDoc("Parameter may not be null");
284         }
285
286         try {
287             return data.getBytes("US-ASCII");
288         } catch (UnsupportedEncodingException JavaDoc e) {
289             throw new RuntimeException JavaDoc("HttpClient requires ASCII support");
290         }
291     }
292
293     /**
294      * Converts the byte array of ASCII characters to a string. This method is
295      * to be used when decoding content of HTTP elements (such as response
296      * headers)
297      *
298      * @param data the byte array to be encoded
299      * @param offset the index of the first byte to encode
300      * @param length the number of bytes to encode
301      * @return The string representation of the byte array
302      */

303     public static String JavaDoc getAsciiString(final byte[] data, int offset, int length) {
304
305         if (data == null) {
306             throw new IllegalArgumentException JavaDoc("Parameter may not be null");
307         }
308
309         try {
310             return new String JavaDoc(data, offset, length, "US-ASCII");
311         } catch (UnsupportedEncodingException JavaDoc e) {
312             throw new RuntimeException JavaDoc("HttpClient requires ASCII support");
313         }
314     }
315
316     /**
317      * Converts the byte array of ASCII characters to a string. This method is
318      * to be used when decoding content of HTTP elements (such as response
319      * headers)
320      *
321      * @param data the byte array to be encoded
322      * @return The string representation of the byte array
323      */

324     public static String JavaDoc getAsciiString(final byte[] data) {
325         return getAsciiString(data, 0, data.length);
326     }
327 }
328
Popular Tags