• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/EncodingUtils.java $
3  * $Revision: 503413 $
4  * $Date: 2007-02-04 06:22:14 -0800 (Sun, 04 Feb 2007) $
5  *
6  * ====================================================================
7  * Licensed to the Apache Software Foundation (ASF) under one
8  * or more contributor license agreements.  See the NOTICE file
9  * distributed with this work for additional information
10  * regarding copyright ownership.  The ASF licenses this file
11  * to you under the Apache License, Version 2.0 (the
12  * "License"); you may not use this file except in compliance
13  * with the License.  You may obtain a copy of the License at
14  *
15  *   http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing,
18  * software distributed under the License is distributed on an
19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20  * KIND, either express or implied.  See the License for the
21  * specific language governing permissions and limitations
22  * under the License.
23  * ====================================================================
24  *
25  * This software consists of voluntary contributions made by many
26  * individuals on behalf of the Apache Software Foundation.  For more
27  * information on the Apache Software Foundation, please see
28  * <http://www.apache.org/>.
29  *
30  */
31 package org.apache.http.util;
32 
33 import java.io.UnsupportedEncodingException;
34 
35 import org.apache.http.protocol.HTTP;
36 
37 /**
38  * The home for utility methods that handle various encoding tasks.
39  *
40  * @author Michael Becke
41  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
42  *
43  * @since 4.0
44  */
45 public final class EncodingUtils {
46 
47     /**
48      * Converts the byte array of HTTP content characters to a string. If
49      * the specified charset is not supported, default system encoding
50      * is used.
51      *
52      * @param data the byte array to be encoded
53      * @param offset the index of the first byte to encode
54      * @param length the number of bytes to encode
55      * @param charset the desired character encoding
56      * @return The result of the conversion.
57      */
getString( final byte[] data, int offset, int length, String charset )58     public static String getString(
59         final byte[] data,
60         int offset,
61         int length,
62         String charset
63     ) {
64 
65         if (data == null) {
66             throw new IllegalArgumentException("Parameter may not be null");
67         }
68 
69         if (charset == null || charset.length() == 0) {
70             throw new IllegalArgumentException("charset may not be null or empty");
71         }
72 
73         try {
74             return new String(data, offset, length, charset);
75         } catch (UnsupportedEncodingException e) {
76             return new String(data, offset, length);
77         }
78     }
79 
80 
81     /**
82      * Converts the byte array of HTTP content characters to a string. If
83      * the specified charset is not supported, default system encoding
84      * is used.
85      *
86      * @param data the byte array to be encoded
87      * @param charset the desired character encoding
88      * @return The result of the conversion.
89      */
getString(final byte[] data, final String charset)90     public static String getString(final byte[] data, final String charset) {
91         if (data == null) {
92             throw new IllegalArgumentException("Parameter may not be null");
93         }
94         return getString(data, 0, data.length, charset);
95     }
96 
97     /**
98      * Converts the specified string to a byte array.  If the charset is not supported the
99      * default system charset is used.
100      *
101      * @param data the string to be encoded
102      * @param charset the desired character encoding
103      * @return The resulting byte array.
104      */
getBytes(final String data, final String charset)105     public static byte[] getBytes(final String data, final String charset) {
106 
107         if (data == null) {
108             throw new IllegalArgumentException("data may not be null");
109         }
110 
111         if (charset == null || charset.length() == 0) {
112             throw new IllegalArgumentException("charset may not be null or empty");
113         }
114 
115         try {
116             return data.getBytes(charset);
117         } catch (UnsupportedEncodingException e) {
118             return data.getBytes();
119         }
120     }
121 
122     /**
123      * Converts the specified string to byte array of ASCII characters.
124      *
125      * @param data the string to be encoded
126      * @return The string as a byte array.
127      */
getAsciiBytes(final String data)128     public static byte[] getAsciiBytes(final String data) {
129 
130         if (data == null) {
131             throw new IllegalArgumentException("Parameter may not be null");
132         }
133 
134         try {
135             return data.getBytes(HTTP.US_ASCII);
136         } catch (UnsupportedEncodingException e) {
137             throw new Error("HttpClient requires ASCII support");
138         }
139     }
140 
141     /**
142      * Converts the byte array of ASCII characters to a string. This method is
143      * to be used when decoding content of HTTP elements (such as response
144      * headers)
145      *
146      * @param data the byte array to be encoded
147      * @param offset the index of the first byte to encode
148      * @param length the number of bytes to encode
149      * @return The string representation of the byte array
150      */
getAsciiString(final byte[] data, int offset, int length)151     public static String getAsciiString(final byte[] data, int offset, int length) {
152 
153         if (data == null) {
154             throw new IllegalArgumentException("Parameter may not be null");
155         }
156 
157         try {
158             return new String(data, offset, length, HTTP.US_ASCII);
159         } catch (UnsupportedEncodingException e) {
160             throw new Error("HttpClient requires ASCII support");
161         }
162     }
163 
164     /**
165      * Converts the byte array of ASCII characters to a string. This method is
166      * to be used when decoding content of HTTP elements (such as response
167      * headers)
168      *
169      * @param data the byte array to be encoded
170      * @return The string representation of the byte array
171      */
getAsciiString(final byte[] data)172     public static String getAsciiString(final byte[] data) {
173         if (data == null) {
174             throw new IllegalArgumentException("Parameter may not be null");
175         }
176         return getAsciiString(data, 0, data.length);
177     }
178 
179     /**
180      * This class should not be instantiated.
181      */
EncodingUtils()182     private EncodingUtils() {
183     }
184 
185 }
186