001: /*
002: *
003: * The contents of this file are subject to the Netscape Public
004: * License Version 1.1 (the "License"); you may not use this file
005: * except in compliance with the License. You may obtain a copy of
006: * the License at http://www.mozilla.org/NPL/
007: *
008: * Software distributed under the License is distributed on an "AS
009: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
010: * implied. See the License for the specific language governing
011: * rights and limitations under the License.
012: *
013: * The Original Code is mozilla.org code.
014: *
015: * The Initial Developer of the Original Code is Netscape
016: * Communications Corporation. Portions created by Netscape are
017: * Copyright (C) 1999 Netscape Communications Corporation. All
018: * Rights Reserved.
019: *
020: * Contributor(s):
021: */
022:
023: package org.infozone.tools;
024:
025: /**
026: * Byte to text encoder using base 64 encoding. To create a base 64
027: * encoding of a byte stream call {@link #translate} for every
028: * sequence of bytes and {@link #getCharArray} to mark closure of
029: * the byte stream and retrieve the text presentation.
030: *
031: * @author Based on code from the Mozilla Directory SDK
032: */
033: public final class MimeBase64Encoder {
034:
035: private StringBuffer out = new StringBuffer();
036:
037: private int buf = 0; // a 24-bit quantity
038: private int buf_bytes = 0; // how many octets are set in it
039: private char line[] = new char[74]; // output buffer
040: private int line_length = 0; // output buffer fill pointer
041:
042: static private final byte crlf[] = "\r\n".getBytes();
043:
044: static private final char map[] = { 'A', 'B', 'C', 'D', 'E', 'F',
045: 'G', 'H', // 0-7
046: 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 8-15
047: 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 16-23
048: 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 24-31
049: 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 32-39
050: 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 40-47
051: 'w', 'x', 'y', 'z', '0', '1', '2', '3', // 48-55
052: '4', '5', '6', '7', '8', '9', '+', '/', // 56-63
053: };
054:
055: private final void encode_token() {
056: int i = line_length;
057: line[i] = map[0x3F & (buf >> 18)]; // sextet 1 (octet 1)
058: line[i + 1] = map[0x3F & (buf >> 12)]; // sextet 2 (octet 1 and 2)
059: line[i + 2] = map[0x3F & (buf >> 6)]; // sextet 3 (octet 2 and 3)
060: line[i + 3] = map[0x3F & buf]; // sextet 4 (octet 3)
061: line_length += 4;
062: buf = 0;
063: buf_bytes = 0;
064: }
065:
066: private final void encode_partial_token() {
067: int i = line_length;
068: line[i] = map[0x3F & (buf >> 18)]; // sextet 1 (octet 1)
069: line[i + 1] = map[0x3F & (buf >> 12)]; // sextet 2 (octet 1 and 2)
070:
071: if (buf_bytes == 1)
072: line[i + 2] = '=';
073: else
074: line[i + 2] = map[0x3F & (buf >> 6)]; // sextet 3 (octet 2 and 3)
075:
076: if (buf_bytes <= 2)
077: line[i + 3] = '=';
078: else
079: line[i + 3] = map[0x3F & buf]; // sextet 4 (octet 3)
080: line_length += 4;
081: buf = 0;
082: buf_bytes = 0;
083: }
084:
085: private final void flush_line() {
086: out.append(line, 0, line_length);
087: line_length = 0;
088: }
089:
090: /** Given a sequence of input bytes, produces a sequence of output bytes
091: using the base64 encoding. If there are bytes in `out' already, the
092: new bytes are appended, so the caller should do `out.setLength(0)'
093: first if that's desired.
094: */
095: public final void translate(byte[] in) {
096: int in_length = in.length;
097:
098: for (int i = 0; i < in_length; i++) {
099: if (buf_bytes == 0)
100: buf = (buf & 0x00FFFF) | (in[i] << 16);
101: else if (buf_bytes == 1)
102: buf = (buf & 0xFF00FF) | ((in[i] << 8) & 0x00FFFF);
103: else
104: buf = (buf & 0xFFFF00) | (in[i] & 0x0000FF);
105:
106: if ((++buf_bytes) == 3) {
107: encode_token();
108: if (line_length >= 72) {
109: flush_line();
110: }
111: }
112:
113: if (i == (in_length - 1)) {
114: if ((buf_bytes > 0) && (buf_bytes < 3))
115: encode_partial_token();
116: if (line_length > 0)
117: flush_line();
118: }
119: }
120:
121: for (int i = 0; i < line.length; i++)
122: line[i] = 0;
123: }
124:
125: public char[] getCharArray() {
126: char[] ch;
127:
128: if (buf_bytes != 0)
129: encode_partial_token();
130: flush_line();
131: for (int i = 0; i < line.length; i++)
132: line[i] = 0;
133: ch = new char[out.length()];
134: out.getChars(0, out.length(), ch, 0);
135: return ch;
136: }
137: }
|