簡述
Base64常用于在通常處理文本數(shù)據(jù)的場合,表示、傳輸、存儲(chǔ)一些二進(jìn)制數(shù)據(jù)。
| flag | meaning |
|---|---|
| DEFAULT | Encoder/Decoder flag,RFC 2045 |
| NO_PADDING | Encoder flag bit to omit the padding '=' characters at the end of the output (if any). |
| NO_WRAP | Encoder flag bit to omit all line terminators(i.e., the output will be on one long line). |
| CRLF | Encoder flag bit to indicate lines should be terminated with a CRLF pair instead of just an LF. Has no effect if NO_WRAP is specified as well. |
| URL_SAFE | Encoder/decoder flag bit to indicate using the "URL and filename safe" variant of Base64 (see RFC 3548 section 4) where - and _ are used in place of + and*. |
| NO_CLOSE | Flag to pass to Base64OutputStream to indicate that it should not close the output stream it is wrapping when it itself is closed. |
Base64編碼提供以下幾種flag:
| flag | meaning |
|---|---|
| DEFAULT | Encoder/Decoder flag,RFC 2045 |
| NO_PADDING | Encoder flag bit to omit the padding '=' characters at the end of the output (if any). |
| NO_WRAP | Encoder flag bit to omit all line terminators(i.e., the output will be on one long line). |
| CRLF | Encoder flag bit to indicate lines should be terminated with a CRLF pair instead of just an LF. Has no effect if NO_WRAP is specified as well. |
| URL_SAFE | Encoder/decoder flag bit to indicate using the "URL and filename safe" variant of Base64 (see RFC 3548 section 4) where - and _ are used in place of + and*. |
| NO_CLOSE | Flag to pass to Base64OutputStream to indicate that it should not close the output stream it is wrapping when it itself is closed. |
在Base64中的可打印字符包括字母A-Z、a-z、數(shù)字0-9,這樣共有62個(gè)字符,最后兩個(gè)可打印符號(hào)在不同的系統(tǒng)中而不同。
轉(zhuǎn)換的時(shí)候,將三個(gè)byte的數(shù)據(jù),先后放入一個(gè)24bit的緩沖區(qū)中,先來的byte占高位。數(shù)據(jù)不足3byte的話,于緩沖器中剩下的bit用0補(bǔ)足。然后,每次取出6(因?yàn)?6=64)個(gè)bit,按照其值選擇ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/中的字符作為編碼后的輸出。不斷進(jìn)行,直到全部輸入數(shù)據(jù)轉(zhuǎn)換完成。
當(dāng)原數(shù)據(jù)長度不是3的整數(shù)倍時(shí), 如果最后剩下一個(gè)輸入數(shù)據(jù),在編碼結(jié)果后加2個(gè)“=”;如果最后剩下兩個(gè)輸入數(shù)據(jù),編碼結(jié)果后加1個(gè)“=”;如果沒有剩下任何數(shù)據(jù),就什么都不要加

因?yàn)閁RL編碼器會(huì)把標(biāo)準(zhǔn)Base64中的“/”和“+”字符變?yōu)樾稳纭?XX”的形式,而這些“%”號(hào)在存入數(shù)據(jù)庫時(shí)還需要再進(jìn)行轉(zhuǎn)換,因?yàn)锳NSI SQL中已將“%”號(hào)用作通配符。
為解決此問題,可采用一種用于URL的改進(jìn)Base64編碼,它不在末尾填充'='號(hào),并將標(biāo)準(zhǔn)Base64中的“+”和“/”分別改成了“-”和“_”,這樣就免去了在URL編解碼和數(shù)據(jù)庫存儲(chǔ)時(shí)所要作的轉(zhuǎn)換,避免了編碼信息長度在此過程中的增加,并統(tǒng)一了數(shù)據(jù)庫、表單等處對(duì)象標(biāo)識(shí)符的格式。