public static byte[] encodeBase64(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) {
    encodedDataLength=(numberTriplets + 1) * 4;
  }
 else {
    encodedDataLength=numberTriplets * 4;
  }
  if (isChunked) {
    nbrChunks=(CHUNK_SEPARATOR.length == 0 ? 0 : (int)Math.ceil((float)encodedDataLength / CHUNK_SIZE));
    encodedDataLength+=nbrChunks * CHUNK_SEPARATOR.length;
  }
  encodedData=new byte[encodedDataLength];
  byte k=0, l=0, b1=0, b2=0, b3=0;
  int encodedIndex=0;
  int dataIndex=0;
  int i=0;
  int nextSeparatorIndex=CHUNK_SIZE;
  int chunksSoFar=0;
  for (i=0; i < numberTriplets; i++) {
    dataIndex=i * 3;
    b1=binaryData[dataIndex];
    b2=binaryData[dataIndex + 1];
    b3=binaryData[dataIndex + 2];
    l=(byte)(b2 & 0x0f);
    k=(byte)(b1 & 0x03);
    byte val1=((b1 & SIGN) == 0) ? (byte)(b1 >> 2) : (byte)((b1) >> 2 ^ 0xc0);
    byte val2=((b2 & SIGN) == 0) ? (byte)(b2 >> 4) : (byte)((b2) >> 4 ^ 0xf0);
    byte val3=((b3 & SIGN) == 0) ? (byte)(b3 >> 6) : (byte)((b3) >> 6 ^ 0xfc);
    encodedData[encodedIndex]=lookUpBase64Alphabet[val1];
    encodedData[encodedIndex + 1]=lookUpBase64Alphabet[val2 | (k << 4)];
    encodedData[encodedIndex + 2]=lookUpBase64Alphabet[(l << 2) | val3];
    encodedData[encodedIndex + 3]=lookUpBase64Alphabet[b3 & 0x3f];
    encodedIndex+=4;
    if (isChunked) {
      if (encodedIndex == nextSeparatorIndex) {
        System.arraycopy(CHUNK_SEPARATOR,0,encodedData,encodedIndex,CHUNK_SEPARATOR.length);
        chunksSoFar++;
        nextSeparatorIndex=(CHUNK_SIZE * (chunksSoFar + 1)) + (chunksSoFar * CHUNK_SEPARATOR.length);
        encodedIndex+=CHUNK_SEPARATOR.length;
      }
    }
  }
  dataIndex=i * 3;
  if (fewerThan24bits == EIGHTBIT) {
    b1=binaryData[dataIndex];
    k=(byte)(b1 & 0x03);
    byte val1=((b1 & SIGN) == 0) ? (byte)(b1 >> 2) : (byte)((b1) >> 2 ^ 0xc0);
    encodedData[encodedIndex]=lookUpBase64Alphabet[val1];
    encodedData[encodedIndex + 1]=lookUpBase64Alphabet[k << 4];
    encodedData[encodedIndex + 2]=PAD;
    encodedData[encodedIndex + 3]=PAD;
  }
 else   if (fewerThan24bits == SIXTEENBIT) {
    b1=binaryData[dataIndex];
    b2=binaryData[dataIndex + 1];
    l=(byte)(b2 & 0x0f);
    k=(byte)(b1 & 0x03);
    byte val1=((b1 & SIGN) == 0) ? (byte)(b1 >> 2) : (byte)((b1) >> 2 ^ 0xc0);
    byte val2=((b2 & SIGN) == 0) ? (byte)(b2 >> 4) : (byte)((b2) >> 4 ^ 0xf0);
    encodedData[encodedIndex]=lookUpBase64Alphabet[val1];
    encodedData[encodedIndex + 1]=lookUpBase64Alphabet[val2 | (k << 4)];
    encodedData[encodedIndex + 2]=lookUpBase64Alphabet[l << 2];
    encodedData[encodedIndex + 3]=PAD;
  }
  if (isChunked) {
    if (chunksSoFar < nbrChunks) {
      System.arraycopy(CHUNK_SEPARATOR,0,encodedData,encodedDataLength - CHUNK_SEPARATOR.length,CHUNK_SEPARATOR.length);
    }
  }
  return encodedData;
}

