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.ozoneDB.util;
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;
038: private int buf_bytes = 0;
039: private char[] line = new char[74];
040: private int line_length = 0;
041:
042: private final static byte[] crlf = "\r\n".getBytes();
043:
044: private final static char[] map =
045:
046: { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
047: 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
048: 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
049: 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
050: 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8',
051: '9', '+', '/' };
052:
053: // 0-7
054: // 8-15
055: // 16-23
056: // 24-31
057: // 32-39
058: // 40-47
059: // 48-55
060: // 56-63
061:
062: private final void encode_token() {
063: int i = line_length;
064: line[i] = map[0x3F & buf >> 18];
065: line[i + 1] = map[0x3F & buf >> 12];
066: line[i + 2] = map[0x3F & buf >> 6];
067: line[i + 3] = map[0x3F & buf];
068: line_length += 4;
069: buf = 0;
070: buf_bytes = 0;
071: }
072:
073: private final void encode_partial_token() {
074: int i = line_length;
075: line[i] = map[0x3F & buf >> 18];
076: line[i + 1] = map[0x3F & buf >> 12];
077:
078: if (buf_bytes == 1) {
079: line[i + 2] = '=';
080: } else {
081: line[i + 2] = map[0x3F & buf >> 6];
082: }
083:
084: if (buf_bytes <= 2) {
085: line[i + 3] = '=';
086: } else {
087: line[i + 3] = map[0x3F & buf];
088: }
089: line_length += 4;
090: buf = 0;
091: buf_bytes = 0;
092: }
093:
094: private final void flush_line() {
095: out.append(line, 0, line_length);
096: line_length = 0;
097: }
098:
099: /**
100: * Given a sequence of input bytes, produces a sequence of output bytes
101: * using the base64 encoding. If there are bytes in `out' already, the
102: * new bytes are appended, so the caller should do `out.setLength(0)'
103: * first if that's desired.
104: */
105: public final void translate(byte[] in) {
106: int in_length = in.length;
107:
108: for (int i = 0; i < in_length; i++) {
109: if (buf_bytes == 0) {
110: buf = buf & 0x00FFFF | in[i] << 16;
111: } else if (buf_bytes == 1) {
112: buf = buf & 0xFF00FF | in[i] << 8 & 0x00FFFF;
113: } else {
114: buf = buf & 0xFFFF00 | in[i] & 0x0000FF;
115: }
116:
117: if (++buf_bytes == 3) {
118: encode_token();
119: if (line_length >= 72) {
120: flush_line();
121: }
122: }
123:
124: if (i == (in_length - 1)) {
125: if (buf_bytes > 0 && buf_bytes < 3) {
126: encode_partial_token();
127: }
128: if (line_length > 0) {
129: flush_line();
130: }
131: }
132: }
133:
134: for (int i = 0; i < line.length; i++) {
135: line[i] = 0;
136: }
137: }
138:
139: public char[] getCharArray() {
140: char[] ch;
141:
142: if (buf_bytes != 0) {
143: encode_partial_token();
144: }
145: flush_line();
146: for (int i = 0; i < line.length; i++) {
147: line[i] = 0;
148: }
149: ch = new char[out.length()];
150: out.getChars(0, out.length(), ch, 0);
151: return ch;
152: }
153: }
|