【Java开源代码栏目提醒】:以下是网学会员为您推荐的Java开源代码-Base64.java,希望本篇文章对您学习有所帮助。
package com.bam;
/**
* 按照Base64的规范进行编码和解码;
*/
public class Base64
{
/** 如果设置为True,则进行编码 */
public final static boolean ENCODE = true;
/** 设置为false,也就是不进行解码 */
public final static boolean DECODE = false;
/** Base64规范的最大输出长度 */
private final static int MAX_LINE_LENGTH = 76;
/** 得到(=)的值 */
private final static byte EQUALS_SIGN = (byte)'=';
/** 得到(\n)的值 */
private final static byte NEW_LINE = (byte)'\n';
/** 64个有效的Base64规范的值 */
private final static byte[] ALPHABET =
{
(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)'/'
};
/**
* 将Base64规范的值转换成6-bit的值或代表其他意思的值
**/
private final static byte[] DECODABET =
{
-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 0 - 8
-5,-5, // Whitespace: Tab and Linefeed
-9,-9, // Decimal 11 - 12
-5, // Whitespace: Carriage Return
-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 14 - 26
-9,-9,-9,-9,-9, // Decimal 27 - 31
-5, // Whitespace: Space
-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 33 - 42
62, // Plus sign at decimal 43
-9,-9,-9, // Decimal 44 - 46
63, // Slash at decimal 47
52,53,54,55,56,57,58,59,60,61, // Numbers zero through nine
-9,-9,-9, // Decimal 58 - 60
-1, // Equals sign at decimal 61
-9,-9,-9, // Decimal 62 - 64
0,1,2,3,4,5,6,7,8,9,10,11,12,13, // Letters 'A' through 'N'
14,15,16,17,18,19,20,21,22,23,24,25, // Letters 'O' through 'Z'
-9,-9,-9,-9,-9,-9, // Decimal 91 - 96
26,27,28,29,30,31,32,33,34,35,36,37,38, // Letters 'a' through 'm'
39,40,41,42,43,44,45,46,47,48,49,50,51, // Letters 'n' through 'z'
-9,-9,-9,-9 // Decimal 123 - 126
/*,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 127 - 139
-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 140 - 152
-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 153 - 165
-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 166 - 178
-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 179 - 191
-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 192 - 204
-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 205 - 217
-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 218 - 230
-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 231 - 243
-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9 // Decimal 244 - 255 */
};
private final static byte BAD_ENCODING = -9; // Indicates error in encoding
private final static byte WHITE_SPACE_ENC = -5; // Indicates white space in encoding
private final static byte EQUALS_SIGN_ENC = -1; // Indicates equals sign in encoding
/** Defeats instantiation. */
private Base64(){}
/**
* main函数,用来测试Base64类;
*/
public static void main( String[] args )
{
try
{
// Test encoding/decoding byte arrays
{
byte[] bytes1 = { (byte)2,(byte)2,(byte)3,(byte)0,(byte)9 }; // My zip code
byte[] bytes2 = { (byte)99,(byte)2,(byte)2,(byte)3,(byte)0,(byte)9 };
System.out.println( "Bytes 2,2,3,0,9 as Base64: " + encodeBytes( bytes1 ) );
System.out.println( "Bytes 2,2,3,0,9 w/ offset: " + encodeBytes( bytes2, 1, bytes2.length-1 ) );
byte[] dbytes = decode( encodeBytes( bytes1 ) );
System.out.print( encodeBytes( bytes1 ) + " decoded: " );
for( int i = 0; i < dbytes.length; i++ )
System.out.print( dbytes[i] + (i<dbytes.length-1?",":"\n") );
} // end testing byte arrays
// Test Input Stream
{
// Read GIF stored in base64 form.
java.io.FileInputStream fis = null;//new java.io.FileInputStream( "test.gif.b64" );
Base64.InputStream b64is = null;//new Base64.InputStream( fis, DECODE );
byte[] bytes = new byte[0];
int b = -1;
/*
while( (b = b64is.read()) >= 0 ){
byte[] temp = new byte[ bytes.length + 1 ];
System.arraycopy( bytes,0, temp,0,bytes.length );
temp[bytes.length] = (byte)b;
bytes = temp;
} // end while: terribly inefficient way to read data
b64is.close();
*/
bytes = Base64.readFile( "test.gif.b64", false );
javax.swing.ImageIcon iicon = new javax.swing.ImageIcon( bytes );
javax.swing.JLabel jlabel = new javax.swing.JLabel( "Read from test.gif.b64", iico