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

Base64::OutputStream Class Reference

List of all members.

Public Member Functions

 OutputStream (java.io.OutputStream out)
 OutputStream (java.io.OutputStream out, int options)
void write (int theByte) throws java.io.IOException
void write (byte[] theBytes, int off, int len) throws java.io.IOException
void flushBase64 () throws java.io.IOException
void close () throws java.io.IOException
void suspendEncoding () throws java.io.IOException
void resumeEncoding ()

Private Attributes

boolean encode
int position
byte[] buffer
int bufferLength
int lineLength
boolean breakLines
byte[] b4
boolean suspendEncoding

Detailed Description

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

See also:
Base64
Since:
1.3

Definition at line 1228 of file Base64.java.


Constructor & Destructor Documentation

Base64::OutputStream::OutputStream java.io.OutputStream  out  )  [inline]
 

Constructs a Base64.OutputStream in ENCODE mode.

Parameters:
out the java.io.OutputStream to which data will be written.
Since:
1.3

Definition at line 1245 of file Base64.java.

References Base64::ENCODE.

01246         {   
01247             this( out, ENCODE );
01248         }   // end constructor

Base64::OutputStream::OutputStream java.io.OutputStream  out,
int  options
[inline]
 

Constructs a Base64.OutputStream 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.OutputStream( out, Base64.ENCODE )

Parameters:
out the java.io.OutputStream to which data will be written.
options Specified options.
See also:
Base64::ENCODE

Base64::DECODE

Base64::DONT_BREAK_LINES

Since:
1.3

Definition at line 1271 of file Base64.java.

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

01272         {   
01273             super( out );
01274             this.breakLines   = (options & DONT_BREAK_LINES) != DONT_BREAK_LINES;
01275             this.encode       = (options & ENCODE) == ENCODE;
01276             this.bufferLength = encode ? 3 : 4;
01277             this.buffer       = new byte[ bufferLength ];
01278             this.position     = 0;
01279             this.lineLength   = 0;
01280             this.suspendEncoding = false;
01281             this.b4           = new byte[4];
01282         }   // end constructor


Member Function Documentation

void Base64::OutputStream::close  )  throws java.io.IOException [inline]
 

Flushes and closes (I think, in the superclass) the stream.

Since:
1.3

Definition at line 1403 of file Base64.java.

References buffer, and flushBase64().

01404         {
01405             // 1. Ensure that pending characters are written
01406             flushBase64();
01407 
01408             // 2. Actually close the stream
01409             // Base class both flushes and closes.
01410             super.close();
01411             
01412             buffer = null;
01413             out    = null;
01414         }   // end close

void Base64::OutputStream::flushBase64  )  throws java.io.IOException [inline]
 

Method added by PHIL. [Thanks, PHIL. -Rob] This pads the buffer without closing the stream.

Definition at line 1380 of file Base64.java.

References b4, buffer, encode, Base64::encode3to4(), and position.

01381         {
01382             if( position > 0 )
01383             {
01384                 if( encode )
01385                 {
01386                     out.write( encode3to4( b4, buffer, position ) );
01387                     position = 0;
01388                 }   // end if: encoding
01389                 else
01390                 {
01391                     throw new java.io.IOException( "Base64 input not properly padded." );
01392                 }   // end else: decoding
01393             }   // end if: buffer partially full
01394 
01395         }   // end flush

void Base64::OutputStream::resumeEncoding  )  [inline]
 

Resumes encoding of the stream. May be helpful if you need to embed a piece of base640-encoded data in a stream.

Since:
1.5.1

Definition at line 1439 of file Base64.java.

01440         {
01441             this.suspendEncoding = false;
01442         }   // end resumeEncoding

void Base64::OutputStream::suspendEncoding  )  throws java.io.IOException [inline]
 

Suspends encoding of the stream. May be helpful if you need to embed a piece of base640-encoded data in a stream.

Since:
1.5.1

Definition at line 1425 of file Base64.java.

References flushBase64().

01426         {
01427             flushBase64();
01428             this.suspendEncoding = true;
01429         }   // end suspendEncoding

void Base64::OutputStream::write byte[]  theBytes,
int  off,
int  len
throws java.io.IOException [inline]
 

Calls write(int) repeatedly until len bytes are written.

Parameters:
theBytes array from which to read bytes
off offset for array
len max number of bytes to read into array
Since:
1.3

Definition at line 1358 of file Base64.java.

References suspendEncoding(), and write().

01359         {
01360             // Encoding suspended?
01361             if( suspendEncoding )
01362             {
01363                 super.out.write( theBytes, off, len );
01364                 return;
01365             }   // end if: supsended
01366             
01367             for( int i = 0; i < len; i++ )
01368             {
01369                 write( theBytes[ off + i ] );
01370             }   // end for: each byte written
01371             
01372         }   // end write

void Base64::OutputStream::write int  theByte  )  throws java.io.IOException [inline]
 

Writes the byte to the output stream after converting to/from Base64 notation. When encoding, bytes are buffered three at a time before the output stream actually gets a write() call. When decoding, bytes are buffered four at a time.

Parameters:
theByte the byte to write
Since:
1.3

Definition at line 1297 of file Base64.java.

References b4, breakLines, buffer, bufferLength, Base64::DECODABET, encode, Base64::encode3to4(), lineLength, Base64::MAX_LINE_LENGTH, Base64::NEW_LINE, position, suspendEncoding(), and Base64::WHITE_SPACE_ENC.

01298         {
01299             // Encoding suspended?
01300             if( suspendEncoding )
01301             {
01302                 super.out.write( theByte );
01303                 return;
01304             }   // end if: supsended
01305             
01306             // Encode?
01307             if( encode )
01308             {
01309                 buffer[ position++ ] = (byte)theByte;
01310                 if( position >= bufferLength )  // Enough to encode.
01311                 {
01312                     out.write( encode3to4( b4, buffer, bufferLength ) );
01313 
01314                     lineLength += 4;
01315                     if( breakLines && lineLength >= MAX_LINE_LENGTH )
01316                     {
01317                         out.write( NEW_LINE );
01318                         lineLength = 0;
01319                     }   // end if: end of line
01320 
01321                     position = 0;
01322                 }   // end if: enough to output
01323             }   // end if: encoding
01324 
01325             // Else, Decoding
01326             else
01327             {
01328                 // Meaningful Base64 character?
01329                 if( DECODABET[ theByte & 0x7f ] > WHITE_SPACE_ENC )
01330                 {
01331                     buffer[ position++ ] = (byte)theByte;
01332                     if( position >= bufferLength )  // Enough to output.
01333                     {
01334                         int len = Base64.decode4to3( buffer, 0, b4, 0 );
01335                         out.write( b4, 0, len );
01336                         //out.write( Base64.decode4to3( buffer ) );
01337                         position = 0;
01338                     }   // end if: enough to output
01339                 }   // end if: meaningful base64 character
01340                 else if( DECODABET[ theByte & 0x7f ] != WHITE_SPACE_ENC )
01341                 {
01342                     throw new java.io.IOException( "Invalid character in Base64 data." );
01343                 }   // end else: not white space either
01344             }   // end else: decoding
01345         }   // end write


Member Data Documentation

byte [] Base64::OutputStream::b4 [private]
 

Definition at line 1236 of file Base64.java.

boolean Base64::OutputStream::breakLines [private]
 

Definition at line 1235 of file Base64.java.

byte [] Base64::OutputStream::buffer [private]
 

Definition at line 1232 of file Base64.java.

int Base64::OutputStream::bufferLength [private]
 

Definition at line 1233 of file Base64.java.

boolean Base64::OutputStream::encode [private]
 

Definition at line 1230 of file Base64.java.

int Base64::OutputStream::lineLength [private]
 

Definition at line 1234 of file Base64.java.

int Base64::OutputStream::position [private]
 

Definition at line 1231 of file Base64.java.

boolean Base64::OutputStream::suspendEncoding [private]
 

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