Home | History | Annotate | Download | only in util
      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  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     46  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     47  *     for further details.
     48  */
     49 @Deprecated
     50 public final class EncodingUtils {
     51 
     52     /**
     53      * Converts the byte array of HTTP content characters to a string. If
     54      * the specified charset is not supported, default system encoding
     55      * is used.
     56      *
     57      * @param data the byte array to be encoded
     58      * @param offset the index of the first byte to encode
     59      * @param length the number of bytes to encode
     60      * @param charset the desired character encoding
     61      * @return The result of the conversion.
     62      */
     63     public static String getString(
     64         final byte[] data,
     65         int offset,
     66         int length,
     67         String charset
     68     ) {
     69 
     70         if (data == null) {
     71             throw new IllegalArgumentException("Parameter may not be null");
     72         }
     73 
     74         if (charset == null || charset.length() == 0) {
     75             throw new IllegalArgumentException("charset may not be null or empty");
     76         }
     77 
     78         try {
     79             return new String(data, offset, length, charset);
     80         } catch (UnsupportedEncodingException e) {
     81             return new String(data, offset, length);
     82         }
     83     }
     84 
     85 
     86     /**
     87      * Converts the byte array of HTTP content characters to a string. If
     88      * the specified charset is not supported, default system encoding
     89      * is used.
     90      *
     91      * @param data the byte array to be encoded
     92      * @param charset the desired character encoding
     93      * @return The result of the conversion.
     94      */
     95     public static String getString(final byte[] data, final String charset) {
     96         if (data == null) {
     97             throw new IllegalArgumentException("Parameter may not be null");
     98         }
     99         return getString(data, 0, data.length, charset);
    100     }
    101 
    102     /**
    103      * Converts the specified string to a byte array.  If the charset is not supported the
    104      * default system charset is used.
    105      *
    106      * @param data the string to be encoded
    107      * @param charset the desired character encoding
    108      * @return The resulting byte array.
    109      */
    110     public static byte[] getBytes(final String data, final String charset) {
    111 
    112         if (data == null) {
    113             throw new IllegalArgumentException("data may not be null");
    114         }
    115 
    116         if (charset == null || charset.length() == 0) {
    117             throw new IllegalArgumentException("charset may not be null or empty");
    118         }
    119 
    120         try {
    121             return data.getBytes(charset);
    122         } catch (UnsupportedEncodingException e) {
    123             return data.getBytes();
    124         }
    125     }
    126 
    127     /**
    128      * Converts the specified string to byte array of ASCII characters.
    129      *
    130      * @param data the string to be encoded
    131      * @return The string as a byte array.
    132      */
    133     public static byte[] getAsciiBytes(final String data) {
    134 
    135         if (data == null) {
    136             throw new IllegalArgumentException("Parameter may not be null");
    137         }
    138 
    139         try {
    140             return data.getBytes(HTTP.US_ASCII);
    141         } catch (UnsupportedEncodingException e) {
    142             throw new Error("HttpClient requires ASCII support");
    143         }
    144     }
    145 
    146     /**
    147      * Converts the byte array of ASCII characters to a string. This method is
    148      * to be used when decoding content of HTTP elements (such as response
    149      * headers)
    150      *
    151      * @param data the byte array to be encoded
    152      * @param offset the index of the first byte to encode
    153      * @param length the number of bytes to encode
    154      * @return The string representation of the byte array
    155      */
    156     public static String getAsciiString(final byte[] data, int offset, int length) {
    157 
    158         if (data == null) {
    159             throw new IllegalArgumentException("Parameter may not be null");
    160         }
    161 
    162         try {
    163             return new String(data, offset, length, HTTP.US_ASCII);
    164         } catch (UnsupportedEncodingException e) {
    165             throw new Error("HttpClient requires ASCII support");
    166         }
    167     }
    168 
    169     /**
    170      * Converts the byte array of ASCII characters to a string. This method is
    171      * to be used when decoding content of HTTP elements (such as response
    172      * headers)
    173      *
    174      * @param data the byte array to be encoded
    175      * @return The string representation of the byte array
    176      */
    177     public static String getAsciiString(final byte[] data) {
    178         if (data == null) {
    179             throw new IllegalArgumentException("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 }
    191