Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import android.content.res.Resources;

public class Main {
    private static final int Big5TPAD = 33088;
    private static final short Big5TSize = 32190;
    private static char big5_to_ucs[];

    public static String decode(char[] cl, String encoding, Resources res) {

        StringBuffer buf = new StringBuffer();
        if (!encoding.equalsIgnoreCase("big5"))
            return decode(cl, encoding);
        /* UAO Patch
         *  We use our own convert table instead of the system's.
         *  Maybe we should implement Charset instead, though it seems complicated.
         */
        try {
            if (big5_to_ucs == null)
                loadUAO(res);
            for (int i = 0; i < cl.length; i++) {
                if (cl[i] >= 0x81 && cl[i] <= 0xfe && i < cl.length - 1 && cl[i + 1] >= 0x40 && cl[i + 1] <= 0xfe) { // Big5 Range                  
                    buf.append(big5_to_ucs[(cl[i] << 8 | cl[i + 1]) - Big5TPAD]);
                    i++;
                } else
                    buf.append(cl[i]);
            }
        } catch (Exception e) {
            return decode(cl, encoding); //Give up UAO
        }
        return buf.toString();
    }

    public static String decode(char[] cl, String encoding) {
        char[] cm = cl;
        byte[] b = new byte[cm.length];
        for (int i = 0; i < cm.length; i++) {
            char _c = cm[i];

            if (_c > 256) {
                byte _b = (byte) _c;
                _c = (char) (_b + 2009);
            }

            b[i] = (byte) _c;
        }
        String s = "";
        try {
            s = new String(b, encoding);
        } catch (UnsupportedEncodingException e) {
        }

        return s;
    }

    private static void loadUAO(Resources res) throws Exception {
        InputStream in = res.getAssets().open("big5uao");
        big5_to_ucs = new char[Big5TSize];
        byte r[] = new byte[2];
        for (short i = 0; i < big5_to_ucs.length; i++) {
            if (in.read(r) != -1)
                /* albb.100618: byte is signed, however char is not */
                big5_to_ucs[i] = (char) (((char) r[0] & 0xFF) << 8 | (char) r[1] & 0xFF);
            else
                return;
        }
    }
}