Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

Base64 Class Reference

List of all members.

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

Detailed Description

Encodes and decodes to and from Base64 notation.

Change Log:

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.

Author:
Robert Harder

rob@iharder.net

Version:
2.1

Definition at line 48 of file Base64.java.


Constructor & Destructor Documentation

Base64::Base64  )  [inline, private]
 

Defeats instantiation.

Definition at line 170 of file Base64.java.

00170 {}


Member Function Documentation

Base64::[static initializer]  )  [inline, static, package]
 

Determine which ALPHABET to use.

static byte [] Base64::decode String  s  )  [inline, static]
 

Decodes data from Base64 notation, automatically detecting gzip-compressed data and decompressing it.

Parameters:
s the string to decode
Returns:
the decoded data
Since:
1.4

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

static byte [] Base64::decode byte[]  source,
int  off,
int  len
[inline, static]
 

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.

Parameters:
source The Base64 encoded data
off The offset of where to begin decoding
len The length of characters to decode
Returns:
decoded data
Since:
1.3

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

static int Base64::decode4to3 byte[]  source,
int  srcOffset,
byte[]  destination,
int  destOffset
[inline, static, private]
 

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.

Parameters:
source the array to convert
srcOffset the index where conversion begins
destination the array to hold the conversion
destOffset the index where output will be put
Returns:
the number of decoded bytes converted
Since:
1.3

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

static byte [] Base64::decodeFromFile String  filename  )  [inline, static]
 

Convenience method for reading a base64-encoded file and decoding it.

Parameters:
filename Filename for reading encoded data
Returns:
decoded byte array or null if unsuccessful
Since:
2.1

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

static boolean Base64::decodeToFile String  dataToDecode,
String  filename
[inline, static]
 

Convenience method for decoding data to a file.

Parameters:
dataToDecode Base64-encoded data as a string
filename Filename for saving decoded data
Returns:
true if successful, false otherwise
Since:
2.1

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

static Object Base64::decodeToObject String  encodedObject  )  [inline, static]
 

Attempts to decode Base64 data and deserialize a Java Object within. Returns null if there was an error.

Parameters:
encodedObject The Base64 data to decode
Returns:
The decoded and deserialized object
Since:
1.5

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

static byte [] Base64::encode3to4 byte[]  source,
int  srcOffset,
int  numSigBytes,
byte[]  destination,
int  destOffset
[inline, static, private]
 

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.

Parameters:
source the array to convert
srcOffset the index where conversion begins
numSigBytes the number of significant bytes in your array
destination the array to hold the conversion
destOffset the index where output will be put
Returns:
the destination array
Since:
1.3

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

static byte [] Base64::encode3to4 byte[]  b4,
byte[]  threeBytes,
int  numSigBytes
[inline, static, private]
 

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.

Parameters:
b4 A reusable byte array to reduce array instantiation
threeBytes the array to convert
numSigBytes the number of significant bytes in your array
Returns:
four byte array in Base64 notation.
Since:
1.5.1

Definition at line 192 of file Base64.java.

00193     {
00194         encode3to4( threeBytes, 0, numSigBytes, b4, 0 );
00195         return b4;
00196     }   // end encode3to4

static String Base64::encodeBytes byte[]  source,
int  off,
int  len,
int  options
[inline, static]
 

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: encodeBytes( myData, Base64.GZIP ) or

Example: encodeBytes( myData, Base64.GZIP | Base64.DONT_BREAK_LINES )

Parameters:
source The data to convert
off Offset in array where conversion should begin
len Length of data to convert
options Specified options
See also:
Base64::GZIP

Base64::DONT_BREAK_LINES

Since:
2.0

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

static String Base64::encodeBytes byte[]  source,
int  off,
int  len
[inline, static]
 

Encodes a byte array into Base64 notation. Does not GZip-compress data.

Parameters:
source The data to convert
off Offset in array where conversion should begin
len Length of data to convert
Since:
1.4

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

static String Base64::encodeBytes byte[]  source,
int  options
[inline, static]
 

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: encodeBytes( myData, Base64.GZIP ) or

Example: encodeBytes( myData, Base64.GZIP | Base64.DONT_BREAK_LINES )

Parameters:
source The data to convert
options Specified options
See also:
Base64::GZIP

Base64::DONT_BREAK_LINES

Since:
2.0

Definition at line 400 of file Base64.java.

References encodeBytes().

00401     {   
00402         return encodeBytes( source, 0, source.length, options );
00403     }   // end encodeBytes

static String Base64::encodeBytes byte[]  source  )  [inline, static]
 

Encodes a byte array into Base64 notation. Does not GZip-compress data.

Parameters:
source The data to convert
Since:
1.4

Definition at line 373 of file Base64.java.

References NO_OPTIONS.

00374     {
00375         return encodeBytes( source, 0, source.length, NO_OPTIONS );
00376     }   // end encodeBytes

static String Base64::encodeFromFile String  filename  )  [inline, static]
 

Convenience method for reading a binary file and base64-encoding it.

Parameters:
filename Filename for reading binary data
Returns:
base64-encoded string or null if unsuccessful
Since:
2.1

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

static String Base64::encodeObject java.io.Serializable  serializableObject,
int  options
[inline, static]
 

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 null.

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: encodeObject( myObj, Base64.GZIP ) or

Example: encodeObject( myObj, Base64.GZIP | Base64.DONT_BREAK_LINES )

Parameters:
serializableObject The object to encode
options Specified options
Returns:
The Base64-encoded object
See also:
Base64::GZIP

Base64::DONT_BREAK_LINES

Since:
2.0

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

static String Base64::encodeObject java.io.Serializable  serializableObject  )  [inline, static]
 

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 null. The object is not GZip-compressed before being encoded.

Parameters:
serializableObject The object to encode
Returns:
The Base64-encoded object
Since:
1.4

Definition at line 280 of file Base64.java.

References NO_OPTIONS.

00281     {
00282         return encodeObject( serializableObject, NO_OPTIONS );
00283     }   // end encodeObject

static boolean Base64::encodeToFile byte[]  dataToEncode,
String  filename
[inline, static]
 

Convenience method for encoding data to a file.

Parameters:
dataToEncode byte array of data to encode in base64 form
filename Filename for saving encoded data
Returns:
true if successful, false otherwise
Since:
2.1

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


Member Data Documentation

final byte [] Base64::_NATIVE_ALPHABET [static, private]
 

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.

final byte [] Base64::ALPHABET [static, private]
 

The 64 valid Base64 values.

Definition at line 93 of file Base64.java.

final byte [] Base64::DECODABET [static, private]
 

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                                 
        
    }
Translates a Base64 value to either its 6-bit reconstruction value or a negative number indicating some other meaning.

Definition at line 128 of file Base64.java.

final int Base64::DECODE = 0 [static]
 

Specify decoding.

Definition at line 62 of file Base64.java.

final int Base64::DONT_BREAK_LINES = 8 [static]
 

Don't break lines when encoding (violates strict Base64 specification)

Definition at line 70 of file Base64.java.

final int Base64::ENCODE = 1 [static]
 

Specify encoding.

Definition at line 58 of file Base64.java.

final byte Base64::EQUALS_SIGN = (byte)'=' [static, private]
 

The equals sign (=) as a byte.

Definition at line 81 of file Base64.java.

final byte Base64::EQUALS_SIGN_ENC = -1 [static, private]
 

Definition at line 166 of file Base64.java.

final int Base64::GZIP = 2 [static]
 

Specify that data should be gzip-compressed.

Definition at line 66 of file Base64.java.

final int Base64::MAX_LINE_LENGTH = 76 [static, private]
 

Maximum line length (76) of Base64 output.

Definition at line 77 of file Base64.java.

final byte Base64::NEW_LINE = (byte)'\n' [static, private]
 

The new line character (
) as a byte.

Definition at line 85 of file Base64.java.

final int Base64::NO_OPTIONS = 0 [static]
 

No options specified. Value is zero.

Definition at line 55 of file Base64.java.

final String Base64::PREFERRED_ENCODING = "UTF-8" [static, private]
 

Preferred encoding.

Definition at line 89 of file Base64.java.

final byte Base64::WHITE_SPACE_ENC = -5 [static, private]
 

Definition at line 165 of file Base64.java.


The documentation for this class was generated from the following file:
Generated on Mon Mar 6 23:34:35 2006 by  doxygen 1.4.4