Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.net.Uri;
import android.text.Html;

public class Main {
    private final static int NUM_LEN = 8;
    private final static int FLAG_NULL = -1;
    private final static int FLAG_SPANNED = -3;

    private static Uri uriFromString(StringBuilder s) {
        String str = toString(extract(s));
        Uri uri = str == null ? null : Uri.parse(str);
        return uri;
    }

    /**
     * toString method for values include null
     * @param cs
     * @return the string
     */
    public static String toString(CharSequence cs) {
        return cs == null ? null : cs.toString();
    }

    private static CharSequence extract(StringBuilder data) {
        return extract(data, false);
    }

    private static CharSequence extract(StringBuilder data, boolean isSpanned) {
        CharSequence len = cut(data, 0, NUM_LEN);
        int l = intFromHexString(len);
        CharSequence ret = null;
        switch (l) {
        case FLAG_NULL:
            ret = null;
            break;
        case FLAG_SPANNED:
            ret = extract(data, true);
            break;
        default:
            ret = cut(data, 0, l);
            if (isSpanned) {
                ret = Html.fromHtml(ret.toString());
            }
        }
        return ret;
    }

    private static CharSequence cut(StringBuilder sb, int start, int end) {
        CharSequence cs = sb.subSequence(start, end);
        sb.delete(start, end);
        return cs;
    }

    /**
     * Convert the hex string to the integer value.
     * @param str
     * @return the integer value for the specified hex string
     */
    public static int intFromHexString(CharSequence str) {
        int n = 0;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            int v = c <= '9' ? c - '0' : c - 'a' + 10;
            n <<= 4;
            n |= (v & 0x0f);
        }
        return n;
    }
}