Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    public static final char[] TABLE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./"
            .toCharArray();

    public static byte[] fromb64(String str) throws NumberFormatException {
        int len = str.length();
        if (len == 0)
            throw new NumberFormatException("Empty Base64 string");

        byte[] a = new byte[len + 1];
        char c;
        int i, j;

        for (i = 0; i < len; ++i) {
            c = str.charAt(i);
            try {
                for (j = 0; c != TABLE[j]; ++j)
                    ;
            } catch (Exception e) {
                throw new NumberFormatException("Illegal Base64 character");
            }
            a[i] = (byte) j;
        }

        i = len - 1;
        j = len;
        try {
            while (true) {
                a[j] = a[i];
                if (--i < 0)
                    break;
                a[j] |= (a[i] & 3) << 6;
                --j;
                a[j] = (byte) ((a[i] & 0x3c) >>> 2);
                if (--i < 0)
                    break;
                a[j] |= (a[i] & 0xf) << 4;
                --j;
                a[j] = (byte) ((a[i] & 0x30) >>> 4);
                if (--i < 0)
                    break;
                a[j] |= (a[i] << 2);

                // Nasty, evil bug in Microsloth's Java interpreter under
                // Netscape:  The following three lines of code are supposed
                // to be equivalent, but under the Windows NT VM (Netscape3.0)
                // using either of the two commented statements would cause
                // the zero to be placed in a[j] *before* decrementing j.
                // Weeeeird.
                a[j - 1] = 0;
                --j;
                // a[--j] = 0;
                // --j; a[j] = 0;

                if (--i < 0)
                    break;
            }
        } catch (Exception e) {
        }

        try {
            while (a[j] == 0)
                ++j;
        } catch (Exception e) {
            return new byte[1];
        }

        byte[] result = new byte[len - j + 1];
        System.arraycopy(a, j, result, 0, len - j + 1);
        //for(i = 0; i < len - j + 1; ++i)
        //      result[i] = a[i + j];
        return result;
    }
}