001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/util/EncodingUtils.java $
003: * $Revision: 503413 $
004: * $Date: 2007-02-04 15:22:14 +0100 (Sun, 04 Feb 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with 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,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031: package org.apache.http.util;
032:
033: import java.io.UnsupportedEncodingException;
034:
035: import org.apache.http.protocol.HTTP;
036:
037: /**
038: * The home for utility methods that handle various encoding tasks.
039: *
040: * @author Michael Becke
041: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
042: *
043: * @since 4.0
044: */
045: public final class EncodingUtils {
046:
047: /**
048: * Converts the byte array of HTTP content characters to a string. If
049: * the specified charset is not supported, default system encoding
050: * is used.
051: *
052: * @param data the byte array to be encoded
053: * @param offset the index of the first byte to encode
054: * @param length the number of bytes to encode
055: * @param charset the desired character encoding
056: * @return The result of the conversion.
057: */
058: public static String getString(final byte[] data, int offset,
059: int length, String charset) {
060:
061: if (data == null) {
062: throw new IllegalArgumentException(
063: "Parameter may not be null");
064: }
065:
066: if (charset == null || charset.length() == 0) {
067: throw new IllegalArgumentException(
068: "charset may not be null or empty");
069: }
070:
071: try {
072: return new String(data, offset, length, charset);
073: } catch (UnsupportedEncodingException e) {
074: return new String(data, offset, length);
075: }
076: }
077:
078: /**
079: * Converts the byte array of HTTP content characters to a string. If
080: * the specified charset is not supported, default system encoding
081: * is used.
082: *
083: * @param data the byte array to be encoded
084: * @param charset the desired character encoding
085: * @return The result of the conversion.
086: */
087: public static String getString(final byte[] data,
088: final String charset) {
089: if (data == null) {
090: throw new IllegalArgumentException(
091: "Parameter may not be null");
092: }
093: return getString(data, 0, data.length, charset);
094: }
095:
096: /**
097: * Converts the specified string to a byte array. If the charset is not supported the
098: * default system charset is used.
099: *
100: * @param data the string to be encoded
101: * @param charset the desired character encoding
102: * @return The resulting byte array.
103: */
104: public static byte[] getBytes(final String data,
105: 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(
113: "charset may not be null or empty");
114: }
115:
116: try {
117: return data.getBytes(charset);
118: } catch (UnsupportedEncodingException e) {
119: return data.getBytes();
120: }
121: }
122:
123: /**
124: * Converts the specified string to byte array of ASCII characters.
125: *
126: * @param data the string to be encoded
127: * @return The string as a byte array.
128: */
129: public static byte[] getAsciiBytes(final String data) {
130:
131: if (data == null) {
132: throw new IllegalArgumentException(
133: "Parameter may not be null");
134: }
135:
136: try {
137: return data.getBytes(HTTP.US_ASCII);
138: } catch (UnsupportedEncodingException e) {
139: throw new Error("HttpClient requires ASCII support");
140: }
141: }
142:
143: /**
144: * Converts the byte array of ASCII characters to a string. This method is
145: * to be used when decoding content of HTTP elements (such as response
146: * headers)
147: *
148: * @param data the byte array to be encoded
149: * @param offset the index of the first byte to encode
150: * @param length the number of bytes to encode
151: * @return The string representation of the byte array
152: */
153: public static String getAsciiString(final byte[] data, int offset,
154: int length) {
155:
156: if (data == null) {
157: throw new IllegalArgumentException(
158: "Parameter may not be null");
159: }
160:
161: try {
162: return new String(data, offset, length, HTTP.US_ASCII);
163: } catch (UnsupportedEncodingException e) {
164: throw new Error("HttpClient requires ASCII support");
165: }
166: }
167:
168: /**
169: * Converts the byte array of ASCII characters to a string. This method is
170: * to be used when decoding content of HTTP elements (such as response
171: * headers)
172: *
173: * @param data the byte array to be encoded
174: * @return The string representation of the byte array
175: */
176: public static String getAsciiString(final byte[] data) {
177: if (data == null) {
178: throw new IllegalArgumentException(
179: "Parameter may not be null");
180: }
181: return getAsciiString(data, 0, data.length);
182: }
183:
184: /**
185: * This class should not be instantiated.
186: */
187: private EncodingUtils() {
188: }
189:
190: }
|