📄 base64.java
字号:
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
/**
* 按照Base64编码串行化某个对象。如果该对象不能被串行化或存在其他错误,
* 该方法会返回一个null值。
*
* @param serializableObject The object to encode
* @return The Base64-encoded object
* @since 1.4
*/
public static String encodeObject( java.io.Serializable serializableObject )
{
return encodeObject( serializableObject, true );
} // end encodeObject
/**
* 按照Base64编码串行化某个对象。如果该对象不能被串行化或存在其他错误,
* 该方法会返回一个null值。
*
* @param serializableObject The object to encode
* @param breakLines Break lines at 80 characters or less.
* @return The Base64-encoded object
* @since 1.4
*/
public static String encodeObject( java.io.Serializable serializableObject, boolean breakLines )
{
java.io.ByteArrayOutputStream baos = null;
java.io.OutputStream b64os = null;
java.io.ObjectOutputStream oos = null;
try
{
baos = new java.io.ByteArrayOutputStream();
b64os = new Base64.OutputStream( baos, Base64.ENCODE, breakLines );
oos = new java.io.ObjectOutputStream( b64os );
oos.writeObject( serializableObject );
} // end try
catch( java.io.IOException e )
{
e.printStackTrace();
return null;
} // end catch
finally
{
try{ oos.close(); } catch( Exception e ){}
try{ b64os.close(); } catch( Exception e ){}
try{ baos.close(); } catch( Exception e ){}
} // end finally
return new String( baos.toByteArray() );
} // end encode
/**
* 对字节数组进行编码,等同于<code>encodeBytes( source, 0, source.length )
*
* @param source The data to convert
* @since 1.4
*/
public static String encodeBytes( byte[] source )
{
return encodeBytes( source, true );
} // end encodeBytes
/**
* 对字节数组进行编码;
*
* @param source The data to convert
* @param breakLines Break lines at 80 characters or less.
* @since 1.4
*/
public static String encodeBytes( byte[] source, boolean breakLines )
{
return encodeBytes( source, 0, source.length, breakLines );
} // end encodeBytes
/**
* 对字节数组进行编码;
*
* @param source The data to convert
* @param off Offset in array where conversion should begin
* @param len Length of data to convert
* @since 1.4
*/
public static String encodeBytes( byte[] source, int off, int len )
{
return encodeBytes( source, off, len, true );
} // end encodeBytes
/**
* 实际进行编码的方法;
*
* @param source The data to convert
* @param off Offset in array where conversion should begin
* @param len Length of data to convert
* @param breakLines Break lines at 80 characters or less.
* @since 1.4
*/
public static String encodeBytes( byte[] source, int off, int len, boolean breakLines )
{
int len43 = len * 4 / 3;
byte[] outBuff = new byte[ ( len43 ) // Main 4:3
+ ( (len % 3) > 0 ? 4 : 0 ) // Account for padding
+ (breakLines ? ( len43 / MAX_LINE_LENGTH ) : 0) ]; // New lines
int d = 0;
int e = 0;
int len2 = len - 2;
int lineLength = 0;
for( ; d < len2; d+=3, e+=4 )
{
encode3to4( source, d+off, 3, outBuff, e );
lineLength += 4;
if( breakLines && lineLength == MAX_LINE_LENGTH )
{
outBuff[e+4] = NEW_LINE;
e++;
lineLength = 0;
} // end if: end of line
} // en dfor: each piece of array
if( d < len )
{
encode3to4( source, d+off, len - d, outBuff, e );
e += 4;
} // end if: some padding needed
return new String( outBuff, 0, e );
} // end encodeBytes
/**
* 对String进行编码,并且每个break line之前的字符不超过80个
*
* @param s the string to encode
* @return the encoded string
* @since 1.3
*/
public static String encodeString( String s )
{
return encodeString( s, true );
} // end encodeString
/**
* 对String进行编码;
*
* @param s the string to encode
* @param breakLines Break lines at 80 characters or less.
* @return the encoded string
* @since 1.3
*/
public static String encodeString( String s, boolean breakLines )
{
return encodeBytes( s.getBytes(), breakLines );
} // end encodeString
/**
* 根据encode参数,读取文件中的数据;
*
* @param file The file to read
* @param encode Whether or not to encode file as it is read.
* @return The encoded/decoded file or <tt>null</tt> if there was an error.
* @since 1.4
*/
public static byte[] readFile( String file, boolean encode )
{
return readFile( new java.io.File( file ), encode );
} // end readFile
/**
* 根据encode参数,读取文件中的数据;
*
* @param file The file to read
* @param encode Whether or not to encode file as it is read.
* @return The encoded/decoded file or <tt>null</tt> if there was an error.
* @since 1.4
*/
public static byte[] readFile( java.io.File file, boolean encode )
{
byte[] data = new byte[100];
byte[] returnValue = null;
int nextIndex = 0;
int b = -1;
Base64.InputStream bis = null;
try{
bis = new Base64.InputStream(
new java.io.BufferedInputStream(
new java.io.FileInputStream( file ) ), encode );
while( (b = bis.read()) >= 0 ){
// Resize array?
if( nextIndex >= data.length ){
byte[] temp = new byte[ data.length << 1 ];
System.arraycopy( data,0, temp,0,data.length );
data = temp;
} // end if: resize array
data[ nextIndex++ ] = (byte)b;
} // end while: each byte
returnValue = new byte[ nextIndex ];
System.arraycopy( data,0, returnValue,0,nextIndex );
} // end try
catch( java.io.IOException e ){
returnValue = null;
} // end catch
finally{
try{ bis.close(); }catch( Exception e ){}
} // end finally
return returnValue;
} // end readFile
/**
* 根据encode参数,将字节数组写入文件;
*
* @param data The array to write to a file.
* @param file The file to write to.
* @param encode Whether or not to encode the data.
* @return Whether or not the write was a success.
* @since 1.4
*/
public static boolean writeFile( byte[] data, String file, boolean encode )
{
return writeFile( data, 0, data.length, new java.io.File( file ), encode );
} // end writeFile
/**
* 根据encode参数,将字节数组写入文件;
*
* @param data The array to write to a file.
* @param file The file to write to.
* @param encode Whether or not to encode the data.
* @return Whether or not the write was a success.
* @since 1.4
*/
public static boolean writeFile( byte[] data, java.io.File file, boolean encode )
{
return writeFile( data, 0, data.length, file, encode );
} // end writeFile
/**
* 根据encode参数,将字节数组写入文件;
*
* @param data The array to write to a file.
* @param offset The offset where the "real" data begins.
* @param length The amount of data to write.
* @param file The file to write to.
* @param encode Whether or not to encode the data.
* @return Whether or not the write was a success.
* @since 1.4
*/
public static boolean writeFile( byte[] data, int offset, int length, java.io.File file, boolean encode )
{
Base64.OutputStream bos = null;
boolean success = false;
try{
bos = new Base64.OutputStream(
new java.io.BufferedOutputStream(
new java.io.FileOutputStream( file ) ), encode );
bos.write( data, offset, length );
success = true;
} // end try
catch( java.io.IOException e ){
success = false;
} // end catch
finally{
try{ bos.close(); }catch( Exception e ){}
} // end finally
return success;
} // end writeFile
/**
* 对文件数据进行编码,然后返回编码后的字符串,如果遇到错误则返回null。
*
* @param rawfile The file to read
* @return The encoded file or <tt>null</tt> if there was an error.
* @since 1.4
*/
public static String encodeFromFile( String rawfile )
{
byte[] ebytes = readFile( rawfile, ENCODE );
return ebytes == null ? null : new String( ebytes );
} // end encodeFromFile
/**
* 对文件数据进行解码并返回解码后的数据,如果遇到错误则返回null。
*
* @param encfile The file to read
* @return The decoded file or <tt>null</tt> if there was an error.
* @since 1.4
*/
public static byte[] decodeFromFile( String encfile )
{
return readFile( encfile, DECODE );
} // end encodeFromFile
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -