FileDocCategorySizeDatePackage
EncodingUtils.javaAPI DocAndroid 1.5 API6204Wed May 06 22:41:10 BST 2009org.apache.http.util

EncodingUtils

public final class EncodingUtils extends Object
The home for utility methods that handle various encoding tasks.
author
Michael Becke
author
Oleg Kalnichevski
since
4.0

Fields Summary
Constructors Summary
private EncodingUtils()
This class should not be instantiated.

    
Methods Summary
public static byte[]getAsciiBytes(java.lang.String data)
Converts the specified string to byte array of ASCII characters.

param
data the string to be encoded
return
The string as a byte array.


        if (data == null) {
            throw new IllegalArgumentException("Parameter may not be null");
        }

        try {
            return data.getBytes(HTTP.US_ASCII);
        } catch (UnsupportedEncodingException e) {
            throw new Error("HttpClient requires ASCII support");
        }
    
public static java.lang.StringgetAsciiString(byte[] data, int offset, int length)
Converts the byte array of ASCII characters to a string. This method is to be used when decoding content of HTTP elements (such as response headers)

param
data the byte array to be encoded
param
offset the index of the first byte to encode
param
length the number of bytes to encode
return
The string representation of the byte array


        if (data == null) {
            throw new IllegalArgumentException("Parameter may not be null");
        }

        try {
            return new String(data, offset, length, HTTP.US_ASCII);
        } catch (UnsupportedEncodingException e) {
            throw new Error("HttpClient requires ASCII support");
        }
    
public static java.lang.StringgetAsciiString(byte[] data)
Converts the byte array of ASCII characters to a string. This method is to be used when decoding content of HTTP elements (such as response headers)

param
data the byte array to be encoded
return
The string representation of the byte array

        if (data == null) {
            throw new IllegalArgumentException("Parameter may not be null");
        }
        return getAsciiString(data, 0, data.length);
    
public static byte[]getBytes(java.lang.String data, java.lang.String charset)
Converts the specified string to a byte array. If the charset is not supported the default system charset is used.

param
data the string to be encoded
param
charset the desired character encoding
return
The resulting byte array.


        if (data == null) {
            throw new IllegalArgumentException("data may not be null");
        }

        if (charset == null || charset.length() == 0) {
            throw new IllegalArgumentException("charset may not be null or empty");
        }

        try {
            return data.getBytes(charset);
        } catch (UnsupportedEncodingException e) {
            return data.getBytes();
        }
    
public static java.lang.StringgetString(byte[] data, int offset, int length, java.lang.String charset)
Converts the byte array of HTTP content characters to a string. If the specified charset is not supported, default system encoding is used.

param
data the byte array to be encoded
param
offset the index of the first byte to encode
param
length the number of bytes to encode
param
charset the desired character encoding
return
The result of the conversion.


        if (data == null) {
            throw new IllegalArgumentException("Parameter may not be null");
        }

        if (charset == null || charset.length() == 0) {
            throw new IllegalArgumentException("charset may not be null or empty");
        }

        try {
            return new String(data, offset, length, charset);
        } catch (UnsupportedEncodingException e) {
            return new String(data, offset, length);
        }
    
public static java.lang.StringgetString(byte[] data, java.lang.String charset)
Converts the byte array of HTTP content characters to a string. If the specified charset is not supported, default system encoding is used.

param
data the byte array to be encoded
param
charset the desired character encoding
return
The result of the conversion.

        if (data == null) {
            throw new IllegalArgumentException("Parameter may not be null");
        }
        return getString(data, 0, data.length, charset);