public static byte [ ] encodeBase64 ( 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 ) { encodedDataLength = ( numberTriplets + 1 ) * 4 ; } else { encodedDataLength = numberTriplets * 4 ; } if ( isChunked ) { nbrChunks = ( CHUNK_SEPARATOR . length == 0 ? 0 : ( int ) Math . ceil ( ( float ) encodedDataLength / CHUNK_SIZE ) ) ; encodedDataLength += nbrChunks * CHUNK_SEPARATOR . length ; } encodedData = new byte [ encodedDataLength ] ; byte k = 0 , l = 0 , b1 = 0 , b2 = 0 , b3 = 0 ; int encodedIndex = 0 ; int dataIndex = 0 ; int i = 0 ; int nextSeparatorIndex = CHUNK_SIZE ; int chunksSoFar = 0 ; for ( i = 0 ; i < numberTriplets ; i ++ ) { dataIndex = i * 3 ; b1 = binaryData [ dataIndex ] ; b2 = binaryData [ dataIndex + 1 ] ; b3 = binaryData [ dataIndex + 2 ] ; l = ( byte ) ( b2 & 0x0f ) ; k = ( byte ) ( b1 & 0x03 ) ; byte val1 = ( ( b1 & SIGN ) == 0 ) ? ( byte ) ( b1 > > 2 ) : ( byte ) ( ( b1 ) > > 2 ^ 0xc0 ) ; byte val2 = ( ( b2 & SIGN ) == 0 ) ? ( byte ) ( b2 > > 4 ) : ( byte ) ( ( b2 ) > > 4 ^ 0xf0 ) ; byte val3 = ( ( b3 & SIGN ) == 0 ) ? ( byte ) ( b3 > > 6 ) : ( byte ) ( ( b3 ) > > 6 ^ 0xfc ) ; encodedData [ encodedIndex ] = lookUpBase64Alphabet [ val1 ] ; encodedData [ encodedIndex + 1 ] = lookUpBase64Alphabet [ val2 | ( k << 4 ) ] ; encodedData [ encodedIndex + 2 ] = lookUpBase64Alphabet [ ( l << 2 ) | val3 ] ; encodedData [ encodedIndex + 3 ] = lookUpBase64Alphabet [ b3 & 0x3f ] ; encodedIndex += 4 ; if ( isChunked ) { if ( encodedIndex == nextSeparatorIndex ) { System . arraycopy ( CHUNK_SEPARATOR , 0 , encodedData , encodedIndex , CHUNK_SEPARATOR . length ) ; chunksSoFar ++ ; nextSeparatorIndex = ( CHUNK_SIZE * ( chunksSoFar + 1 ) ) + ( chunksSoFar * CHUNK_SEPARATOR . length ) ; encodedIndex += CHUNK_SEPARATOR . length ; } } } dataIndex = i * 3 ; if ( fewerThan24bits == EIGHTBIT ) { b1 = binaryData [ dataIndex ] ; k = ( byte ) ( b1 & 0x03 ) ; byte val1 = ( ( b1 & SIGN ) == 0 ) ? ( byte ) ( b1 > > 2 ) : ( byte ) ( ( b1 ) > > 2 ^ 0xc0 ) ; encodedData [ encodedIndex ] = lookUpBase64Alphabet [ val1 ] ; encodedData [ encodedIndex + 1 ] = lookUpBase64Alphabet [ k << 4 ] ; encodedData [ encodedIndex + 2 ] = PAD ; encodedData [ encodedIndex + 3 ] = PAD ; } else if ( fewerThan24bits == SIXTEENBIT ) { b1 = binaryData [ dataIndex ] ; b2 = binaryData [ dataIndex + 1 ] ; l = ( byte ) ( b2 & 0x0f ) ; k = ( byte ) ( b1 & 0x03 ) ; byte val1 = ( ( b1 & SIGN ) == 0 ) ? ( byte ) ( b1 > > 2 ) : ( byte ) ( ( b1 ) > > 2 ^ 0xc0 ) ; byte val2 = ( ( b2 & SIGN ) == 0 ) ? ( byte ) ( b2 > > 4 ) : ( byte ) ( ( b2 ) > > 4 ^ 0xf0 ) ; encodedData [ encodedIndex ] = lookUpBase64Alphabet [ val1 ] ; encodedData [ encodedIndex + 1 ] = lookUpBase64Alphabet [ val2 | ( k << 4 ) ] ; encodedData [ encodedIndex + 2 ] = lookUpBase64Alphabet [ l << 2 ] ; encodedData [ encodedIndex + 3 ] = PAD ; } if ( isChunked ) { if ( chunksSoFar < nbrChunks ) { System . arraycopy ( CHUNK_SEPARATOR , 0 , encodedData , encodedDataLength - CHUNK_SEPARATOR . length , CHUNK_SEPARATOR . length ) ; } } return encodedData ; }
encodes binary data using the base64 algorithm , optionally chunking the output into 76 character blocks .
