FileDocCategorySizeDatePackage
EncodedStringValue.javaAPI DocAndroid 5.1 API8686Thu Mar 12 22:22:54 GMT 2015com.google.android.mms.pdu

EncodedStringValue

public class EncodedStringValue extends Object implements Cloneable
Encoded-string-value = Text-string | Value-length Char-set Text-string

Fields Summary
private static final String
TAG
private static final boolean
DEBUG
private static final boolean
LOCAL_LOGV
private int
mCharacterSet
The Char-set value.
private byte[]
mData
The Text-string value.
Constructors Summary
public EncodedStringValue(int charset, byte[] data)
Constructor.

param
charset the Char-set value
param
data the Text-string value
throws
NullPointerException if Text-string value is null.


                           
         
        // TODO: CharSet needs to be validated against MIBEnum.
        if(null == data) {
            throw new NullPointerException("EncodedStringValue: Text-string is null.");
        }

        mCharacterSet = charset;
        mData = new byte[data.length];
        System.arraycopy(data, 0, mData, 0, data.length);
    
public EncodedStringValue(byte[] data)
Constructor.

param
data the Text-string value
throws
NullPointerException if Text-string value is null.

        this(CharacterSets.DEFAULT_CHARSET, data);
    
public EncodedStringValue(String data)

        try {
            mData = data.getBytes(CharacterSets.DEFAULT_CHARSET_NAME);
            mCharacterSet = CharacterSets.DEFAULT_CHARSET;
        } catch (UnsupportedEncodingException e) {
            Log.e(TAG, "Default encoding must be supported.", e);
        }
    
Methods Summary
public voidappendTextString(byte[] textString)
Append to Text-string.

param
textString the textString to append
throws
NullPointerException if the text String is null or an IOException occured.

        if(null == textString) {
            throw new NullPointerException("Text-string is null.");
        }

        if(null == mData) {
            mData = new byte[textString.length];
            System.arraycopy(textString, 0, mData, 0, textString.length);
        } else {
            ByteArrayOutputStream newTextString = new ByteArrayOutputStream();
            try {
                newTextString.write(mData);
                newTextString.write(textString);
            } catch (IOException e) {
                e.printStackTrace();
                throw new NullPointerException(
                        "appendTextString: failed when write a new Text-string");
            }

            mData = newTextString.toByteArray();
        }
    
public java.lang.Objectclone()

        super.clone();
        int len = mData.length;
        byte[] dstBytes = new byte[len];
        System.arraycopy(mData, 0, dstBytes, 0, len);

        try {
            return new EncodedStringValue(mCharacterSet, dstBytes);
        } catch (Exception e) {
            Log.e(TAG, "failed to clone an EncodedStringValue: " + this);
            e.printStackTrace();
            throw new CloneNotSupportedException(e.getMessage());
        }
    
public static java.lang.Stringconcat(com.google.android.mms.pdu.EncodedStringValue[] addr)
Concatenate an EncodedStringValue[] into a single String.

        StringBuilder sb = new StringBuilder();
        int maxIndex = addr.length - 1;
        for (int i = 0; i <= maxIndex; i++) {
            sb.append(addr[i].getString());
            if (i < maxIndex) {
                sb.append(";");
            }
        }

        return sb.toString();
    
public static com.google.android.mms.pdu.EncodedStringValuecopy(com.google.android.mms.pdu.EncodedStringValue value)

        if (value == null) {
            return null;
        }

        return new EncodedStringValue(value.mCharacterSet, value.mData);
    
public static com.google.android.mms.pdu.EncodedStringValue[]encodeStrings(java.lang.String[] array)

        int count = array.length;
        if (count > 0) {
            EncodedStringValue[] encodedArray = new EncodedStringValue[count];
            for (int i = 0; i < count; i++) {
                encodedArray[i] = new EncodedStringValue(array[i]);
            }
            return encodedArray;
        }
        return null;
    
public static com.google.android.mms.pdu.EncodedStringValue[]extract(java.lang.String src)
Extract an EncodedStringValue[] from a given String.

        String[] values = src.split(";");

        ArrayList<EncodedStringValue> list = new ArrayList<EncodedStringValue>();
        for (int i = 0; i < values.length; i++) {
            if (values[i].length() > 0) {
                list.add(new EncodedStringValue(values[i]));
            }
        }

        int len = list.size();
        if (len > 0) {
            return list.toArray(new EncodedStringValue[len]);
        } else {
            return null;
        }
    
public intgetCharacterSet()
Get Char-set value.

return
the value

        return mCharacterSet;
    
public java.lang.StringgetString()
Convert this object to a {@link java.lang.String}. If the encoding of the EncodedStringValue is null or unsupported, it will be treated as iso-8859-1 encoding.

return
The decoded String.

        if (CharacterSets.ANY_CHARSET == mCharacterSet) {
            return new String(mData); // system default encoding.
        } else {
            try {
                String name = CharacterSets.getMimeName(mCharacterSet);
                return new String(mData, name);
            } catch (UnsupportedEncodingException e) {
            	if (LOCAL_LOGV) {
            		Log.v(TAG, e.getMessage(), e);
            	}
            	try {
                    return new String(mData, CharacterSets.MIMENAME_ISO_8859_1);
                } catch (UnsupportedEncodingException _) {
                    return new String(mData); // system default encoding.
                }
            }
        }
    
public byte[]getTextString()
Get Text-string value.

return
the value

        byte[] byteArray = new byte[mData.length];

        System.arraycopy(mData, 0, byteArray, 0, mData.length);
        return byteArray;
    
public voidsetCharacterSet(int charset)
Set Char-set value.

param
charset the Char-set value

        // TODO: CharSet needs to be validated against MIBEnum.
        mCharacterSet = charset;
    
public voidsetTextString(byte[] textString)
Set Text-string value.

param
textString the Text-string value
throws
NullPointerException if Text-string value is null.

        if(null == textString) {
            throw new NullPointerException("EncodedStringValue: Text-string is null.");
        }

        mData = new byte[textString.length];
        System.arraycopy(textString, 0, mData, 0, textString.length);
    
public com.google.android.mms.pdu.EncodedStringValue[]split(java.lang.String pattern)
Split this encoded string around matches of the given pattern.

param
pattern the delimiting pattern
return
the array of encoded strings computed by splitting this encoded string around matches of the given pattern

        String[] temp = getString().split(pattern);
        EncodedStringValue[] ret = new EncodedStringValue[temp.length];
        for (int i = 0; i < ret.length; ++i) {
            try {
                ret[i] = new EncodedStringValue(mCharacterSet,
                        temp[i].getBytes());
            } catch (NullPointerException _) {
                // Can't arrive here
                return null;
            }
        }
        return ret;