View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.util;
28  
29  import java.io.UnsupportedEncodingException;
30  
31  import org.apache.http.Consts;
32  
33  /**
34   * The home for utility methods that handle various encoding tasks.
35   *
36   *
37   * @since 4.0
38   */
39  public final class EncodingUtils {
40  
41      /**
42       * Converts the byte array of HTTP content characters to a string. If
43       * the specified charset is not supported, default system encoding
44       * is used.
45       *
46       * @param data the byte array to be encoded
47       * @param offset the index of the first byte to encode
48       * @param length the number of bytes to encode
49       * @param charset the desired character encoding
50       * @return The result of the conversion.
51       */
52      public static String getString(
53          final byte[] data,
54          final int offset,
55          final int length,
56          final String charset) {
57          Args.notNull(data, "Input");
58          Args.notEmpty(charset, "Charset");
59          try {
60              return new String(data, offset, length, charset);
61          } catch (final UnsupportedEncodingException e) {
62              return new String(data, offset, length);
63          }
64      }
65  
66  
67      /**
68       * Converts the byte array of HTTP content characters to a string. If
69       * the specified charset is not supported, default system encoding
70       * is used.
71       *
72       * @param data the byte array to be encoded
73       * @param charset the desired character encoding
74       * @return The result of the conversion.
75       */
76      public static String getString(final byte[] data, final String charset) {
77          Args.notNull(data, "Input");
78          return getString(data, 0, data.length, charset);
79      }
80  
81      /**
82       * Converts the specified string to a byte array.  If the charset is not supported the
83       * default system charset is used.
84       *
85       * @param data the string to be encoded
86       * @param charset the desired character encoding
87       * @return The resulting byte array.
88       */
89      public static byte[] getBytes(final String data, final String charset) {
90          Args.notNull(data, "Input");
91          Args.notEmpty(charset, "Charset");
92          try {
93              return data.getBytes(charset);
94          } catch (final UnsupportedEncodingException e) {
95              return data.getBytes();
96          }
97      }
98  
99      /**
100      * Converts the specified string to byte array of ASCII characters.
101      *
102      * @param data the string to be encoded
103      * @return The string as a byte array.
104      */
105     public static byte[] getAsciiBytes(final String data) {
106         Args.notNull(data, "Input");
107         return data.getBytes(Consts.ASCII);
108     }
109 
110     /**
111      * Converts the byte array of ASCII characters to a string. This method is
112      * to be used when decoding content of HTTP elements (such as response
113      * headers)
114      *
115      * @param data the byte array to be encoded
116      * @param offset the index of the first byte to encode
117      * @param length the number of bytes to encode
118      * @return The string representation of the byte array
119      */
120     public static String getAsciiString(final byte[] data, final int offset, final int length) {
121         Args.notNull(data, "Input");
122         return new String(data, offset, length, Consts.ASCII);
123     }
124 
125     /**
126      * Converts the byte array of ASCII characters to a string. This method is
127      * to be used when decoding content of HTTP elements (such as response
128      * headers)
129      *
130      * @param data the byte array to be encoded
131      * @return The string representation of the byte array
132      */
133     public static String getAsciiString(final byte[] data) {
134         Args.notNull(data, "Input");
135         return getAsciiString(data, 0, data.length);
136     }
137 
138     /**
139      * This class should not be instantiated.
140      */
141     private EncodingUtils() {
142     }
143 
144 }