Static Public Member Functions | |
static String | encodeObject (java.io.Serializable serializableObject) |
static String | encodeObject (java.io.Serializable serializableObject, int options) |
static String | encodeBytes (byte[] source) |
static String | encodeBytes (byte[] source, int options) |
static String | encodeBytes (byte[] source, int off, int len) |
static String | encodeBytes (byte[] source, int off, int len, int options) |
static byte[] | decode (byte[] source, int off, int len) |
static byte[] | decode (String s) |
static Object | decodeToObject (String encodedObject) |
static boolean | encodeToFile (byte[] dataToEncode, String filename) |
static boolean | decodeToFile (String dataToDecode, String filename) |
static byte[] | decodeFromFile (String filename) |
static String | encodeFromFile (String filename) |
Static Public Attributes | |
static final int | NO_OPTIONS = 0 |
static final int | ENCODE = 1 |
static final int | DECODE = 0 |
static final int | GZIP = 2 |
static final int | DONT_BREAK_LINES = 8 |
Static Package Functions | |
[static initializer] | |
Private Member Functions | |
Base64 () | |
Static Private Member Functions | |
static byte[] | encode3to4 (byte[] b4, byte[] threeBytes, int numSigBytes) |
static byte[] | encode3to4 (byte[] source, int srcOffset, int numSigBytes, byte[] destination, int destOffset) |
static int | decode4to3 (byte[] source, int srcOffset, byte[] destination, int destOffset) |
Static Private Attributes | |
static final int | MAX_LINE_LENGTH = 76 |
static final byte | EQUALS_SIGN = (byte)'=' |
static final byte | NEW_LINE = (byte)'\n' |
static final String | PREFERRED_ENCODING = "UTF-8" |
static final byte[] | ALPHABET |
static final byte[] | _NATIVE_ALPHABET |
static final byte[] | DECODABET |
static final byte | WHITE_SPACE_ENC = -5 |
static final byte | EQUALS_SIGN_ENC = -1 |
Classes | |
class | InputStream |
class | OutputStream |
Change Log:
int
s that you "OR" together). decode( String s, boolean gzipCompressed )
. Added the ability to "suspend" encoding in the Output Stream so you can turn on and off the encoding if you need to embed base64 data in an otherwise "normal" stream (like an XML file). I am placing this code in the Public Domain. Do with it as you will. This software comes with no guarantees or warranties but with plenty of well-wishing instead! Please visit http://iharder.net/base64 periodically to check for updates or to contribute improvements.
Definition at line 48 of file Base64.java.
|
Defeats instantiation. Definition at line 170 of file Base64.java.
|
|
Determine which ALPHABET to use. |
|
Decodes data from Base64 notation, automatically detecting gzip-compressed data and decompressing it.
Definition at line 700 of file Base64.java. References decode(), and PREFERRED_ENCODING. 00701 { 00702 byte[] bytes; 00703 try 00704 { 00705 bytes = s.getBytes( PREFERRED_ENCODING ); 00706 } // end try 00707 catch( java.io.UnsupportedEncodingException uee ) 00708 { 00709 bytes = s.getBytes(); 00710 } // end catch 00711 //</change> 00712 00713 // Decode 00714 bytes = decode( bytes, 0, bytes.length ); 00715 00716 00717 // Check to see if it's gzip-compressed 00718 // GZIP Magic Two-Byte Number: 0x8b1f (35615) 00719 if( bytes != null && bytes.length >= 4 ) 00720 { 00721 00722 int head = ((int)bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00); 00723 if( java.util.zip.GZIPInputStream.GZIP_MAGIC == head ) 00724 { 00725 java.io.ByteArrayInputStream bais = null; 00726 java.util.zip.GZIPInputStream gzis = null; 00727 java.io.ByteArrayOutputStream baos = null; 00728 byte[] buffer = new byte[2048]; 00729 int length = 0; 00730 00731 try 00732 { 00733 baos = new java.io.ByteArrayOutputStream(); 00734 bais = new java.io.ByteArrayInputStream( bytes ); 00735 gzis = new java.util.zip.GZIPInputStream( bais ); 00736 00737 while( ( length = gzis.read( buffer ) ) >= 0 ) 00738 { 00739 baos.write(buffer,0,length); 00740 } // end while: reading input 00741 00742 // No error? Get new bytes. 00743 bytes = baos.toByteArray(); 00744 00745 } // end try 00746 catch( java.io.IOException e ) 00747 { 00748 // Just return originally-decoded bytes 00749 } // end catch 00750 finally 00751 { 00752 try{ baos.close(); } catch( Exception e ){} 00753 try{ gzis.close(); } catch( Exception e ){} 00754 try{ bais.close(); } catch( Exception e ){} 00755 } // end finally 00756 00757 } // end if: gzipped 00758 } // end if: bytes.length >= 2 00759 00760 return bytes; 00761 } // end decode
|
|
Very low-level access to decoding ASCII characters in the form of a byte array. Does not support automatically gunzipping or any other "fancy" features.
Definition at line 643 of file Base64.java. References DECODABET, decode4to3(), EQUALS_SIGN, EQUALS_SIGN_ENC, and WHITE_SPACE_ENC. 00644 { 00645 int len34 = len * 3 / 4; 00646 byte[] outBuff = new byte[ len34 ]; // Upper limit on size of output 00647 int outBuffPosn = 0; 00648 00649 byte[] b4 = new byte[4]; 00650 int b4Posn = 0; 00651 int i = 0; 00652 byte sbiCrop = 0; 00653 byte sbiDecode = 0; 00654 for( i = off; i < off+len; i++ ) 00655 { 00656 sbiCrop = (byte)(source[i] & 0x7f); // Only the low seven bits 00657 sbiDecode = DECODABET[ sbiCrop ]; 00658 00659 if( sbiDecode >= WHITE_SPACE_ENC ) // White space, Equals sign or better 00660 { 00661 if( sbiDecode >= EQUALS_SIGN_ENC ) 00662 { 00663 b4[ b4Posn++ ] = sbiCrop; 00664 if( b4Posn > 3 ) 00665 { 00666 outBuffPosn += decode4to3( b4, 0, outBuff, outBuffPosn ); 00667 b4Posn = 0; 00668 00669 // If that was the equals sign, break out of 'for' loop 00670 if( sbiCrop == EQUALS_SIGN ) 00671 break; 00672 } // end if: quartet built 00673 00674 } // end if: equals sign or better 00675 00676 } // end if: white space, equals sign or better 00677 else 00678 { 00679 System.err.println( "Bad Base64 input character at " + i + ": " + source[i] + "(decimal)" ); 00680 return null; 00681 } // end else: 00682 } // each input character 00683 00684 byte[] out = new byte[ outBuffPosn ]; 00685 System.arraycopy( outBuff, 0, out, 0, outBuffPosn ); 00686 return out; 00687 } // end decode
|
|
Decodes four bytes from array source and writes the resulting bytes (up to three of them) to destination. The source and destination arrays can be manipulated anywhere along their length by specifying srcOffset and destOffset. This method does not check to make sure your arrays are large enough to accomodate srcOffset + 4 for the source array or destOffset + 3 for the destination array. This method returns the actual number of bytes that were converted from the Base64 encoding.
Definition at line 568 of file Base64.java. References DECODABET, and EQUALS_SIGN. 00569 { 00570 // Example: Dk== 00571 if( source[ srcOffset + 2] == EQUALS_SIGN ) 00572 { 00573 // Two ways to do the same thing. Don't know which way I like best. 00574 //int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 ) 00575 // | ( ( DECODABET[ source[ srcOffset + 1] ] << 24 ) >>> 12 ); 00576 int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 ) 00577 | ( ( DECODABET[ source[ srcOffset + 1] ] & 0xFF ) << 12 ); 00578 00579 destination[ destOffset ] = (byte)( outBuff >>> 16 ); 00580 return 1; 00581 } 00582 00583 // Example: DkL= 00584 else if( source[ srcOffset + 3 ] == EQUALS_SIGN ) 00585 { 00586 // Two ways to do the same thing. Don't know which way I like best. 00587 //int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 ) 00588 // | ( ( DECODABET[ source[ srcOffset + 1 ] ] << 24 ) >>> 12 ) 00589 // | ( ( DECODABET[ source[ srcOffset + 2 ] ] << 24 ) >>> 18 ); 00590 int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 ) 00591 | ( ( DECODABET[ source[ srcOffset + 1 ] ] & 0xFF ) << 12 ) 00592 | ( ( DECODABET[ source[ srcOffset + 2 ] ] & 0xFF ) << 6 ); 00593 00594 destination[ destOffset ] = (byte)( outBuff >>> 16 ); 00595 destination[ destOffset + 1 ] = (byte)( outBuff >>> 8 ); 00596 return 2; 00597 } 00598 00599 // Example: DkLE 00600 else 00601 { 00602 try{ 00603 // Two ways to do the same thing. Don't know which way I like best. 00604 //int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 ) 00605 // | ( ( DECODABET[ source[ srcOffset + 1 ] ] << 24 ) >>> 12 ) 00606 // | ( ( DECODABET[ source[ srcOffset + 2 ] ] << 24 ) >>> 18 ) 00607 // | ( ( DECODABET[ source[ srcOffset + 3 ] ] << 24 ) >>> 24 ); 00608 int outBuff = ( ( DECODABET[ source[ srcOffset ] ] & 0xFF ) << 18 ) 00609 | ( ( DECODABET[ source[ srcOffset + 1 ] ] & 0xFF ) << 12 ) 00610 | ( ( DECODABET[ source[ srcOffset + 2 ] ] & 0xFF ) << 6) 00611 | ( ( DECODABET[ source[ srcOffset + 3 ] ] & 0xFF ) ); 00612 00613 00614 destination[ destOffset ] = (byte)( outBuff >> 16 ); 00615 destination[ destOffset + 1 ] = (byte)( outBuff >> 8 ); 00616 destination[ destOffset + 2 ] = (byte)( outBuff ); 00617 00618 return 3; 00619 }catch( Exception e){ 00620 System.out.println(""+source[srcOffset]+ ": " + ( DECODABET[ source[ srcOffset ] ] ) ); 00621 System.out.println(""+source[srcOffset+1]+ ": " + ( DECODABET[ source[ srcOffset + 1 ] ] ) ); 00622 System.out.println(""+source[srcOffset+2]+ ": " + ( DECODABET[ source[ srcOffset + 2 ] ] ) ); 00623 System.out.println(""+source[srcOffset+3]+ ": " + ( DECODABET[ source[ srcOffset + 3 ] ] ) ); 00624 return -1; 00625 } //e nd catch 00626 } 00627 } // end decodeToBytes
|
|
Convenience method for reading a base64-encoded file and decoding it.
Definition at line 889 of file Base64.java. 00890 { 00891 byte[] decodedData = null; 00892 Base64.InputStream bis = null; 00893 try 00894 { 00895 // Set up some useful variables 00896 java.io.File file = new java.io.File( filename ); 00897 byte[] buffer = null; 00898 int length = 0; 00899 int numBytes = 0; 00900 00901 // Check for size of file 00902 if( file.length() > Integer.MAX_VALUE ) 00903 { 00904 System.err.println( "File is too big for this convenience method (" + file.length() + " bytes)." ); 00905 return null; 00906 } // end if: file too big for int index 00907 buffer = new byte[ (int)file.length() ]; 00908 00909 // Open a stream 00910 bis = new Base64.InputStream( 00911 new java.io.BufferedInputStream( 00912 new java.io.FileInputStream( file ) ), Base64.DECODE ); 00913 00914 // Read until done 00915 while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 ) 00916 length += numBytes; 00917 00918 // Save in a variable to return 00919 decodedData = new byte[ length ]; 00920 System.arraycopy( buffer, 0, decodedData, 0, length ); 00921 00922 } // end try 00923 catch( java.io.IOException e ) 00924 { 00925 System.err.println( "Error decoding from file " + filename ); 00926 } // end catch: IOException 00927 finally 00928 { 00929 try{ bis.close(); } catch( Exception e) {} 00930 } // end finally 00931 00932 return decodedData; 00933 } // end decodeFromFile
|
|
Convenience method for decoding data to a file.
Definition at line 854 of file Base64.java. References PREFERRED_ENCODING. 00855 { 00856 boolean success = false; 00857 Base64.OutputStream bos = null; 00858 try 00859 { 00860 bos = new Base64.OutputStream( 00861 new java.io.FileOutputStream( filename ), Base64.DECODE ); 00862 bos.write( dataToDecode.getBytes( PREFERRED_ENCODING ) ); 00863 success = true; 00864 } // end try 00865 catch( java.io.IOException e ) 00866 { 00867 success = false; 00868 } // end catch: IOException 00869 finally 00870 { 00871 try{ bos.close(); } catch( Exception e ){} 00872 } // end finally 00873 00874 return success; 00875 } // end decodeToFile
|
|
Attempts to decode Base64 data and deserialize a Java Object within. Returns
Definition at line 774 of file Base64.java. References decode(). 00775 { 00776 // Decode and gunzip if necessary 00777 byte[] objBytes = decode( encodedObject ); 00778 00779 java.io.ByteArrayInputStream bais = null; 00780 java.io.ObjectInputStream ois = null; 00781 Object obj = null; 00782 00783 try 00784 { 00785 bais = new java.io.ByteArrayInputStream( objBytes ); 00786 ois = new java.io.ObjectInputStream( bais ); 00787 00788 obj = ois.readObject(); 00789 } // end try 00790 catch( java.io.IOException e ) 00791 { 00792 e.printStackTrace(); 00793 obj = null; 00794 } // end catch 00795 catch( java.lang.ClassNotFoundException e ) 00796 { 00797 e.printStackTrace(); 00798 obj = null; 00799 } // end catch 00800 finally 00801 { 00802 try{ bais.close(); } catch( Exception e ){} 00803 try{ ois.close(); } catch( Exception e ){} 00804 } // end finally 00805 00806 return obj; 00807 } // end decodeObject
|
|
Encodes up to three bytes of the array source and writes the resulting four Base64 bytes to destination. The source and destination arrays can be manipulated anywhere along their length by specifying srcOffset and destOffset. This method does not check to make sure your arrays are large enough to accomodate srcOffset + 3 for the source array or destOffset + 4 for the destination array. The actual number of significant bytes in your array is given by numSigBytes.
Definition at line 220 of file Base64.java. References ALPHABET, and EQUALS_SIGN. 00223 { 00224 // 1 2 3 00225 // 01234567890123456789012345678901 Bit position 00226 // --------000000001111111122222222 Array position from threeBytes 00227 // --------| || || || | Six bit groups to index ALPHABET 00228 // >>18 >>12 >> 6 >> 0 Right shift necessary 00229 // 0x3f 0x3f 0x3f Additional AND 00230 00231 // Create buffer with zero-padding if there are only one or two 00232 // significant bytes passed in the array. 00233 // We have to shift left 24 in order to flush out the 1's that appear 00234 // when Java treats a value as negative that is cast from a byte to an int. 00235 int inBuff = ( numSigBytes > 0 ? ((source[ srcOffset ] << 24) >>> 8) : 0 ) 00236 | ( numSigBytes > 1 ? ((source[ srcOffset + 1 ] << 24) >>> 16) : 0 ) 00237 | ( numSigBytes > 2 ? ((source[ srcOffset + 2 ] << 24) >>> 24) : 0 ); 00238 00239 switch( numSigBytes ) 00240 { 00241 case 3: 00242 destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ]; 00243 destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ]; 00244 destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ]; 00245 destination[ destOffset + 3 ] = ALPHABET[ (inBuff ) & 0x3f ]; 00246 return destination; 00247 00248 case 2: 00249 destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ]; 00250 destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ]; 00251 destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ]; 00252 destination[ destOffset + 3 ] = EQUALS_SIGN; 00253 return destination; 00254 00255 case 1: 00256 destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ]; 00257 destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ]; 00258 destination[ destOffset + 2 ] = EQUALS_SIGN; 00259 destination[ destOffset + 3 ] = EQUALS_SIGN; 00260 return destination; 00261 00262 default: 00263 return destination; 00264 } // end switch 00265 } // end encode3to4
|
|
Encodes up to the first three bytes of array threeBytes and returns a four-byte array in Base64 notation. The actual number of significant bytes in your array is given by numSigBytes. The array threeBytes needs only be as big as numSigBytes. Code can reuse a byte array by passing a four-byte array as b4.
Definition at line 192 of file Base64.java. 00193 { 00194 encode3to4( threeBytes, 0, numSigBytes, b4, 0 ); 00195 return b4; 00196 } // end encode3to4
|
|
Encodes a byte array into Base64 notation. Valid options: GZIP: gzip-compresses object before encoding it. DONT_BREAK_LINES: don't break lines at 76 characters Note: Technically, this makes your encoding non-compliant.
Example:
Example:
Definition at line 444 of file Base64.java. References DONT_BREAK_LINES, ENCODE, encode3to4(), GZIP, MAX_LINE_LENGTH, NEW_LINE, and PREFERRED_ENCODING. 00445 { 00446 // Isolate options 00447 int dontBreakLines = ( options & DONT_BREAK_LINES ); 00448 int gzip = ( options & GZIP ); 00449 00450 // Compress? 00451 if( gzip == GZIP ) 00452 { 00453 java.io.ByteArrayOutputStream baos = null; 00454 java.util.zip.GZIPOutputStream gzos = null; 00455 Base64.OutputStream b64os = null; 00456 00457 00458 try 00459 { 00460 // GZip -> Base64 -> ByteArray 00461 baos = new java.io.ByteArrayOutputStream(); 00462 b64os = new Base64.OutputStream( baos, ENCODE | dontBreakLines ); 00463 gzos = new java.util.zip.GZIPOutputStream( b64os ); 00464 00465 gzos.write( source, off, len ); 00466 gzos.close(); 00467 } // end try 00468 catch( java.io.IOException e ) 00469 { 00470 e.printStackTrace(); 00471 return null; 00472 } // end catch 00473 finally 00474 { 00475 try{ gzos.close(); } catch( Exception e ){} 00476 try{ b64os.close(); } catch( Exception e ){} 00477 try{ baos.close(); } catch( Exception e ){} 00478 } // end finally 00479 00480 // Return value according to relevant encoding. 00481 try 00482 { 00483 return new String( baos.toByteArray(), PREFERRED_ENCODING ); 00484 } // end try 00485 catch (java.io.UnsupportedEncodingException uue) 00486 { 00487 return new String( baos.toByteArray() ); 00488 } // end catch 00489 } // end if: compress 00490 00491 // Else, don't compress. Better not to use streams at all then. 00492 else 00493 { 00494 // Convert option to boolean in way that code likes it. 00495 boolean breakLines = dontBreakLines == 0; 00496 00497 int len43 = len * 4 / 3; 00498 byte[] outBuff = new byte[ ( len43 ) // Main 4:3 00499 + ( (len % 3) > 0 ? 4 : 0 ) // Account for padding 00500 + (breakLines ? ( len43 / MAX_LINE_LENGTH ) : 0) ]; // New lines 00501 int d = 0; 00502 int e = 0; 00503 int len2 = len - 2; 00504 int lineLength = 0; 00505 for( ; d < len2; d+=3, e+=4 ) 00506 { 00507 encode3to4( source, d+off, 3, outBuff, e ); 00508 00509 lineLength += 4; 00510 if( breakLines && lineLength == MAX_LINE_LENGTH ) 00511 { 00512 outBuff[e+4] = NEW_LINE; 00513 e++; 00514 lineLength = 0; 00515 } // end if: end of line 00516 } // en dfor: each piece of array 00517 00518 if( d < len ) 00519 { 00520 encode3to4( source, d+off, len - d, outBuff, e ); 00521 e += 4; 00522 } // end if: some padding needed 00523 00524 00525 // Return value according to relevant encoding. 00526 try 00527 { 00528 return new String( outBuff, 0, e, PREFERRED_ENCODING ); 00529 } // end try 00530 catch (java.io.UnsupportedEncodingException uue) 00531 { 00532 return new String( outBuff, 0, e ); 00533 } // end catch 00534 00535 } // end else: don't compress 00536 00537 } // end encodeBytes
|
|
Encodes a byte array into Base64 notation. Does not GZip-compress data.
Definition at line 415 of file Base64.java. References encodeBytes(), and NO_OPTIONS. 00416 { 00417 return encodeBytes( source, off, len, NO_OPTIONS ); 00418 } // end encodeBytes
|
|
Encodes a byte array into Base64 notation. Valid options: GZIP: gzip-compresses object before encoding it. DONT_BREAK_LINES: don't break lines at 76 characters Note: Technically, this makes your encoding non-compliant.
Example:
Example:
Definition at line 400 of file Base64.java. References encodeBytes(). 00401 { 00402 return encodeBytes( source, 0, source.length, options ); 00403 } // end encodeBytes
|
|
Encodes a byte array into Base64 notation. Does not GZip-compress data.
Definition at line 373 of file Base64.java. References NO_OPTIONS. 00374 { 00375 return encodeBytes( source, 0, source.length, NO_OPTIONS ); 00376 } // end encodeBytes
|
|
Convenience method for reading a binary file and base64-encoding it.
Definition at line 946 of file Base64.java. 00947 { 00948 String encodedData = null; 00949 Base64.InputStream bis = null; 00950 try 00951 { 00952 // Set up some useful variables 00953 java.io.File file = new java.io.File( filename ); 00954 byte[] buffer = new byte[ (int)(file.length() * 1.4) ]; 00955 int length = 0; 00956 int numBytes = 0; 00957 00958 // Open a stream 00959 bis = new Base64.InputStream( 00960 new java.io.BufferedInputStream( 00961 new java.io.FileInputStream( file ) ), Base64.ENCODE ); 00962 00963 // Read until done 00964 while( ( numBytes = bis.read( buffer, length, 4096 ) ) >= 0 ) 00965 length += numBytes; 00966 00967 // Save in a variable to return 00968 encodedData = new String( buffer, 0, length, Base64.PREFERRED_ENCODING ); 00969 00970 } // end try 00971 catch( java.io.IOException e ) 00972 { 00973 System.err.println( "Error encoding from file " + filename ); 00974 } // end catch: IOException 00975 finally 00976 { 00977 try{ bis.close(); } catch( Exception e) {} 00978 } // end finally 00979 00980 return encodedData; 00981 } // end encodeFromFile
|
|
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 Valid options: GZIP: gzip-compresses object before encoding it. DONT_BREAK_LINES: don't break lines at 76 characters Note: Technically, this makes your encoding non-compliant.
Example:
Example:
Definition at line 310 of file Base64.java. References DONT_BREAK_LINES, ENCODE, GZIP, and PREFERRED_ENCODING. 00311 { 00312 // Streams 00313 java.io.ByteArrayOutputStream baos = null; 00314 java.io.OutputStream b64os = null; 00315 java.io.ObjectOutputStream oos = null; 00316 java.util.zip.GZIPOutputStream gzos = null; 00317 00318 // Isolate options 00319 int gzip = (options & GZIP); 00320 int dontBreakLines = (options & DONT_BREAK_LINES); 00321 00322 try 00323 { 00324 // ObjectOutputStream -> (GZIP) -> Base64 -> ByteArrayOutputStream 00325 baos = new java.io.ByteArrayOutputStream(); 00326 b64os = new Base64.OutputStream( baos, ENCODE | dontBreakLines ); 00327 00328 // GZip? 00329 if( gzip == GZIP ) 00330 { 00331 gzos = new java.util.zip.GZIPOutputStream( b64os ); 00332 oos = new java.io.ObjectOutputStream( gzos ); 00333 } // end if: gzip 00334 else 00335 oos = new java.io.ObjectOutputStream( b64os ); 00336 00337 oos.writeObject( serializableObject ); 00338 } // end try 00339 catch( java.io.IOException e ) 00340 { 00341 e.printStackTrace(); 00342 return null; 00343 } // end catch 00344 finally 00345 { 00346 try{ oos.close(); } catch( Exception e ){} 00347 try{ gzos.close(); } catch( Exception e ){} 00348 try{ b64os.close(); } catch( Exception e ){} 00349 try{ baos.close(); } catch( Exception e ){} 00350 } // end finally 00351 00352 // Return value according to relevant encoding. 00353 try 00354 { 00355 return new String( baos.toByteArray(), PREFERRED_ENCODING ); 00356 } // end try 00357 catch (java.io.UnsupportedEncodingException uue) 00358 { 00359 return new String( baos.toByteArray() ); 00360 } // end catch 00361 00362 } // end encode
|
|
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
Definition at line 280 of file Base64.java. References NO_OPTIONS. 00281 { 00282 return encodeObject( serializableObject, NO_OPTIONS ); 00283 } // end encodeObject
|
|
Convenience method for encoding data to a file.
Definition at line 820 of file Base64.java. 00821 { 00822 boolean success = false; 00823 Base64.OutputStream bos = null; 00824 try 00825 { 00826 bos = new Base64.OutputStream( 00827 new java.io.FileOutputStream( filename ), Base64.ENCODE ); 00828 bos.write( dataToEncode ); 00829 success = true; 00830 } // end try 00831 catch( java.io.IOException e ) 00832 { 00833 00834 success = false; 00835 } // end catch: IOException 00836 finally 00837 { 00838 try{ bos.close(); } catch( Exception e ){} 00839 } // end finally 00840 00841 return success; 00842 } // end encodeToFile
|
|
Initial value: { (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)'/' } Definition at line 94 of file Base64.java. |
|
The 64 valid Base64 values. Definition at line 93 of file Base64.java. |
|
Initial value: { -9,-9,-9,-9,-9,-9,-9,-9,-9, -5,-5, -9,-9, -5, -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, -9,-9,-9,-9,-9, -5, -9,-9,-9,-9,-9,-9,-9,-9,-9,-9, 62, -9,-9,-9, 63, 52,53,54,55,56,57,58,59,60,61, -9,-9,-9, -1, -9,-9,-9, 0,1,2,3,4,5,6,7,8,9,10,11,12,13, 14,15,16,17,18,19,20,21,22,23,24,25, -9,-9,-9,-9,-9,-9, 26,27,28,29,30,31,32,33,34,35,36,37,38, 39,40,41,42,43,44,45,46,47,48,49,50,51, -9,-9,-9,-9 } Definition at line 128 of file Base64.java. |
|
Specify decoding. Definition at line 62 of file Base64.java. |
|
Don't break lines when encoding (violates strict Base64 specification) Definition at line 70 of file Base64.java. |
|
Specify encoding. Definition at line 58 of file Base64.java. |
|
The equals sign (=) as a byte. Definition at line 81 of file Base64.java. |
|
Definition at line 166 of file Base64.java. |
|
Specify that data should be gzip-compressed. Definition at line 66 of file Base64.java. |
|
Maximum line length (76) of Base64 output. Definition at line 77 of file Base64.java. |
|
The new line character ( Definition at line 85 of file Base64.java. |
|
No options specified. Value is zero. Definition at line 55 of file Base64.java. |
|
Preferred encoding. Definition at line 89 of file Base64.java. |
|
Definition at line 165 of file Base64.java. |