2
* $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore/src/main/java/org/apache/http/util/EncodingUtils.java $
4
* $Date: 2009-02-14 18:15:18 +0100 (Sat, 14 Feb 2009) $
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
15
* http://www.apache.org/licenses/LICENSE-2.0
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
23
* ====================================================================
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/>.
31
package org.apache.http.util;
33
import java.io.UnsupportedEncodingException;
35
import org.apache.http.protocol.HTTP;
38
* The home for utility methods that handle various encoding tasks.
43
public final class EncodingUtils {
46
* Converts the byte array of HTTP content characters to a string. If
47
* the specified charset is not supported, default system encoding
50
* @param data the byte array to be encoded
51
* @param offset the index of the first byte to encode
52
* @param length the number of bytes to encode
53
* @param charset the desired character encoding
54
* @return The result of the conversion.
56
public static String getString(
64
throw new IllegalArgumentException("Parameter may not be null");
67
if (charset == null || charset.length() == 0) {
68
throw new IllegalArgumentException("charset may not be null or empty");
72
return new String(data, offset, length, charset);
73
} catch (UnsupportedEncodingException e) {
74
return new String(data, offset, length);
80
* Converts the byte array of HTTP content characters to a string. If
81
* the specified charset is not supported, default system encoding
84
* @param data the byte array to be encoded
85
* @param charset the desired character encoding
86
* @return The result of the conversion.
88
public static String getString(final byte[] data, final String charset) {
90
throw new IllegalArgumentException("Parameter may not be null");
92
return getString(data, 0, data.length, charset);
96
* Converts the specified string to a byte array. If the charset is not supported the
97
* default system charset is used.
99
* @param data the string to be encoded
100
* @param charset the desired character encoding
101
* @return The resulting byte array.
103
public static byte[] getBytes(final String data, final String charset) {
106
throw new IllegalArgumentException("data may not be null");
109
if (charset == null || charset.length() == 0) {
110
throw new IllegalArgumentException("charset may not be null or empty");
114
return data.getBytes(charset);
115
} catch (UnsupportedEncodingException e) {
116
return data.getBytes();
121
* Converts the specified string to byte array of ASCII characters.
123
* @param data the string to be encoded
124
* @return The string as a byte array.
126
public static byte[] getAsciiBytes(final String data) {
129
throw new IllegalArgumentException("Parameter may not be null");
133
return data.getBytes(HTTP.US_ASCII);
134
} catch (UnsupportedEncodingException e) {
135
throw new Error("HttpClient requires ASCII support");
140
* Converts the byte array of ASCII characters to a string. This method is
141
* to be used when decoding content of HTTP elements (such as response
144
* @param data the byte array to be encoded
145
* @param offset the index of the first byte to encode
146
* @param length the number of bytes to encode
147
* @return The string representation of the byte array
149
public static String getAsciiString(final byte[] data, int offset, int length) {
152
throw new IllegalArgumentException("Parameter may not be null");
156
return new String(data, offset, length, HTTP.US_ASCII);
157
} catch (UnsupportedEncodingException e) {
158
throw new Error("HttpClient requires ASCII support");
163
* Converts the byte array of ASCII characters to a string. This method is
164
* to be used when decoding content of HTTP elements (such as response
167
* @param data the byte array to be encoded
168
* @return The string representation of the byte array
170
public static String getAsciiString(final byte[] data) {
172
throw new IllegalArgumentException("Parameter may not be null");
174
return getAsciiString(data, 0, data.length);
178
* This class should not be instantiated.
180
private EncodingUtils() {