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

Base64::InputStream Class Reference

List of all members.

Public Member Functions

 InputStream (java.io.InputStream in)
 InputStream (java.io.InputStream in, int options)
int read () throws java.io.IOException
int read (byte[] dest, int off, int len) throws java.io.IOException

Private Attributes

boolean encode
int position
byte[] buffer
int bufferLength
int numSigBytes
int lineLength
boolean breakLines

Detailed Description

A Base64.InputStream will read data from another java.io.InputStream, given in the constructor, and encode/decode to/from Base64 notation on the fly.

See also:
Base64
Since:
1.3

Definition at line 998 of file Base64.java.


Constructor & Destructor Documentation

Base64::InputStream::InputStream java.io.InputStream  in  )  [inline]
 

Constructs a Base64.InputStream in DECODE mode.

Parameters:
in the java.io.InputStream from which to read data.
Since:
1.3

Definition at line 1015 of file Base64.java.

References Base64::DECODE.

01016         {   
01017             this( in, DECODE );
01018         }   // end constructor

Base64::InputStream::InputStream java.io.InputStream  in,
int  options
[inline]
 

Constructs a Base64.InputStream in either ENCODE or DECODE mode.

Valid options:

   ENCODE or DECODE: Encode or Decode as data is read.
   DONT_BREAK_LINES: don't break lines at 76 characters
     (only meaningful when encoding)
     Note: Technically, this makes your encoding non-compliant.
 

Example: new Base64.InputStream( in, Base64.DECODE )

Parameters:
in the java.io.InputStream from which to read data.
options Specified options
See also:
Base64::ENCODE

Base64::DECODE

Base64::DONT_BREAK_LINES

Since:
2.0

Definition at line 1042 of file Base64.java.

References bufferLength, Base64::DONT_BREAK_LINES, encode, and Base64::ENCODE.

01043         {   
01044             super( in );
01045             this.breakLines   = (options & DONT_BREAK_LINES) != DONT_BREAK_LINES;
01046             this.encode       = (options & ENCODE) == ENCODE;
01047             this.bufferLength = encode ? 4 : 3;
01048             this.buffer   = new byte[ bufferLength ];
01049             this.position = -1;
01050             this.lineLength = 0;
01051         }   // end constructor


Member Function Documentation

int Base64::InputStream::read byte[]  dest,
int  off,
int  len
throws java.io.IOException [inline]
 

Calls read() repeatedly until the end of stream is reached or len bytes are read. Returns number of bytes read into array or -1 if end of stream is encountered.

Parameters:
dest array to hold values
off offset for array
len max number of bytes to read into array
Returns:
bytes read into array or -1 if end of stream is encountered.
Since:
1.3

Definition at line 1188 of file Base64.java.

References read().

01189         {
01190             int i;
01191             int b;
01192             for( i = 0; i < len; i++ )
01193             {
01194                 b = read();
01195                 
01196                 //if( b < 0 && i == 0 )
01197                 //    return -1;
01198                 
01199                 if( b >= 0 )
01200                     dest[off + i] = (byte)b;
01201                 else if( i == 0 )
01202                     return -1;
01203                 else
01204                     break; // Out of 'for' loop
01205             }   // end for: each byte read
01206             return i;
01207         }   // end read

int Base64::InputStream::read  )  throws java.io.IOException [inline]
 

Reads enough of the input stream to convert to/from Base64 and returns the next byte.

Returns:
next byte
Since:
1.3
encode &&

Definition at line 1060 of file Base64.java.

References breakLines, buffer, bufferLength, Base64::DECODABET, Base64::decode4to3(), encode, Base64::encode3to4(), lineLength, Base64::MAX_LINE_LENGTH, numSigBytes, position, and Base64::WHITE_SPACE_ENC.

