home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / misc / BASE64Encoder.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  3.7 KB  |  108 lines

  1. /*
  2.  * @(#)BASE64Encoder.java    1.8 95/10/08 Chuck McManis
  3.  *
  4.  * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package sun.misc;
  20.  
  21. import java.io.OutputStream;
  22. import java.io.InputStream;
  23. import java.io.PrintStream;
  24. import java.io.IOException;
  25.  
  26. /**
  27.  * This class implements a BASE64 Character encoder as specified in RFC1521.
  28.  * This RFC is part of the MIME specification as published by the Internet 
  29.  * Engineering Task Force (IETF). Unlike some other encoding schemes there 
  30.  * is nothing in this encoding that indicates
  31.  * where a buffer starts or ends.
  32.  *
  33.  * This means that the encoded text will simply start with the first line
  34.  * of encoded text and end with the last line of encoded text.
  35.  *
  36.  * @version    1.8, 10/08/95
  37.  * @author    Chuck McManis
  38.  * @see        CharacterEncoder
  39.  * @see        BASE64Decoder
  40.  */
  41.  
  42. public class BASE64Encoder extends CharacterEncoder {
  43.     
  44.     /** this class encodes three bytes per atom. */
  45.     int bytesPerAtom() {
  46.     return (3);
  47.     }
  48.  
  49.     /** 
  50.      * this class encodes 57 bytes per line. This results in a maximum
  51.      * of 57/3 * 4 or 76 characters per output line. Not counting the
  52.      * line termination.
  53.      */
  54.     int bytesPerLine() {
  55.     return (57);
  56.     }
  57.  
  58.     /** This array maps the characters to their 6 bit values */
  59.     private final static char pem_array[] = {
  60.     //       0   1   2   3   4   5   6   7
  61.         'A','B','C','D','E','F','G','H', // 0
  62.         'I','J','K','L','M','N','O','P', // 1
  63.         'Q','R','S','T','U','V','W','X', // 2
  64.         'Y','Z','a','b','c','d','e','f', // 3
  65.         'g','h','i','j','k','l','m','n', // 4
  66.         'o','p','q','r','s','t','u','v', // 5
  67.         'w','x','y','z','0','1','2','3', // 6
  68.         '4','5','6','7','8','9','+','/'  // 7
  69.     };
  70.  
  71.     /** 
  72.      * enocodeAtom - Take three bytes of input and encode it as 4
  73.      * printable characters. Note that if the length in len is less
  74.      * than three is encodes either one or two '=' signs to indicate
  75.      * padding characters.
  76.      */
  77.     void encodeAtom(OutputStream outStream, byte data[], int offset, int len) 
  78.     throws IOException {
  79.     byte a, b, c;
  80.  
  81.     if (len == 1) {
  82.         a = data[offset];
  83.         b = 0;
  84.         c = 0;
  85.         outStream.write(pem_array[(a >>> 2) & 0x3F]);
  86.         outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
  87.         outStream.write('=');
  88.         outStream.write('=');
  89.     } else if (len == 2) {
  90.         a = data[offset];
  91.         b = data[offset+1];
  92.         c = 0;
  93.         outStream.write(pem_array[(a >>> 2) & 0x3F]);
  94.         outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
  95.         outStream.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);
  96.         outStream.write('=');
  97.     } else {
  98.         a = data[offset];
  99.         b = data[offset+1];
  100.         c = data[offset+2];
  101.         outStream.write(pem_array[(a >>> 2) & 0x3F]);
  102.         outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
  103.         outStream.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);
  104.         outStream.write(pem_array[c & 0x3F]);
  105.     }
  106.     }
  107. }
  108.