📄 d0981a3d6d36001b19ca9d8cdf62cf01
字号:
* version of that serialized object. If the object
* cannot be serialized or there is another error,
* the method will return <tt>null</tt>.
*
* @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
/**
* Serializes an object and returns the Base64-encoded
* version of that serialized object. If the object
* cannot be serialized or there is another error,
* the method will return <tt>null</tt>.
*
* @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
/**
* Encodes a byte array into Base64 notation.
* Equivalen to calling
* <code>encodeBytes( source, 0, source.length )</code>
*
* @param source The data to convert
* @since 1.4
*/
public static String encodeBytes( byte[] source )
{
return encodeBytes( source, true );
} // end encodeBytes
/**
* Encodes a byte array into Base64 notation.
* Equivalen to calling
* <code>encodeBytes( source, 0, source.length )</code>
*
* @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
/**
* Encodes a byte array into Base64 notation.
*
* @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
/**
* Encodes a byte array into Base64 notation.
*
* @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
/**
* Encodes a string in Base64 notation with line breaks
* after every 75 Base64 characters.
* Of course you probably only need to encode a string
* if there are non-ASCII characters in it such as many
* non-English languages.
*
* @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
/**
* Encodes a string in Base64 notation with line breaks
* after every 75 Base64 characters.
* Of course you probably only need to encode a string
* if there are non-ASCII characters in it such as many
* non-English languages.
*
* @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
/**
* Reads a file and either encodes or decodes it.
*
* @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
/**
* Reads a file and either encodes or decodes it.
*
* @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
/**
* Writes a byte array to a file either encoding
* it or decoding it as specified.
*
* @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
/**
* Writes a byte array to a file either encoding
* it or decoding it as specified.
*
* @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
/**
* Writes a byte array to a file either encoding
* it or decoding it as specified.
*
* @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
/**
* Simple helper method that Base64-encodes a file
* and returns the encoded string or <tt>null</tt>
* if there was an error.
*
* @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
/**
* Simple helper method that Base64-decodes a file
* and returns the decoded data or <tt>null</tt>
* if there was an error.
*
* @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 + -