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.util.*;
import java.io.*;
import java.net.*;

public class Main {
    public static String getUrlStatus(String ss) {
        String st = "";
        try {
            st = URLDecoder.decode(ss.replaceAll("\\+", "-000000000000000-"), "utf-8")
                    .replaceAll("-000000000000000-", "+");
        } catch (UnsupportedEncodingException e) {
            Log.d("decode", ss, e);
        }
        return st.substring("file:".length(), st.length()).replaceAll("///", "/").replaceAll("//", "/");
    }

    public static String replaceAll(String s, String as[], String as1[]) {
        // long millis = System.currentTimeMillis();
        StringBuilder sb = new StringBuilder();
        for (int k = 0; k < as.length; k++) {
            if (as[k].length() > 0) {
                int i = 0;
                sb.setLength(0);
                int j;
                while ((j = s.indexOf(as[k], i)) >= 0) {
                    sb.append(s, i, j);
                    sb.append(as1[k]);
                    // LOGGER.info("replaced " + as[k] + " = " + as1[k]);
                    i = j + as[k].length();
                }
                sb.append(s, i, s.length());
                s = sb.toString();
            }
        }
        // LOGGER.info("replaced result: " + s);
        return s;
    }

    public static StringBuilder replaceAll(String s, String s1, String s2) {
        StringBuilder sb = new StringBuilder(s.length());
        if (s1.length() > 0) {
            int i = 0;
            int j;
            while ((j = s.indexOf(s1, i)) >= 0) {
                sb.append(s, i, j);
                sb.append(s2);
                i = j + s1.length();
            }
            sb.append(s, i, s.length());
            return sb;
        } else {
            return new StringBuilder(s);
        }
    }

    public static StringBuilder replaceAll(String s, StringBuilder sb, String s1, String s2) {
        int s1Length = s1.length();
        if (s1Length > 0) {
            int i = 0;
            int j;
            while ((j = s.indexOf(s1, i)) >= 0) {
                sb.append(s, i, j);
                sb.append(s2);
                i = j + s1Length;
            }
            sb.append(s, i, s.length());
            return sb;
        } else {
            return sb.append(s);
        }
    }

    public static void replaceAll(CharSequence s, int start, int end, StringBuilder sb2, String[] s10,
            String[] s20) {
        replaceAll(s.toString(), start, end, sb2, s10, s20);
    }

    public static void replaceAll(String s, int start, int end, StringBuilder sb2, String[] s10, String[] s20) {
        s = s.substring(start, end);
        for (int k = 0; k < s10.length; k++) {
            String s1 = s10[k];
            String s2 = s20[k];
            int s1Length = s1.length();
            if (s1Length > 0) {
                int i = 0;
                int j;
                StringBuilder sb = new StringBuilder();
                end = s.length();
                while (((j = s.indexOf(s1, i)) >= 0) && j < end) {
                    sb.append(s, i, j);
                    sb.append(s2);
                    i = j + s1Length;
                }
                sb.append(s, i, end);
                s = sb.toString();
            }
        }
        sb2.append(s);
    }

    public static StringBuilder replaceAll(String s, int start, int end, StringBuilder sb, String s1, String s2) {
        int s1Length = s1.length();
        if (s1Length > 0) {
            int i = start;
            int j;
            while (((j = s.indexOf(s1, i)) >= 0) && j < end) {
                sb.append(s, i, j);
                sb.append(s2);
                i = j + s1Length;
            }
            sb.append(s, i, end);
            return sb;
        } else {
            return sb.append(s);
        }
    }

    public static StringBuilder replaceAll(StringBuilder s, int start, int end, StringBuilder sb, String s1,
            String s2) {
        int s1Length = s1.length();
        if (s1Length > 0) {
            int i = start;
            int j;
            while (((j = s.indexOf(s1, i)) >= 0) && j < end) {
                sb.append(s, i, j);
                sb.append(s2);
                i = j + s1Length;
            }
            sb.append(s, i, end);
            return sb;
        } else {
            return sb.append(s);
        }
    }

    public static StringBuilder replaceAll(String sourceCase, String sourceLower, int start, int end,
            StringBuilder sb, String pattern, String tagStart, String tagEnd) {
        Log.d("sourceCase, sourceLower, start, end", sourceCase.length() + ", " + sourceLower.length() + "," + start
                + "," + end + pattern + "," + tagStart + "," + tagEnd);
        int patternLength = pattern.length();
        if (patternLength > 0) {
            int i = start;
            int j;
            while (((j = sourceLower.indexOf(pattern, i)) >= 0) && ((j + patternLength) <= end)) {
                sb.append(sourceCase, i, j);
                sb.append(tagStart);
                i = j + patternLength;
                sb.append(sourceCase, j, i);
                sb.append(tagEnd);
            }
            Log.d("sourceCase, i, end", sourceCase.length() + ", " + i + ", " + end);
            sb.append(sourceCase, i, end);
            return sb;
        } else {
            return sb.append(sourceCase);
        }
    }

    public static StringBuilder replaceAll(StringBuilder sourceCase, StringBuilder sourceLower, int start, int end,
            StringBuilder sb, String pattern, String tagStart, String tagEnd) {
        Log.d("sourceCase, sourceLower, start, end", sourceCase.length() + ", " + sourceLower.length() + "," + start
                + "," + end + pattern + "," + tagStart + "," + tagEnd);
        int patternLength = pattern.length();
        if (patternLength > 0) {
            int i = start;
            int j;
            while (((j = sourceLower.indexOf(pattern, i)) >= 0) && ((j + patternLength) <= end)) {
                sb.append(sourceCase, i, j);
                sb.append(tagStart);
                i = j + patternLength;
                sb.append(sourceCase, j, i);
                sb.append(tagEnd);
            }
            Log.d("sourceCase, i, end", sourceCase.length() + ", " + i + ", " + end);
            sb.append(sourceCase, i, end);
            return sb;
        } else {
            return sb.append(sourceCase);
        }
    }

    public static byte[] decode(byte[] url) throws IllegalArgumentException {
        if (url.length == 0) {
            return new byte[0];
        }

        // Create a new byte array with the same length to ensure capacity
        byte[] tempData = new byte[url.length];

        int tempCount = 0;
        for (int i = 0; i < url.length; i++) {
            byte b = url[i];
            if (b == '%') {
                if (url.length - i > 2) {
                    b = (byte) (parseHex(url[i + 1]) * 16 + parseHex(url[i + 2]));
                    i += 2;
                } else {
                    throw new IllegalArgumentException("Invalid format");
                }
            }
            tempData[tempCount++] = b;
        }
        byte[] retData = new byte[tempCount];
        System.arraycopy(tempData, 0, retData, 0, tempCount);
        return retData;
    }

    private static int parseHex(byte b) {
        if (b >= '0' && b <= '9')
            return (b - '0');
        if (b >= 'A' && b <= 'F')
            return (b - 'A' + 10);
        if (b >= 'a' && b <= 'f')
            return (b - 'a' + 10);

        throw new IllegalArgumentException("Invalid hex char '" + b + "'");
    }
}