~ubuntu-branches/ubuntu/trusty/httpcomponents-core/trusty

« back to all changes in this revision

Viewing changes to httpcore/src/main/java/org/apache/http/util/EncodingUtils.java

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-06-12 08:37:34 UTC
  • Revision ID: james.westby@ubuntu.com-20100612083734-1y8kp6qm4sjk60az
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore/src/main/java/org/apache/http/util/EncodingUtils.java $
 
3
 * $Revision: 744533 $
 
4
 * $Date: 2009-02-14 18:15:18 +0100 (Sat, 14 Feb 2009) $
 
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
 * 
 
41
 * @since 4.0
 
42
 */
 
43
public final class EncodingUtils {
 
44
 
 
45
    /**
 
46
     * Converts the byte array of HTTP content characters to a string. If
 
47
     * the specified charset is not supported, default system encoding
 
48
     * is used.
 
49
     *
 
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.
 
55
     */
 
56
    public static String getString(
 
57
        final byte[] data, 
 
58
        int offset, 
 
59
        int length, 
 
60
        String charset
 
61
    ) {
 
62
 
 
63
        if (data == null) {
 
64
            throw new IllegalArgumentException("Parameter may not be null");
 
65
        }
 
66
 
 
67
        if (charset == null || charset.length() == 0) {
 
68
            throw new IllegalArgumentException("charset may not be null or empty");
 
69
        }
 
70
 
 
71
        try {
 
72
            return new String(data, offset, length, charset);
 
73
        } catch (UnsupportedEncodingException e) {
 
74
            return new String(data, offset, length);
 
75
        }
 
76
    }
 
77
 
 
78
 
 
79
    /**
 
80
     * Converts the byte array of HTTP content characters to a string. If
 
81
     * the specified charset is not supported, default system encoding
 
82
     * is used.
 
83
     *
 
84
     * @param data the byte array to be encoded
 
85
     * @param charset the desired character encoding
 
86
     * @return The result of the conversion.
 
87
     */
 
88
    public static String getString(final byte[] data, final String charset) {
 
89
        if (data == null) {
 
90
            throw new IllegalArgumentException("Parameter may not be null");
 
91
        }
 
92
        return getString(data, 0, data.length, charset);
 
93
    }
 
94
 
 
95
    /**
 
96
     * Converts the specified string to a byte array.  If the charset is not supported the
 
97
     * default system charset is used.
 
98
     *
 
99
     * @param data the string to be encoded
 
100
     * @param charset the desired character encoding
 
101
     * @return The resulting byte array.
 
102
     */
 
103
    public static byte[] getBytes(final String data, final String charset) {
 
104
 
 
105
        if (data == null) {
 
106
            throw new IllegalArgumentException("data may not be null");
 
107
        }
 
108
 
 
109
        if (charset == null || charset.length() == 0) {
 
110
            throw new IllegalArgumentException("charset may not be null or empty");
 
111
        }
 
112
 
 
113
        try {
 
114
            return data.getBytes(charset);
 
115
        } catch (UnsupportedEncodingException e) {
 
116
            return data.getBytes();
 
117
        }
 
118
    }    
 
119
    
 
120
    /**
 
121
     * Converts the specified string to byte array of ASCII characters.
 
122
     *
 
123
     * @param data the string to be encoded
 
124
     * @return The string as a byte array.
 
125
     */
 
126
    public static byte[] getAsciiBytes(final String data) {
 
127
 
 
128
        if (data == null) {
 
129
            throw new IllegalArgumentException("Parameter may not be null");
 
130
        }
 
131
 
 
132
        try {
 
133
            return data.getBytes(HTTP.US_ASCII);
 
134
        } catch (UnsupportedEncodingException e) {
 
135
            throw new Error("HttpClient requires ASCII support");
 
136
        }
 
137
    }
 
138
 
 
139
    /**
 
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
 
142
     * headers)
 
143
     *
 
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
 
148
     */
 
149
    public static String getAsciiString(final byte[] data, int offset, int length) {
 
150
 
 
151
        if (data == null) {
 
152
            throw new IllegalArgumentException("Parameter may not be null");
 
153
        }
 
154
 
 
155
        try {
 
156
            return new String(data, offset, length, HTTP.US_ASCII);
 
157
        } catch (UnsupportedEncodingException e) {
 
158
            throw new Error("HttpClient requires ASCII support");
 
159
        }
 
160
    }
 
161
 
 
162
    /**
 
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
 
165
     * headers)
 
166
     *
 
167
     * @param data the byte array to be encoded
 
168
     * @return The string representation of the byte array
 
169
     */
 
170
    public static String getAsciiString(final byte[] data) {
 
171
        if (data == null) {
 
172
            throw new IllegalArgumentException("Parameter may not be null");
 
173
        }
 
174
        return getAsciiString(data, 0, data.length);
 
175
    }
 
176
 
 
177
    /**
 
178
     * This class should not be instantiated.
 
179
     */
 
180
    private EncodingUtils() {
 
181
    }
 
182
 
 
183
}