You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by to...@apache.org on 2003/02/07 01:06:49 UTC

cvs commit: jakarta-commons-sandbox/codec/src/java/org/apache/commons/codec/binary Base64.java

tobrien     2003/02/06 16:06:49

  Modified:    codec/src/java/org/apache/commons/codec/binary Base64.java
  Log:
  Added option for chunked output ( RFC 2045 MIME compliance )
  
  Revision  Changes    Path
  1.2       +38 -18    jakarta-commons-sandbox/codec/src/java/org/apache/commons/codec/binary/Base64.java
  
  Index: Base64.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/codec/src/java/org/apache/commons/codec/binary/Base64.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Base64.java	4 Feb 2003 05:50:00 -0000	1.1
  +++ Base64.java	7 Feb 2003 00:06:49 -0000	1.2
  @@ -161,19 +161,28 @@
            return true;
        }
   
  +    public static byte[] encode( byte[] binaryData ) {
  +	return( encode( binaryData, false ) );
  +    }
  +
  +    public static byte[] encodeChunked( byte[] binaryData ) {
  +	return( encode( binaryData, true ) );
  +    }
  +
        /**
         * Encodes hex octects into Base64.
         *
         * @param binaryData Array containing binary data to encode.
         * @return Base64-encoded data.
         */
  -     public static byte[] encode( byte[] binaryData )
  +     public static byte[] encode( byte[] binaryData, boolean isChunked )
        {
            int      lengthDataBits    = binaryData.length*EIGHTBIT;
            int      fewerThan24bits   = lengthDataBits%TWENTYFOURBITGROUP;
            int      numberTriplets    = lengthDataBits/TWENTYFOURBITGROUP;
            byte     encodedData[]     = null;
            int      encodedDataLength = 0;
  +	 int      nbrChunks = 0;
   
            if (fewerThan24bits != 0)
            {
  @@ -186,11 +195,17 @@
                encodedDataLength = numberTriplets * 4;
            }
   
  -         // allow extra length for the separator
  -         int nbrChunks = (CHUNK_SEPARATOR.length == 0 ? 0 :
  -                          (int) Math.ceil((float) encodedDataLength / CHUNK_SIZE));
  +	 // If the output is to be "chunked" into 76 character sections, 
  +	 // for compliance with RFC 2045 MIME, then it is important to 
  +	 // allow for extra length to account for the separator(s)
  +	 if( isChunked ) {
  +
  +	     nbrChunks = (CHUNK_SEPARATOR.length == 0 ? 0 :
  +                          (int) Math.ceil((float) encodedDataLength / 
  +					  CHUNK_SIZE));
  +	     encodedDataLength += nbrChunks * CHUNK_SEPARATOR.length;
  +	 }
   
  -         encodedDataLength += nbrChunks * CHUNK_SEPARATOR.length;
            encodedData = new byte[encodedDataLength];
   
            byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0;
  @@ -230,15 +245,18 @@
   
                encodedIndex += 4;
   
  -             // this assumes that CHUNK_SIZE % 4 == 0
  -             if(encodedIndex == nextSeparatorIndex){
  -                 System.arraycopy(CHUNK_SEPARATOR, 0, encodedData,
  +	     // If we are chunking, let's put a chunk separator down.
  +	     if( isChunked ) {
  +		 // this assumes that CHUNK_SIZE % 4 == 0
  +		 if(encodedIndex == nextSeparatorIndex){
  +		     System.arraycopy(CHUNK_SEPARATOR, 0, encodedData,
                                     encodedIndex, CHUNK_SEPARATOR.length);
  -                 chunksSoFar++;
  -                 nextSeparatorIndex = (CHUNK_SIZE * (chunksSoFar + 1)) +
  +		     chunksSoFar++;
  +		     nextSeparatorIndex = (CHUNK_SIZE * (chunksSoFar + 1)) +
                                         (chunksSoFar * CHUNK_SEPARATOR.length);
  -                 encodedIndex += CHUNK_SEPARATOR.length;
  -             }
  +		     encodedIndex += CHUNK_SEPARATOR.length;
  +		 }
  +	     }
            }
   
            // form integral number of 6-bit groups
  @@ -274,11 +292,13 @@
                encodedData[encodedIndex + 3] = PAD;
            }
   
  -         // we also add a separator to the end of the final chunk.
  -         if(chunksSoFar < nbrChunks)
  -             System.arraycopy(CHUNK_SEPARATOR, 0, encodedData,
  +	 if( isChunked ) {
  +	     // we also add a separator to the end of the final chunk.
  +	     if(chunksSoFar < nbrChunks)
  +		 System.arraycopy(CHUNK_SEPARATOR, 0, encodedData,
                                 encodedDataLength - 
   			      CHUNK_SEPARATOR.length, CHUNK_SEPARATOR.length);
  +	 }
   
            return encodedData;
        }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org