DBMNG数据库管理与应用

独立思考能力,对于从事科学研究或其他任何工作,都是十分必要的。
当前位置:首页 > 经验分享 > Java组件

针对JDK7的自定义base64解码Base64Decoder

jdk8开始已经含有base64编解码方法。这里是针对jdk7的实现,使用方法:Base64Decoder.decode(String str);

01package mybase64.decrypt;
02 
03import java.io.ByteArrayInputStream;
04import java.io.ByteArrayOutputStream;
05import java.io.FilterInputStream;
06import java.io.IOException;
07import java.io.InputStream;
08import java.io.UnsupportedEncodingException;
09 
10 
11class Base64Decoder extends FilterInputStream {
12 
13    private static final char[] chars = { 'A''B''C''D''E''F''G',
14            'H''I''J''K''L''M''N''O''P''Q''R''S''T',
15            'U''V''W''X''Y''Z''a''b''c''d''e''f''g',
16            'h''i''j''k''l''m''n''o''p''q''r''s''t',
17            'u''v''w''x''y''z''0''1''2''3''4''5''6',
18            '7''8''9''+''/' };
19 
20    // A mapping between char values and six-bit integers
21    private static final int[] ints = new int[128];
22    static {
23        for (int i = 0; i < 64; i++) {
24            ints[chars[i]] = i;
25        }
26    }
27 
28    private int charCount;
29    private int carryOver;
30 
31    /***
32     * Constructs a new Base64 decoder that reads input from the given
33     * InputStream.
34     *
35     * @param in
36     *            the input stream
37     */
38    public Base64Decoder(InputStream in) {
39        super(in);
40    }
41 
42    /***
43     * Returns the next decoded character from the stream, or -1 if end of
44     * stream was reached.
45     *
46     * @return the decoded character, or -1 if the end of the input stream is
47     *         reached
48     * @exception IOException
49     *                if an I/O error occurs
50     */
51    public int read() throws IOException {
52        // Read the next non-whitespace character
53        int x;
54        do {
55            x = in.read();
56            if (x == -1) {
57                return -1;
58            }
59        while (Character.isWhitespace((char) x));
60        charCount++;
61 
62        // The '=' sign is just padding
63        if (x == '=') {
64            return -1// effective end of stream
65        }
66 
67        // Convert from raw form to 6-bit form
68        x = ints[x];
69 
70        // Calculate which character we're decoding now
71        int mode = (charCount - 1) % 4;
72 
73        // First char save all six bits, go for another
74        if (mode == 0) {
75            carryOver = x & 63;
76            return read();
77        }
78        // Second char use previous six bits and first two new bits,
79        // save last four bits
80        else if (mode == 1) {
81            int decoded = ((carryOver << 2="">> 4)) & 255;
82            carryOver = x & 15;
83            return decoded;
84        }
85        // Third char use previous four bits and first four new bits,
86        // save last two bits
87        else if (mode == 2) {
88            int decoded = ((carryOver << 4="">> 2)) & 255;
89            carryOver = x & 3;
90            return decoded;
91        }
92        // Fourth char use previous two bits and all six new bits
93        else if (mode == 3) {
94            int decoded = ((carryOver << 6="">
本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2025 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号