001: /*
002: * @(#)BASE64Encoder.java 1.25 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027: package sun.misc;
028:
029: import java.io.OutputStream;
030: import java.io.InputStream;
031: import java.io.IOException;
032:
033: /**
034: * This class implements a BASE64 Character encoder as specified in RFC1521.
035: * This RFC is part of the MIME specification as published by the Internet
036: * Engineering Task Force (IETF). Unlike some other encoding schemes there
037: * is nothing in this encoding that indicates
038: * where a buffer starts or ends.
039: *
040: * This means that the encoded text will simply start with the first line
041: * of encoded text and end with the last line of encoded text.
042: *
043: * @version 1.19, 02/02/00
044: * @author Chuck McManis
045: * @see CharacterEncoder
046: * @see BASE64Decoder
047: */
048:
049: public class BASE64Encoder extends CharacterEncoder {
050:
051: /** this class encodes three bytes per atom. */
052: protected int bytesPerAtom() {
053: return (3);
054: }
055:
056: /**
057: * this class encodes 57 bytes per line. This results in a maximum
058: * of 57/3 * 4 or 76 characters per output line. Not counting the
059: * line termination.
060: */
061: protected int bytesPerLine() {
062: return (57);
063: }
064:
065: /** This array maps the characters to their 6 bit values */
066: private final static char pem_array[] = {
067: // 0 1 2 3 4 5 6 7
068: 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0
069: 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 1
070: 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 2
071: 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 3
072: 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 4
073: 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 5
074: 'w', 'x', 'y', 'z', '0', '1', '2', '3', // 6
075: '4', '5', '6', '7', '8', '9', '+', '/' // 7
076: };
077:
078: /**
079: * encodeAtom - Take three bytes of input and encode it as 4
080: * printable characters. Note that if the length in len is less
081: * than three is encodes either one or two '=' signs to indicate
082: * padding characters.
083: */
084: protected void encodeAtom(OutputStream outStream, byte data[],
085: int offset, int len) throws IOException {
086: byte a, b, c;
087:
088: if (len == 1) {
089: a = data[offset];
090: b = 0;
091: c = 0;
092: outStream.write(pem_array[(a >>> 2) & 0x3F]);
093: outStream.write(pem_array[((a << 4) & 0x30)
094: + ((b >>> 4) & 0xf)]);
095: outStream.write('=');
096: outStream.write('=');
097: } else if (len == 2) {
098: a = data[offset];
099: b = data[offset + 1];
100: c = 0;
101: outStream.write(pem_array[(a >>> 2) & 0x3F]);
102: outStream.write(pem_array[((a << 4) & 0x30)
103: + ((b >>> 4) & 0xf)]);
104: outStream.write(pem_array[((b << 2) & 0x3c)
105: + ((c >>> 6) & 0x3)]);
106: outStream.write('=');
107: } else {
108: a = data[offset];
109: b = data[offset + 1];
110: c = data[offset + 2];
111: outStream.write(pem_array[(a >>> 2) & 0x3F]);
112: outStream.write(pem_array[((a << 4) & 0x30)
113: + ((b >>> 4) & 0xf)]);
114: outStream.write(pem_array[((b << 2) & 0x3c)
115: + ((c >>> 6) & 0x3)]);
116: outStream.write(pem_array[c & 0x3F]);
117: }
118: }
119: }
|