01061         { 
01062             // Do we need to get data?
01063             if( position < 0 )
01064             {
01065                 if( encode )
01066                 {
01067                     byte[] b3 = new byte[3];
01068                     int numBinaryBytes = 0;
01069                     for( int i = 0; i < 3; i++ )
01070                     {
01071                         try
01072                         { 
01073                             int b = in.read();
01074                             
01075                             // If end of stream, b is -1.
01076                             if( b >= 0 )
01077                             {
01078                                 b3[i] = (byte)b;
01079                                 numBinaryBytes++;
01080                             }   // end if: not end of stream
01081                             
01082                         }   // end try: read
01083                         catch( java.io.IOException e )
01084                         {   
01085                             // Only a problem if we got no data at all.
01086                             if( i == 0 )
01087                                 throw e;
01088                             
01089                         }   // end catch
01090                     }   // end for: each needed input byte
01091                     
01092                     if( numBinaryBytes > 0 )
01093                     {
01094                         encode3to4( b3, 0, numBinaryBytes, buffer, 0 );
01095                         position = 0;
01096                         numSigBytes = 4;
01097                     }   // end if: got data
01098                     else
01099                     {
01100                         return -1;
01101                     }   // end else
01102                 }   // end if: encoding
01103                 
01104                 // Else decoding
01105                 else
01106                 {
01107                     byte[] b4 = new byte[4];
01108                     int i = 0;
01109                     for( i = 0; i < 4; i++ )
01110                     {
01111                         // Read four "meaningful" bytes:
01112                         int b = 0;
01113                         do{ b = in.read(); }
01114                         while( b >= 0 && DECODABET[ b & 0x7f ] <= WHITE_SPACE_ENC );
01115                         
01116                         if( b < 0 )
01117                             break; // Reads a -1 if end of stream
01118                         
01119                         b4[i] = (byte)b;
01120                     }   // end for: each needed input byte
01121                     
01122                     if( i == 4 )
01123                     {
01124                         numSigBytes = decode4to3( b4, 0, buffer, 0 );
01125                         position = 0;
01126                     }   // end if: got four characters
01127                     else if( i == 0 ){
01128                         return -1;
01129                     }   // end else if: also padded correctly
01130                     else
01131                     {
01132                         // Must have broken out from above.
01133                         throw new java.io.IOException( "Improperly padded Base64 input." );
01134                     }   // end 
01135                     
01136                 }   // end else: decode
01137             }   // end else: get data
01138             
01139             // Got data?
01140             if( position >= 0 )
01141             {
01142                 // End of relevant data?
01143                 if( /*!encode &&*/ position >= numSigBytes )
01144                     return -1;
01145                 
01146                 if( encode && breakLines && lineLength >= MAX_LINE_LENGTH )
01147                 {
01148                     lineLength = 0;
01149                     return '\n';
01150                 }   // end if
01151                 else
01152                 {
01153                     lineLength++;   // This isn't important when decoding
01154                                     // but throwing an extra "if" seems
01155                                     // just as wasteful.
01156                     
01157                     int b = buffer[ position++ ];
01158 
01159                     if( position >= bufferLength )
01160                         position = -1;
01161 
01162                     return b & 0xFF; // This is how you "cast" a byte that's
01163                                      // intended to be unsigned.
01164                 }   // end else
01165             }   // end if: position >= 0
01166             
01167             // Else error
01168             else
01169             {   
01170                 // When JDK1.4 is more accepted, use an assertion here.
01171                 throw new java.io.IOException( "Error in Base64 code reading stream." );
01172             }   // end else
01173         }   // end read


Member Data Documentation

boolean Base64::InputStream::breakLines [private]
 

Definition at line 1006 of file Base64.java.

byte [] Base64::InputStream::buffer [private]
 

Definition at line 1002 of file Base64.java.

int Base64::InputStream::bufferLength [private]
 

Definition at line 1003 of file Base64.java.

boolean Base64::InputStream::encode [private]
 

Definition at line 1000 of file Base64.java.

int Base64::InputStream::lineLength [private]
 

Definition at line 1005 of file Base64.java.

int Base64::InputStream::numSigBytes [private]
 

Definition at line 1004 of file Base64.java.

int Base64::InputStream::position [private]
 

Definition at line 1001 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