Logo Search packages:      
Sourcecode: pcalendar version File versions  Download package

static byte [] net::sf::linuxorg::pcal::common::Base64::encodeBytesToBytes ( byte[]  source,
int  off,
int  len,
int  options 
) throws java.io.IOException [inline, static]

Similar to encodeBytes(byte[], int, int, int) but returns a byte array instead of instantiating a String. This is more efficient if you're working with I/O streams and have large data sets to encode.

Parameters:
sourceThe data to convert
offOffset in array where conversion should begin
lenLength of data to convert
optionsSpecified options
Returns:
The Base64-encoded data as a String
See also:
Base64::GZIP
Base64::DO_BREAK_LINES
Exceptions:
java.io.IOExceptionif there is an error
NullPointerExceptionif source array is null
IllegalArgumentExceptionif source array, offset, or length are invalid
Since:
2.3.1

Definition at line 851 of file Base64.java.

References DO_BREAK_LINES, ENCODE, encode3to4(), GZIP, MAX_LINE_LENGTH, and NEW_LINE.

                                                                                                                       {

        if( source == null ){
            throw new NullPointerException( "Cannot serialize a null array." ); //$NON-NLS-1$
        }   // end if: null

        if( off < 0 ){
            throw new IllegalArgumentException( "Cannot have negative offset: " + off ); //$NON-NLS-1$
        }   // end if: off < 0

        if( len < 0 ){
            throw new IllegalArgumentException( "Cannot have length offset: " + len ); //$NON-NLS-1$
        }   // end if: len < 0

        if( off + len > source.length  ){
            throw new IllegalArgumentException(
            String.format( "Cannot have offset of %d and length of %d with array of length %d", off,len,source.length)); //$NON-NLS-1$
        }   // end if: off < 0



        // Compress?
        if( (options & GZIP) > 0 ) {
            java.io.ByteArrayOutputStream  baos  = null;
            java.util.zip.GZIPOutputStream gzos  = null;
            Base64.OutputStream            b64os = null;

            try {
                // GZip -> Base64 -> ByteArray
                baos = new java.io.ByteArrayOutputStream();
                b64os = new Base64.OutputStream( baos, ENCODE | options );
                gzos  = new java.util.zip.GZIPOutputStream( b64os );

                gzos.write( source, off, len );
                gzos.close();
            }   // end try
            catch( java.io.IOException e ) {
                // Catch it and then throw it immediately so that
                // the finally{} block is called for cleanup.
                throw e;
            }   // end catch
            finally {
                try{ gzos.close();  } catch( Exception e ){}
                try{ b64os.close(); } catch( Exception e ){}
                try{ baos.close();  } catch( Exception e ){}
            }   // end finally

            return baos.toByteArray();
        }   // end if: compress

        // Else, don't compress. Better not to use streams at all then.
        else {
            boolean breakLines = (options & DO_BREAK_LINES) > 0;

            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, options );

                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, options );
                e += 4;
            }   // end if: some padding needed


            byte[] finalOut = new byte[e];
            System.arraycopy(outBuff,0, finalOut,0,e);
            return finalOut;
        
        }   // end else: don't compress

    }   // end encodeBytesToBytes

Here is the call graph for this function:


Generated by  Doxygen 1.6.0   Back to index