📄 base64u.java
字号:
package com.mindprod.base64;
/**
* <pre>
* Freeware from:
* Roedy Green
* Canadian Mind Products
* #101 - 2536 Wark Street
* Victoria, BC Canada V8T 4G8
* tel:(250) 361-9093
* roedy g at mindprod dotcom
* <p/>
* Works exactly like Base64 except avoids using the characters
* + / and =. This means Base64u-encoded data can be used either
* URLCoded or plain in
* URL-Encoded contexts such as GET, PUT or URLs. You can treat the
* output either as
* not needing encoding or already URLEncoded.
* </pre>
*
* @noinspection WeakerAccess
*/
public final class Base64u extends Base64
{
// ------------------------------ FIELDS ------------------------------
/**
* letter of the alphabet used to encode binary values 0..63.
*/
private static char[] vc;
/**
* binary value encoded by a given letter of the alphabet 0..63.
*/
private static int[] cv;
// -------------------------- PUBLIC INSTANCE METHODS --------------------------
/**
* constructor.
*
* @noinspection WeakerAccess
*/
public Base64u()
{
spec1 = '-';
spec2 = '_';
spec3 = '*';
initTables();
}
// -------------------------- OTHER METHODS --------------------------
/**
* Initialise both static and instance table.
*/
private void initTables()
{
/* initialise valueToChar and charToValue tables */
if ( vc == null )
{
// statics are not initialised yet
vc = new char[64];
cv = new int[256];
// build translate valueToChar table only once.
// 0..25 -> 'A'..'Z'
for ( int i = 0; i <= 25; i++ )
{
vc[ i ] = ( char ) ( 'A' + i );
}
// 26..51 -> 'a'..'z'
for ( int i = 0; i <= 25; i++ )
{
vc[ i + 26 ] = ( char ) ( 'a' + i );
}
// 52..61 -> '0'..'9'
for ( int i = 0; i <= 9; i++ )
{
vc[ i + 52 ] = ( char ) ( '0' + i );
}
vc[ 62 ] = spec1;
vc[ 63 ] = spec2;
// build translate charToValue table only once.
for ( int i = 0; i < 256; i++ )
{
cv[ i ] = IGNORE;// default is to ignore
}
for ( int i = 0; i < 64; i++ )
{
cv[ vc[ i ] ] = i;
}
cv[ spec3 ] = PAD;
}
valueToChar = vc;
charToValue = cv;
}
// --------------------------- main() method ---------------------------
/**
* test driver.
*
* @param args not used .
* @noinspection ConstantConditions
*/
public static void main( String[] args )
{
if ( DEBUGGING )
{
byte[] a = { ( byte ) 0xfc, ( byte ) 0x0f, ( byte ) 0xc0 };
byte[] b = { ( byte ) 0x03, ( byte ) 0xf0, ( byte ) 0x3f };
byte[] c = { ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00 };
byte[] d = { ( byte ) 0xff, ( byte ) 0xff, ( byte ) 0xff };
byte[] e = { ( byte ) 0xfc, ( byte ) 0x0f, ( byte ) 0xc0, ( byte ) 1 };
byte[] f =
{ ( byte ) 0xfc, ( byte ) 0x0f, ( byte ) 0xc0, ( byte ) 1, ( byte ) 2 };
byte[] g = {
( byte ) 0xfc,
( byte ) 0x0f,
( byte ) 0xc0,
( byte ) 1,
( byte ) 2,
( byte ) 3 };
byte[] h = "AAAAAAAAAAB".getBytes();
show( a );
show( b );
show( c );
show( d );
show( e );
show( f );
show( g );
show( h );
Base64u b64 = new Base64u();
show( b64.decode( b64.encode( a ) ) );
show( b64.decode( b64.encode( b ) ) );
show( b64.decode( b64.encode( c ) ) );
show( b64.decode( b64.encode( d ) ) );
show( b64.decode( b64.encode( e ) ) );
show( b64.decode( b64.encode( f ) ) );
show( b64.decode( b64.encode( g ) ) );
show( b64.decode( b64.encode( h ) ) );
b64.setLineLength( 8 );
show( ( b64.encode( h ) ).getBytes() );
}
}// end main
}// end Base64
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -