Here's some source I wrote, since it was intended to only encode small
pieces of data, it doesn't have the necessary line break at or before the
72nd character, but that shouldn't be hard to add.
    (*Chris*)

/**
 * Base64 - Base 64 Transfer Encoding
 *
 * @author Chris Pratt
 * @version 1.0
 *
 * 3/15/1999
 */

import java.util.StringTokenizer;

public class Base64 {
  private static final char[] DIGIT =
{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S'
,'T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l'
,'m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4'
,'5','6','7','8','9','+','/'};
  private static final byte[] ORD =
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

0,0,0,0,0,0,0,0,0,0,0,62,0,0,0,63,52,53,54,55,56,57,58,59,60,61,0,0,0,0,0,0,

0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,0,0,0,
0,0,

0,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50
,51,0,0,0,0,0};

    /**
     * Constructor
     */
  private Base64 () {
  } file://Base64

  /**
   * Encode the buffer in base64 mode
   *
   * @param bytes the bytes to be encoded
   * @return the encoded string
   */
  private static String encode (byte[] bytes) {
    StringBuffer buf = new StringBuffer();
    int i = 0,pad,top = bytes.length - (bytes.length % 3);
    long n;

    while(i < top) {
      n = ((((long)bytes[i++]) & 0xFF) << 16) | ((((long)bytes[i++]) & 0xFF)
<< 8) | (((long)bytes[i++]) & 0xFF);
      buf.append(DIGIT[(int)((n >>> 18) & 0x3F)]);
      buf.append(DIGIT[(int)((n >>> 12) & 0x3F)]);
      buf.append(DIGIT[(int)((n >>> 6) & 0x3F)]);
      buf.append(DIGIT[(int)(n & 0x3F)]);
    }
    if(i < bytes.length) {
      n = (((long)bytes[i++]) & 0xFF) << 16;
      pad = 2;
      if(i < bytes.length) {
        n |= (((long)bytes[i]) & 0xFF) << 8;
        pad = 1;
      }
      int s = 18;
      for(i = 0;i < 4 - pad;i++) {
        buf.append(DIGIT[(int)((n >>> s) & 0x3F)]);
        s -= 6;
      }
      for(i = 0;i < pad;i++) {
        buf.append('=');
      }
    }
    return buf.toString();
  } file://encode

  /**
   * Encode the String in base64
   *
   * @param buf The String to be encoded
   * @return the encoded String
   */
  public static String encode (String buf) {
    return encode(buf.getBytes());
  } file://encode

  /**
   * Decode the String from base64
   *
   * @param bytes The Byte buffers to be decoded
   * @return Decoded byte array
   */
  public static byte[] decode (byte[] bytes) {
    long n;
    int i = 0,j = 0;
    int len = (bytes.length * 4) / 3;
    if(((char)bytes[bytes.length - 1]) == '=') {
      if(((char)bytes[bytes.length - 2]) == '=') {
        --len;
      }
      --len;
    }
    byte[] buf = new byte[len];
    while(i < bytes.length) {
      n = (ORD[(int)bytes[i++]] << 18) | (ORD[(int)bytes[i++]] << 12) |
(ORD[(int)bytes[i++]] << 6) | ORD[(int)bytes[i++]];
      buf[j++] = (byte)((n >>> 16) & 0xFF);
      if(--len > 0) {
        buf[j++] = (byte)((n >>> 8) & 0xFF);
        if(--len > 0) {
          buf[j++] = (byte)(n & 0xFF);
          --len;
        }
      }
    }
    return buf;
  } file://decode

  /**
   * Decode the String from base64
   *
   * @param buf The Encoded String
   * @return The Decoded String
   */
  public static String decode (String buf) {
    StringBuffer data = new StringBuffer();
    StringTokenizer token = new StringTokenizer(buf,"\r\n");
    while(token.hasMoreTokens()) {
      data.append(token.nextToken());
    }
    return new String(decode(data.toString().getBytes()));
  } file://decode

  /**
   * Program Entry-point
   *
   * @param args Command-line Arguments
   */
  public static void main (String[] args) {
    String encoded = encode("This is a test");
    System.out.println("Encoded: " + encoded);
    System.out.println("Decoded: " + decode(encoded));
  } file://main

} file://*Base64

----- Original Message -----
From: Java List <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, August 19, 1999 9:40 AM
Subject: Base64 Encoder Class


> Can anyone recommend a good (free) java class library for encoding strings
> into a base64 strings?
>
> -Nash
>
>
___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to