⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 base64.java

📁 打印管理程序,测试完全通过.windows开发环境.
💻 JAVA
字号:
/*
    $Author: $
    $Date: $
    $Revision: $
    $NoKeywords: $
*/
package jp.co.ntl.codec;

import java.io.UnsupportedEncodingException;

public class Base64
{
    private static byte[] encMap;
    private static byte[] decMap;

    static {
        // rfc-2045: Base64 Alphabet
        byte[] map = {
            (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F',
            (byte)'G', (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L',
            (byte)'M', (byte)'N', (byte)'O', (byte)'P', (byte)'Q', (byte)'R',
            (byte)'S', (byte)'T', (byte)'U', (byte)'V', (byte)'W', (byte)'X',
            (byte)'Y', (byte)'Z',
            (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f',
            (byte)'g', (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l',
            (byte)'m', (byte)'n', (byte)'o', (byte)'p', (byte)'q', (byte)'r',
            (byte)'s', (byte)'t', (byte)'u', (byte)'v', (byte)'w', (byte)'x',
            (byte)'y', (byte)'z',
            (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5',
            (byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'+', (byte)'/' };

        encMap = map;
        decMap = new byte[128];

        for (int i=0; i<encMap.length; i++) {
            decMap[encMap[i]] = (byte)i;
        }
    }

    public static String encode(byte[] data) {
        if (data == null) {
            return null;
        }
        byte[] enc_bytes = base64Encode(data);

        String enc_str;
        try{
            enc_str = new String(enc_bytes, "ISO-8859-1");
        } catch(UnsupportedEncodingException e) {
            return null;
        }

        return enc_str;
    }

    public static byte[] decode(String data) {
        if (data == null) {
            return null;
        }
        byte[] bytes;
        try {
            bytes = data.getBytes("ISO-8859-1");
        } catch(UnsupportedEncodingException e) {
           return null;
        }
        byte[] dec_bytes = base64Decode(bytes);

        return dec_bytes;
    }

    public static byte[] base64Encode(byte[] data) {
        byte bytes[] = new byte[((data.length+2)/3)*4];
        int i, j;

        // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion
        for (i=j=0; i<data.length-2; i+=3) {
            bytes[j++] = encMap[(data[i] >>> 2) & 077];
            bytes[j++] = encMap[(data[i+1] >>> 4) & 017 | (data[i] << 4) & 077];
            bytes[j++] = encMap[(data[i+2] >>> 6) & 003 | (data[i+1] << 2) & 077];
            bytes[j++] = encMap[data[i+2] & 077];
	    }

        if (i < data.length) {
            bytes[j++] = encMap[(data[i] >>> 2) & 077];
            if (i < data.length-1) {
                bytes[j++] = encMap[(data[i+1] >>> 4) & 017 | (data[i] << 4) & 077];
                bytes[j++] = encMap[(data[i+1] << 2) & 077];
            } else {
                bytes[j++] = encMap[(data[i] << 4) & 077];
            }
	    }

	    // add padding
        for ( ; j<bytes.length; j++) {
            bytes[j] = (byte) '=';
        }

        return bytes;
    }

    public static byte[] base64Decode(byte[] data) {
        int tail = data.length;
        while (data[tail-1] == '=') {
            tail--;
        }

        byte bytes[] = new byte[tail - data.length/4];

        // ascii printable to 0-63 conversion
        for (int idx = 0; idx < data.length; idx++) {
            data[idx] = decMap[data[idx]];
        }

        // 4-byte to 3-byte conversion
        int i, j;
        for (i=j=0; j<bytes.length-2; i+=4,j+=3) {
            bytes[j]   = (byte) (((data[i] << 2) & 255) | ((data[i+1] >>> 4) & 003));
            bytes[j+1] = (byte) (((data[i+1] << 4) & 255) | ((data[i+2] >>> 2) & 017));
            bytes[j+2] = (byte) (((data[i+2] << 6) & 255) | (data[i+3] & 077));
	    }
        if (j < bytes.length) {
            bytes[j] = (byte) (((data[i] << 2) & 255) | ((data[i+1] >>> 4) & 003));
        }
        if (++j < bytes.length) {
            bytes[j] = (byte) (((data[i+1] << 4) & 255) | ((data[i+2] >>> 2) & 017));
        }

        return bytes;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -