mobile/android/thirdparty/ch/boye/httpclientandroidlib/util/EncodingUtils.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/util/EncodingUtils.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,179 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + * Licensed to the Apache Software Foundation (ASF) under one
     1.7 + * or more contributor license agreements.  See the NOTICE file
     1.8 + * distributed with this work for additional information
     1.9 + * regarding copyright ownership.  The ASF licenses this file
    1.10 + * to you under the Apache License, Version 2.0 (the
    1.11 + * "License"); you may not use this file except in compliance
    1.12 + * with the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *   http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + * Unless required by applicable law or agreed to in writing,
    1.17 + * software distributed under the License is distributed on an
    1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    1.19 + * KIND, either express or implied.  See the License for the
    1.20 + * specific language governing permissions and limitations
    1.21 + * under the License.
    1.22 + * ====================================================================
    1.23 + *
    1.24 + * This software consists of voluntary contributions made by many
    1.25 + * individuals on behalf of the Apache Software Foundation.  For more
    1.26 + * information on the Apache Software Foundation, please see
    1.27 + * <http://www.apache.org/>.
    1.28 + *
    1.29 + */
    1.30 +package ch.boye.httpclientandroidlib.util;
    1.31 +
    1.32 +import java.io.UnsupportedEncodingException;
    1.33 +
    1.34 +import ch.boye.httpclientandroidlib.protocol.HTTP;
    1.35 +
    1.36 +/**
    1.37 + * The home for utility methods that handle various encoding tasks.
    1.38 + *
    1.39 + *
    1.40 + * @since 4.0
    1.41 + */
    1.42 +public final class EncodingUtils {
    1.43 +
    1.44 +    /**
    1.45 +     * Converts the byte array of HTTP content characters to a string. If
    1.46 +     * the specified charset is not supported, default system encoding
    1.47 +     * is used.
    1.48 +     *
    1.49 +     * @param data the byte array to be encoded
    1.50 +     * @param offset the index of the first byte to encode
    1.51 +     * @param length the number of bytes to encode
    1.52 +     * @param charset the desired character encoding
    1.53 +     * @return The result of the conversion.
    1.54 +     */
    1.55 +    public static String getString(
    1.56 +        final byte[] data,
    1.57 +        int offset,
    1.58 +        int length,
    1.59 +        String charset
    1.60 +    ) {
    1.61 +
    1.62 +        if (data == null) {
    1.63 +            throw new IllegalArgumentException("Parameter may not be null");
    1.64 +        }
    1.65 +
    1.66 +        if (charset == null || charset.length() == 0) {
    1.67 +            throw new IllegalArgumentException("charset may not be null or empty");
    1.68 +        }
    1.69 +
    1.70 +        try {
    1.71 +            return new String(data, offset, length, charset);
    1.72 +        } catch (UnsupportedEncodingException e) {
    1.73 +            return new String(data, offset, length);
    1.74 +        }
    1.75 +    }
    1.76 +
    1.77 +
    1.78 +    /**
    1.79 +     * Converts the byte array of HTTP content characters to a string. If
    1.80 +     * the specified charset is not supported, default system encoding
    1.81 +     * is used.
    1.82 +     *
    1.83 +     * @param data the byte array to be encoded
    1.84 +     * @param charset the desired character encoding
    1.85 +     * @return The result of the conversion.
    1.86 +     */
    1.87 +    public static String getString(final byte[] data, final String charset) {
    1.88 +        if (data == null) {
    1.89 +            throw new IllegalArgumentException("Parameter may not be null");
    1.90 +        }
    1.91 +        return getString(data, 0, data.length, charset);
    1.92 +    }
    1.93 +
    1.94 +    /**
    1.95 +     * Converts the specified string to a byte array.  If the charset is not supported the
    1.96 +     * default system charset is used.
    1.97 +     *
    1.98 +     * @param data the string to be encoded
    1.99 +     * @param charset the desired character encoding
   1.100 +     * @return The resulting byte array.
   1.101 +     */
   1.102 +    public static byte[] getBytes(final String data, final String charset) {
   1.103 +
   1.104 +        if (data == null) {
   1.105 +            throw new IllegalArgumentException("data may not be null");
   1.106 +        }
   1.107 +
   1.108 +        if (charset == null || charset.length() == 0) {
   1.109 +            throw new IllegalArgumentException("charset may not be null or empty");
   1.110 +        }
   1.111 +
   1.112 +        try {
   1.113 +            return data.getBytes(charset);
   1.114 +        } catch (UnsupportedEncodingException e) {
   1.115 +            return data.getBytes();
   1.116 +        }
   1.117 +    }
   1.118 +
   1.119 +    /**
   1.120 +     * Converts the specified string to byte array of ASCII characters.
   1.121 +     *
   1.122 +     * @param data the string to be encoded
   1.123 +     * @return The string as a byte array.
   1.124 +     */
   1.125 +    public static byte[] getAsciiBytes(final String data) {
   1.126 +
   1.127 +        if (data == null) {
   1.128 +            throw new IllegalArgumentException("Parameter may not be null");
   1.129 +        }
   1.130 +
   1.131 +        try {
   1.132 +            return data.getBytes(HTTP.US_ASCII);
   1.133 +        } catch (UnsupportedEncodingException e) {
   1.134 +            throw new Error("HttpClient requires ASCII support");
   1.135 +        }
   1.136 +    }
   1.137 +
   1.138 +    /**
   1.139 +     * Converts the byte array of ASCII characters to a string. This method is
   1.140 +     * to be used when decoding content of HTTP elements (such as response
   1.141 +     * headers)
   1.142 +     *
   1.143 +     * @param data the byte array to be encoded
   1.144 +     * @param offset the index of the first byte to encode
   1.145 +     * @param length the number of bytes to encode
   1.146 +     * @return The string representation of the byte array
   1.147 +     */
   1.148 +    public static String getAsciiString(final byte[] data, int offset, int length) {
   1.149 +
   1.150 +        if (data == null) {
   1.151 +            throw new IllegalArgumentException("Parameter may not be null");
   1.152 +        }
   1.153 +
   1.154 +        try {
   1.155 +            return new String(data, offset, length, HTTP.US_ASCII);
   1.156 +        } catch (UnsupportedEncodingException e) {
   1.157 +            throw new Error("HttpClient requires ASCII support");
   1.158 +        }
   1.159 +    }
   1.160 +
   1.161 +    /**
   1.162 +     * Converts the byte array of ASCII characters to a string. This method is
   1.163 +     * to be used when decoding content of HTTP elements (such as response
   1.164 +     * headers)
   1.165 +     *
   1.166 +     * @param data the byte array to be encoded
   1.167 +     * @return The string representation of the byte array
   1.168 +     */
   1.169 +    public static String getAsciiString(final byte[] data) {
   1.170 +        if (data == null) {
   1.171 +            throw new IllegalArgumentException("Parameter may not be null");
   1.172 +        }
   1.173 +        return getAsciiString(data, 0, data.length);
   1.174 +    }
   1.175 +
   1.176 +    /**
   1.177 +     * This class should not be instantiated.
   1.178 +     */
   1.179 +    private EncodingUtils() {
   1.180 +    }
   1.181 +
   1.182 +}

mercurial