KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infozone > tools > MimeBase64Encoder


1 /*
2  *
3  * The contents of this file are subject to the Netscape Public
4  * License Version 1.1 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of
6  * the License at http://www.mozilla.org/NPL/
7  *
8  * Software distributed under the License is distributed on an "AS
9  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10  * implied. See the License for the specific language governing
11  * rights and limitations under the License.
12  *
13  * The Original Code is mozilla.org code.
14  *
15  * The Initial Developer of the Original Code is Netscape
16  * Communications Corporation. Portions created by Netscape are
17  * Copyright (C) 1999 Netscape Communications Corporation. All
18  * Rights Reserved.
19  *
20  * Contributor(s):
21  */

22
23
24 package org.infozone.tools;
25
26
27 /**
28  * Byte to text encoder using base 64 encoding. To create a base 64
29  * encoding of a byte stream call {@link #translate} for every
30  * sequence of bytes and {@link #getCharArray} to mark closure of
31  * the byte stream and retrieve the text presentation.
32  *
33  * @author Based on code from the Mozilla Directory SDK
34  */

35 public final class MimeBase64Encoder
36 {
37
38
39     private StringBuffer JavaDoc out = new StringBuffer JavaDoc();
40     
41     private int buf = 0; // a 24-bit quantity
42
private int buf_bytes = 0; // how many octets are set in it
43
private char line[] = new char[74]; // output buffer
44
private int line_length = 0; // output buffer fill pointer
45

46     static private final byte crlf[] = "\r\n".getBytes();
47
48     static private final char map[] = {
49       'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0-7
50
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 8-15
51
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 16-23
52
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 24-31
53
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 32-39
54
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 40-47
55
'w', 'x', 'y', 'z', '0', '1', '2', '3', // 48-55
56
'4', '5', '6', '7', '8', '9', '+', '/', // 56-63
57
};
58
59     private final void encode_token() {
60         int i = line_length;
61         line[i] = map[0x3F & (buf >> 18)]; // sextet 1 (octet 1)
62
line[i+1] = map[0x3F & (buf >> 12)]; // sextet 2 (octet 1 and 2)
63
line[i+2] = map[0x3F & (buf >> 6)]; // sextet 3 (octet 2 and 3)
64
line[i+3] = map[0x3F & buf]; // sextet 4 (octet 3)
65
line_length += 4;
66         buf = 0;
67         buf_bytes = 0;
68     }
69
70     private final void encode_partial_token() {
71         int i = line_length;
72         line[i] = map[0x3F & (buf >> 18)]; // sextet 1 (octet 1)
73
line[i+1] = map[0x3F & (buf >> 12)]; // sextet 2 (octet 1 and 2)
74

75         if (buf_bytes == 1)
76             line[i+2] = '=';
77         else
78             line[i+2] = map[0x3F & (buf >> 6)]; // sextet 3 (octet 2 and 3)
79

80         if (buf_bytes <= 2)
81             line[i+3] = '=';
82         else
83             line[i+3] = map[0x3F & buf]; // sextet 4 (octet 3)
84
line_length += 4;
85         buf = 0;
86         buf_bytes = 0;
87     }
88
89     private final void flush_line()
90     {
91         out.append(line, 0, line_length);
92         line_length = 0;
93     }
94
95     /** Given a sequence of input bytes, produces a sequence of output bytes
96         using the base64 encoding. If there are bytes in `out' already, the
97         new bytes are appended, so the caller should do `out.setLength(0)'
98         first if that's desired.
99      */

100     public final void translate(byte[] in)
101     {
102         int in_length = in.length;
103
104         for (int i = 0; i < in_length; i++) {
105             if (buf_bytes == 0)
106                 buf = (buf & 0x00FFFF) | (in[i] << 16);
107             else if (buf_bytes == 1)
108                 buf = (buf & 0xFF00FF) | ((in[i] << 8) & 0x00FFFF);
109             else
110                 buf = (buf & 0xFFFF00) | (in[i] & 0x0000FF);
111
112             if ((++buf_bytes) == 3) {
113                 encode_token();
114                 if (line_length >= 72) {
115                     flush_line();
116                 }
117             }
118
119             if (i == (in_length-1)) {
120                 if ((buf_bytes > 0) && (buf_bytes < 3))
121                     encode_partial_token();
122                 if (line_length > 0)
123                     flush_line();
124             }
125         }
126
127         for (int i=0; i<line.length; i++)
128             line[i] = 0;
129     }
130
131     
132     public char[] getCharArray()
133     {
134     char[] ch;
135
136         if (buf_bytes != 0)
137             encode_partial_token();
138         flush_line();
139         for (int i=0; i<line.length; i++)
140             line[i] = 0;
141     ch = new char[ out.length() ];
142     out.getChars( 0, out.length(), ch, 0 );
143     return ch;
144     }
145 }
146
Popular Tags