All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.draagon.util.Base64 Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2001 Draagon Software LLC. All Rights Reserved.
 *
 * This software is the proprietary information of Draagon Software LLC.
 * Use is subject to license terms.
 */

package com.draagon.util;


public class Base64
{
  // Encode characters
  private static char[] alphabet =
      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".toCharArray();

  private static byte[] codes = new byte[ 256 ];

  static
  {
    for ( int i = 0; i < 256; i++ ) codes[ i ] = -1;
    for ( int i = 'A'; i <= 'Z'; i++ ) codes[ i ] = (byte) (     i - 'A' );
    for ( int i = 'a'; i <= 'z'; i++ ) codes[ i ] = (byte) ( 26 + i - 'a' );
    for ( int i = '0'; i <= '9'; i++ ) codes[ i ] = (byte) ( 52 + i - '0' );
    codes[ '+' ] = 62;
    codes[ '/' ] = 63;
  }

  public static String encode( String data )
  {
    char [] tmp = encode( data.toCharArray() );
    return new String( tmp );
  }

  public static String decode( String data )
  {
    char [] tmp = decode( data.toCharArray() );
    return new String( tmp );
  }

  /**
  * returns an array of base64-encoded characters to represent the
  * passed data array.
  *
  * @param data the array of bytes to encode
  * @return base64-coded character array.
  */
  public static char[] encode( char[] data )
  {
    char[] out = new char[((data.length + 2) / 3) * 4];

    for (int i=0, index=0; i>= 6;
        out[index+2] = alphabet[(trip? (val & 0x3F): 64)];
        val >>= 6;
        out[index+1] = alphabet[val & 0x3F];
        val >>= 6;
        out[index+0] = alphabet[val & 0x3F];
    }

    return out;
  }

  /**
   * Decodes a BASE-64 encoded stream to recover the original
   * data. White space before and after will be trimmed away,
   * but no other manipulation of the input will be performed.
   **/
  public static char[] decode( char[] data )
  {
    int tempLen = data.length;
    for( int ix=0; ix 255) || codes[ data[ix] ] < 0 )
            --tempLen;  // ignore non-valid chars and padding
    }
    // calculate required length:
    //  -- 3 bytes for every 4 valid base64 chars
    //  -- plus 2 bytes if there are 3 extra base64 chars,
    //     or plus 1 byte if there are 2 extra.

    int len = (tempLen / 4) * 3;
    if ((tempLen % 4) == 3) len += 2;
    if ((tempLen % 4) == 2) len += 1;

    char[] out = new char[ len ];

    int shift = 0;   // # of excess bits stored in accum
    int accum = 0;   // excess bits
    int index = 0;

    // we now go through the entire array (NOT using the 'tempLen' value)
    for (int ix=0; ix255)? -1: codes[ data[ix] ];

        if ( value >= 0 )           // skip over non-code
        {
            accum <<= 6;            // bits shift up by 6 each time thru
            shift += 6;             // loop, with new bits being put in
            accum |= value;         // at the bottom.
            if ( shift >= 8 )       // whenever there are 8 or more shifted in,
            {
                shift -= 8;         // write them out (from the top, leaving any
                int b = ((accum >> shift) & 0xff);
                out[ index++ ] = (char) b;     // excess at the bottom for next iteration.
            }
        }
    }

    // if there is STILL something wrong we just have to throw up now!
    if( index != out.length)
    {
        throw new Error( "Miscalculated data length (wrote " + index + " instead of " + out.length + ")" );
    }

    return out;
  }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy