michael@0: /* michael@0: * ==================================================================== michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: package ch.boye.httpclientandroidlib.util; michael@0: michael@0: import java.io.UnsupportedEncodingException; michael@0: michael@0: import ch.boye.httpclientandroidlib.protocol.HTTP; michael@0: michael@0: /** michael@0: * The home for utility methods that handle various encoding tasks. michael@0: * michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: public final class EncodingUtils { michael@0: michael@0: /** michael@0: * Converts the byte array of HTTP content characters to a string. If michael@0: * the specified charset is not supported, default system encoding michael@0: * is used. michael@0: * michael@0: * @param data the byte array to be encoded michael@0: * @param offset the index of the first byte to encode michael@0: * @param length the number of bytes to encode michael@0: * @param charset the desired character encoding michael@0: * @return The result of the conversion. michael@0: */ michael@0: public static String getString( michael@0: final byte[] data, michael@0: int offset, michael@0: int length, michael@0: String charset michael@0: ) { michael@0: michael@0: if (data == null) { michael@0: throw new IllegalArgumentException("Parameter may not be null"); michael@0: } michael@0: michael@0: if (charset == null || charset.length() == 0) { michael@0: throw new IllegalArgumentException("charset may not be null or empty"); michael@0: } michael@0: michael@0: try { michael@0: return new String(data, offset, length, charset); michael@0: } catch (UnsupportedEncodingException e) { michael@0: return new String(data, offset, length); michael@0: } michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Converts the byte array of HTTP content characters to a string. If michael@0: * the specified charset is not supported, default system encoding michael@0: * is used. michael@0: * michael@0: * @param data the byte array to be encoded michael@0: * @param charset the desired character encoding michael@0: * @return The result of the conversion. michael@0: */ michael@0: public static String getString(final byte[] data, final String charset) { michael@0: if (data == null) { michael@0: throw new IllegalArgumentException("Parameter may not be null"); michael@0: } michael@0: return getString(data, 0, data.length, charset); michael@0: } michael@0: michael@0: /** michael@0: * Converts the specified string to a byte array. If the charset is not supported the michael@0: * default system charset is used. michael@0: * michael@0: * @param data the string to be encoded michael@0: * @param charset the desired character encoding michael@0: * @return The resulting byte array. michael@0: */ michael@0: public static byte[] getBytes(final String data, final String charset) { michael@0: michael@0: if (data == null) { michael@0: throw new IllegalArgumentException("data may not be null"); michael@0: } michael@0: michael@0: if (charset == null || charset.length() == 0) { michael@0: throw new IllegalArgumentException("charset may not be null or empty"); michael@0: } michael@0: michael@0: try { michael@0: return data.getBytes(charset); michael@0: } catch (UnsupportedEncodingException e) { michael@0: return data.getBytes(); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Converts the specified string to byte array of ASCII characters. michael@0: * michael@0: * @param data the string to be encoded michael@0: * @return The string as a byte array. michael@0: */ michael@0: public static byte[] getAsciiBytes(final String data) { michael@0: michael@0: if (data == null) { michael@0: throw new IllegalArgumentException("Parameter may not be null"); michael@0: } michael@0: michael@0: try { michael@0: return data.getBytes(HTTP.US_ASCII); michael@0: } catch (UnsupportedEncodingException e) { michael@0: throw new Error("HttpClient requires ASCII support"); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Converts the byte array of ASCII characters to a string. This method is michael@0: * to be used when decoding content of HTTP elements (such as response michael@0: * headers) michael@0: * michael@0: * @param data the byte array to be encoded michael@0: * @param offset the index of the first byte to encode michael@0: * @param length the number of bytes to encode michael@0: * @return The string representation of the byte array michael@0: */ michael@0: public static String getAsciiString(final byte[] data, int offset, int length) { michael@0: michael@0: if (data == null) { michael@0: throw new IllegalArgumentException("Parameter may not be null"); michael@0: } michael@0: michael@0: try { michael@0: return new String(data, offset, length, HTTP.US_ASCII); michael@0: } catch (UnsupportedEncodingException e) { michael@0: throw new Error("HttpClient requires ASCII support"); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Converts the byte array of ASCII characters to a string. This method is michael@0: * to be used when decoding content of HTTP elements (such as response michael@0: * headers) michael@0: * michael@0: * @param data the byte array to be encoded michael@0: * @return The string representation of the byte array michael@0: */ michael@0: public static String getAsciiString(final byte[] data) { michael@0: if (data == null) { michael@0: throw new IllegalArgumentException("Parameter may not be null"); michael@0: } michael@0: return getAsciiString(data, 0, data.length); michael@0: } michael@0: michael@0: /** michael@0: * This class should not be instantiated. michael@0: */ michael@0: private EncodingUtils() { michael@0: } michael@0: michael@0: }