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