FileDocCategorySizeDatePackage
Util.javaAPI DocAndroid 1.5 API11702Wed May 06 22:41:04 BST 2009org.apache.harmony.luni.util

Util

public final class Util extends Object

Fields Summary
private static String[]
WEEKDAYS
private static String[]
MONTHS
private static final String
defaultEncoding
Constructors Summary
Methods Summary
public static java.lang.StringconvertFromUTF8(byte[] buf, int offset, int utfSize)

        return convertUTF8WithBuf(buf, new char[utfSize], offset, utfSize);
    
public static java.lang.StringconvertUTF8WithBuf(byte[] buf, char[] out, int offset, int utfSize)

        int count = 0, s = 0, a;
        while (count < utfSize) {
            if ((out[s] = (char) buf[offset + count++]) < '\u0080")
                s++;
            else if (((a = out[s]) & 0xe0) == 0xc0) {
                if (count >= utfSize)
                    throw new UTFDataFormatException(Msg.getString("K0062",
                            count));
                // BEGIN android-changed
                int b = buf[offset + count++];
                // END android-changed
                if ((b & 0xC0) != 0x80)
                    throw new UTFDataFormatException(Msg.getString("K0062",
                            (count - 1)));
                out[s++] = (char) (((a & 0x1F) << 6) | (b & 0x3F));
            } else if ((a & 0xf0) == 0xe0) {
                if (count + 1 >= utfSize)
                    throw new UTFDataFormatException(Msg.getString("K0063",
                            (count + 1)));
                // BEGIN android-changed
                int b = buf[offset + count++];
                int c = buf[offset + count++];
                // END android-changed
                if (((b & 0xC0) != 0x80) || ((c & 0xC0) != 0x80))
                    throw new UTFDataFormatException(Msg.getString("K0064",
                            (count - 2)));
                out[s++] = (char) (((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F));
            } else {
                throw new UTFDataFormatException(Msg.getString("K0065",
                        (count - 1)));
            }
        }
        return new String(out, 0, s);
    
public static java.lang.Stringdecode(java.lang.String s, boolean convertPlus)
'%' and two following hex digit characters are converted to the equivalent byte value. All other characters are passed through unmodified. e.g. "ABC %24%25" -> "ABC $%"

param
s java.lang.String The encoded string.
return
java.lang.String The decoded version.

        if (!convertPlus && s.indexOf('%") == -1)
            return s;
        StringBuffer result = new StringBuffer(s.length());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        for (int i = 0; i < s.length();) {
            char c = s.charAt(i);
            if (convertPlus && c == '+")
                result.append(' ");
            else if (c == '%") {
                out.reset();
                do {
                    if (i + 2 >= s.length())
                        throw new IllegalArgumentException(Msg.getString(
                                "K01fe", i));
                    int d1 = Character.digit(s.charAt(i + 1), 16);
                    int d2 = Character.digit(s.charAt(i + 2), 16);
                    if (d1 == -1 || d2 == -1)
                        throw new IllegalArgumentException(Msg.getString(
                                "K01ff", s.substring(i, i + 3), String
                                        .valueOf(i)));
                    out.write((byte) ((d1 << 4) + d2));
                    i += 3;
                } while (i < s.length() && s.charAt(i) == '%");
                result.append(out.toString());
                continue;
            } else
                result.append(c);
            i++;
        }
        return result.toString();
    
public static byte[]getBytes(java.lang.String name)


     
        // BEGIN android-changed
        String encoding = System.getProperty("file.encoding");
        // END android-changed
        if (encoding != null) {
            try {
                "".getBytes(encoding);
            } catch (java.io.UnsupportedEncodingException e) {
                encoding = null;
            }
        }
        defaultEncoding = encoding;
    
        if (defaultEncoding != null) {
            try {
                return name.getBytes(defaultEncoding);
            } catch (java.io.UnsupportedEncodingException e) {
            }
        }
        return name.getBytes();
    
private static intparse(java.lang.String string, java.lang.String[] array)

        int length = string.length();
        for (int i = 0; i < array.length; i++) {
            if (string.regionMatches(true, 0, array[i], 0, length))
                return i;
        }
        return -1;
    
public static longparseDate(java.lang.String string)
Returns the millisecond value of the date and time parsed from the specified String. Many date/time formats are recognized

param
string the String to parse
return
the millisecond value parsed from the String

        int offset = 0, length = string.length(), state = 0;
        int year = -1, month = -1, date = -1;
        int hour = -1, minute = -1, second = -1;
        final int PAD = 0, LETTERS = 1, NUMBERS = 2;
        StringBuffer buffer = new StringBuffer();

        while (offset <= length) {
            char next = offset < length ? string.charAt(offset) : '\r";
            offset++;

            int nextState;
            if ((next >= 'a" && next <= 'z") || (next >= 'A" && next <= 'Z"))
                nextState = LETTERS;
            else if (next >= '0" && next <= '9")
                nextState = NUMBERS;
            else if (" ,-:\r\t".indexOf(next) == -1)
                throw new IllegalArgumentException();
            else
                nextState = PAD;

            if (state == NUMBERS && nextState != NUMBERS) {
                int digit = Integer.parseInt(buffer.toString());
                buffer.setLength(0);
                if (digit >= 70) {
                    if (year != -1
                            || (next != ' " && next != '," && next != '\r"))
                        throw new IllegalArgumentException();
                    year = digit;
                } else if (next == ':") {
                    if (hour == -1)
                        hour = digit;
                    else if (minute == -1)
                        minute = digit;
                    else
                        throw new IllegalArgumentException();
                } else if (next == ' " || next == '," || next == '-"
                        || next == '\r") {
                    if (hour != -1 && minute == -1)
                        minute = digit;
                    else if (minute != -1 && second == -1)
                        second = digit;
                    else if (date == -1)
                        date = digit;
                    else if (year == -1)
                        year = digit;
                    else
                        throw new IllegalArgumentException();
                } else if (year == -1 && month != -1 && date != -1)
                    year = digit;
                else
                    throw new IllegalArgumentException();
            } else if (state == LETTERS && nextState != LETTERS) {
                String text = buffer.toString().toUpperCase();
                buffer.setLength(0);
                if (text.length() < 3)
                    throw new IllegalArgumentException();
                if (parse(text, WEEKDAYS) != -1) {
                } else if (month == -1 && (month = parse(text, MONTHS)) != -1) {
                } else if (text.equals("GMT")) {
                } else
                    throw new IllegalArgumentException();
            }

            if (nextState == LETTERS || nextState == NUMBERS)
                buffer.append(next);
            state = nextState;
        }

        if (year != -1 && month != -1 && date != -1) {
            if (hour == -1)
                hour = 0;
            if (minute == -1)
                minute = 0;
            if (second == -1)
                second = 0;
            Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
            int current = cal.get(Calendar.YEAR) - 80;
            if (year < 100) {
                year += current / 100 * 100;
                if (year < current)
                    year += 100;
            }
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month);
            cal.set(Calendar.DATE, date);
            cal.set(Calendar.HOUR_OF_DAY, hour);
            cal.set(Calendar.MINUTE, minute);
            cal.set(Calendar.SECOND, second);
            cal.set(Calendar.MILLISECOND, 0);
            return cal.getTime().getTime();
        }
        throw new IllegalArgumentException();
    
public static java.lang.StringtoASCIILowerCase(java.lang.String s)

        int len = s.length();
        StringBuilder buffer = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = s.charAt(i);
            if ('A" <= c && c <= 'Z") {
                buffer.append((char) (c + ('a" - 'A")));
            } else {
                buffer.append(c);
            }
        }
        return buffer.toString();
    
public static java.lang.StringtoASCIIUpperCase(java.lang.String s)

        int len = s.length();
        StringBuilder buffer = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = s.charAt(i);
            if ('a" <= c && c <= 'z") {
                buffer.append((char) (c - ('a" - 'A")));
            } else {
                buffer.append(c);
            }
        }
        return buffer.toString();
    
public static java.lang.StringtoString(byte[] bytes)

        if (defaultEncoding != null) {
            try {
                return new String(bytes, 0, bytes.length, defaultEncoding);
            } catch (java.io.UnsupportedEncodingException e) {
            }
        }
        return new String(bytes, 0, bytes.length);
    
public static java.lang.StringtoString(byte[] bytes, int offset, int length)

        if (defaultEncoding != null) {
            try {
                return new String(bytes, offset, length, defaultEncoding);
            } catch (java.io.UnsupportedEncodingException e) {
            }
        }
        return new String(bytes, offset, length);