📄 105d06f36c36001b19ca9d8cdf62cf01
字号:
package com.bam;
/**
* 对base64进行编码和解码;
*/
public class Base64
{
/** 是否编码 */
public final static boolean ENCODE = true;
/** 是否解码 */
public final static boolean DECODE = false;
/** 最大输出 */
private final static int MAX_LINE_LENGTH = 76;
/** (=)的byte值 */
private final static byte EQUALS_SIGN = (byte)'=';
/** 换行符(\n)的byte值 */
private final static byte NEW_LINE = (byte)'\n';
/** 有效的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(){}
/**
* 用以测试该类的可用性
*/
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", iicon,0 );
javax.swing.JFrame jframe = new javax.swing.JFrame();
jframe.getContentPane().add( jlabel );
jframe.pack();
jframe.show();
// Write raw bytes to file
java.io.FileOutputStream fos = new java.io.FileOutputStream( "test.gif_out" );
fos.write( bytes );
fos.close();
// Read raw bytes and encode
fis = new java.io.FileInputStream( "test.gif_out" );
b64is = new Base64.InputStream( fis, ENCODE );
byte[] ebytes = new byte[0];
b = -1;
while( (b = b64is.read()) >= 0 ){
byte[] temp = new byte[ ebytes.length + 1 ];
System.arraycopy( ebytes,0, temp,0,ebytes.length );
temp[ebytes.length] = (byte)b;
ebytes = temp;
} // end while: terribly inefficient way to read data
b64is.close();
String s = new String( ebytes );
javax.swing.JTextArea jta = new javax.swing.JTextArea( s );
javax.swing.JScrollPane jsp = new javax.swing.JScrollPane( jta );
jframe = new javax.swing.JFrame();
jframe.setTitle( "Read from test.gif_out" );
jframe.getContentPane().add( jsp );
jframe.pack();
jframe.show();
// Write encoded bytes to file
fos = new java.io.FileOutputStream( "test.gif.b64_out" );
fos.write( ebytes );
// Read GIF stored in base64 form.
fis = new java.io.FileInputStream( "test.gif.b64_out" );
b64is = new Base64.InputStream( fis, DECODE );
byte[] edbytes = new byte[0];
b = -1;
while( (b = b64is.read()) >= 0 ){
byte[] temp = new byte[ edbytes.length + 1 ];
System.arraycopy( edbytes,0, temp,0,edbytes.length );
temp[edbytes.length] = (byte)b;
edbytes = temp;
} // end while: terribly inefficient way to read data
b64is.close();
iicon = new javax.swing.ImageIcon( edbytes );
jlabel = new javax.swing.JLabel( "Read from test.gif.b64_out", iicon,0 );
jframe = new javax.swing.JFrame();
jframe.getContentPane().add( jlabel );
jframe.pack();
jframe.show();
} // end: Test Input Stream
// Test Output Stream
{
// Read raw bytes
java.io.FileInputStream fis = new java.io.FileInputStream( "test.gif_out" );
byte[] rbytes = new byte[0];
int b = -1;
while( (b = fis.read()) >= 0 ){
byte[] temp = new byte[ rbytes.length + 1 ];
System.arraycopy( rbytes,0, temp,0,rbytes.length );
temp[rbytes.length] = (byte)b;
rbytes = temp;
} // end while: terribly inefficient way to read data
fis.close();
// Write raw bytes to encoded file
java.io.FileOutputStream fos = null;//new java.io.FileOutputStream("test.gif.b64_out2");
Base64.OutputStream b64os = null;//new Base64.OutputStream( fos, ENCODE );
//b64os.write( rbytes );
//b64os.close();
Base64.writeFile( rbytes, "test.gif.b64", ENCODE );
// Read raw bytes that are actually encoded (but we'll ignore that)
fis = new java.io.FileInputStream( "test.gif.b64_out2" );
byte[] rebytes = new byte[0];
b = -1;
while( (b = fis.read()) >= 0 ){
byte[] temp = new byte[ rebytes.length + 1 ];
System.arraycopy( rebytes,0, temp,0,rebytes.length );
temp[rebytes.length] = (byte)b;
rebytes = temp;
} // end while: terribly inefficient way to read data
fis.close();
String s = new String( rebytes );
javax.swing.JTextArea jta = new javax.swing.JTextArea( s );
javax.swing.JScrollPane jsp = new javax.swing.JScrollPane( jta );
javax.swing.JFrame jframe = new javax.swing.JFrame();
jframe.setTitle( "Read from test.gif.b64_out2" );
jframe.getContentPane().add( jsp );
jframe.pack();
jframe.show();
// Write encoded bytes to decoded raw file
fos = new java.io.FileOutputStream("test.gif_out2");
b64os = new Base64.OutputStream( fos, DECODE );
b64os.write( rebytes );
b64os.close();
javax.swing.ImageIcon iicon = new javax.swing.ImageIcon( "test.gif_out2" );
javax.swing.JLabel jlabel = new javax.swing.JLabel( "Read from test.gif_out2", iicon,0 );
jframe = new javax.swing.JFrame();
jframe.getContentPane().add( jlabel );
jframe.pack();
jframe.show();
} // end: Test Output Stream
} // end try
catch( Exception e)
{ e.printStackTrace();
}
} // end main
/* ******** E N C O D I N G M E T H O D S ******** */
/**
* 对threeBytes数组的头三个字节进行编码,返回base64的4-byte数组
*/
private static byte[] encode3to4( byte[] threeBytes )
{ return encode3to4( threeBytes, 3 );
} // end encodeToBytes
/**
* 对threeBytes进行编码,numSigBytes是标识符字节
*/
private static byte[] encode3to4( byte[] threeBytes, int numSigBytes )
{ byte[] dest = new byte[4];
encode3to4( threeBytes, 0, numSigBytes, dest, 0 );
return dest;
}
/**
* Encodes up to three bytes of the array <var>source</var>
* and writes the resulting four Base64 bytes to <var>destination</var>.
* The source and destination arrays can be manipulated
* anywhere along their length by specifying
* <var>srcOffset</var> and <var>destOffset</var>.
* This method does not check to make sure your arrays
* are large enough to accomodate <var>srcOffset</var> + 3 for
* the <var>source</var> array or <var>destOffset</var> + 4 for
* the <var>destination</var> array.
* The actual number of significant bytes in your array is
* given by <var>numSigBytes</var>.
*/
private static byte[] encode3to4(
byte[] source, int srcOffset, int numSigBytes,
byte[] destination, int destOffset )
{
// 1 2 3
// 01234567890123456789012345678901 Bit position
// --------000000001111111122222222 Array position from threeBytes
// --------| || || || | Six bit groups to index ALPHABET
// >>18 >>12 >> 6 >> 0 Right shift necessary
// 0x3f 0x3f 0x3f Additional AND
// Create buffer with zero-padding if there are only one or two
// significant bytes passed in the array.
// We have to shift left 24 in order to flush out the 1's that appear
// when Java treats a value as negative that is cast from a byte to an int.
int inBuff = ( numSigBytes > 0 ? ((source[ srcOffset ] << 24) >>> 8) : 0 )
| ( numSigBytes > 1 ? ((source[ srcOffset + 1 ] << 24) >>> 16) : 0 )
| ( numSigBytes > 2 ? ((source[ srcOffset + 2 ] << 24) >>> 24) : 0 );
switch( numSigBytes )
{
case 3:
destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ];
destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ];
destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ];
destination[ destOffset + 3 ] = ALPHABET[ (inBuff ) & 0x3f ];
return destination;
case 2:
destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ];
destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ];
destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ];
destination[ destOffset + 3 ] = EQUALS_SIGN;
return destination;
case 1:
destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ];
destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ];
destination[ destOffset + 2 ] = EQUALS_SIGN;
destination[ destOffset + 3 ] = EQUALS_SIGN;
return destination;
default:
return destination;
} // end switch
} // end encode3to4
/**
* Serializes an object and returns the Base64-encoded
* version of that serialized object. If the object
* cannot be serialized or there is another error,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -