KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > myoodb > util > MimeBase64


1 ///////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
4
//
5
// All Rights Reserved
6
//
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License and GNU Library
9
// General Public License as published by the Free Software Foundation;
10
// either version 2, or (at your option) any later version.
11
//
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License and GNU Library General Public License
16
// for more details.
17
//
18
// You should have received a copy of the GNU General Public License
19
// and GNU Library General Public License along with this program; if
20
// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
21
// MA 02139, USA.
22
//
23
///////////////////////////////////////////////////////////////////////////////
24
package org.myoodb.util;
25
26 public final class MimeBase64
27 {
28     private int m_buf = 0;
29     private int m_buf_bytes = 0;
30     private int m_line_length = 0;
31     private char[] m_line = new char[74];
32     private StringBuilder JavaDoc m_strOut = new StringBuilder JavaDoc();
33     private java.io.ByteArrayOutputStream JavaDoc m_streamOut = new java.io.ByteArrayOutputStream JavaDoc();
34     
35     private byte[] m_token = new byte[4];
36     private byte[] m_bytes = new byte[3];
37     private int m_token_length = 0;
38     
39     private final static byte NUL = 127;
40     private final static byte EOF = 126;
41     
42     private final static char[] ENCODE_MAP =
43     {
44         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
45         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
46         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
47     };
48     // 0-7
49
// 8-15
50
// 16-23
51
// 24-31
52
// 32-39
53
// 40-47
54
// 48-55
55
// 56-63
56

57     private final static byte[] DECODE_MAP =
58     {
59         NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
60         NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, 62, NUL, NUL, NUL, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
61         NUL, NUL, NUL, EOF, NUL, NUL, NUL, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, NUL, NUL,
62         NUL, NUL, NUL, NUL, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, NUL, NUL, NUL,
63         NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
64         NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
65         NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
66         NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL,
67         NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL, NUL
68     };
69     // 000-007
70
// 010-017
71
// 020-027
72
// 030-037
73
// 040-047 !"#$%&'
74
// 050-057 ()*+,-./
75
// 060-067 01234567
76
// 070-077 89:;<=>?
77

78     // 100-107 @ABCDEFG
79
// 110-117 HIJKLMNO
80
// 120-127 PQRSTUVW
81
// 130-137 XYZ[\]^_
82
// 140-147 `abcdefg
83
// 150-157 hijklmno
84
// 160-167 pqrstuvw
85
// 170-177 xyz{|}~
86

87     // 200-207
88
// 210-217
89
// 220-227
90
// 230-237
91
// 240-247
92
// 250-257
93
// 260-267
94
// 270-277
95

96     // 300-307
97
// 310-317
98
// 320-327
99
// 330-337
100
// 340-347
101
// 350-357
102
// 360-367
103
// 370-377
104

105     private final void encode_token()
106     {
107         int i = m_line_length;
108         m_line[i] = ENCODE_MAP[0x3F & m_buf >> 18];
109         m_line[i + 1] = ENCODE_MAP[0x3F & m_buf >> 12];
110         m_line[i + 2] = ENCODE_MAP[0x3F & m_buf >> 6];
111         m_line[i + 3] = ENCODE_MAP[0x3F & m_buf];
112         m_line_length += 4;
113         m_buf = 0;
114         m_buf_bytes = 0;
115     }
116     
117     private final void encode_partial_token()
118     {
119         int i = m_line_length;
120         m_line[i] = ENCODE_MAP[0x3F & m_buf >> 18];
121         m_line[i + 1] = ENCODE_MAP[0x3F & m_buf >> 12];
122         
123         if (m_buf_bytes == 1)
124         {
125             m_line[i + 2] = '=';
126         }
127         else
128         {
129             m_line[i + 2] = ENCODE_MAP[0x3F & m_buf >> 6];
130         }
131         
132         if (m_buf_bytes <= 2)
133         {
134             m_line[i + 3] = '=';
135         }
136         else
137         {
138             m_line[i + 3] = ENCODE_MAP[0x3F & m_buf];
139         }
140         m_line_length += 4;
141         m_buf = 0;
142         m_buf_bytes = 0;
143     }
144     
145     private final void flush_line()
146     {
147         m_strOut.append(m_line, 0, m_line_length);
148         m_line_length = 0;
149     }
150     
151     public final void translate(byte[] in)
152     {
153         int in_length = in.length;
154         
155         for (int i = 0; i < in_length; i++)
156         {
157             if (m_buf_bytes == 0)
158             {
159                 m_buf = m_buf & 0x00FFFF | in[i] << 16;
160             }
161             else if (m_buf_bytes == 1)
162             {
163                 m_buf = m_buf & 0xFF00FF | in[i] << 8 & 0x00FFFF;
164             }
165             else
166             {
167                 m_buf = m_buf & 0xFFFF00 | in[i] & 0x0000FF;
168             }
169             
170             if (++m_buf_bytes == 3)
171             {
172                 encode_token();
173                 if (m_line_length >= 72)
174                 {
175                     flush_line();
176                 }
177             }
178             
179             if (i == (in_length - 1))
180             {
181                 if (m_buf_bytes > 0 && m_buf_bytes < 3)
182                 {
183                     encode_partial_token();
184                 }
185                 if (m_line_length > 0)
186                 {
187                     flush_line();
188                 }
189             }
190         }
191         
192         for (int i = 0; i < m_line.length; i++)
193         {
194             m_line[i] = 0;
195         }
196     }
197     
198     public char[] getCharArray()
199     {
200         char[] ch;
201         
202         if (m_buf_bytes != 0)
203         {
204             encode_partial_token();
205         }
206         flush_line();
207         for (int i = 0; i < m_line.length; i++)
208         {
209             m_line[i] = 0;
210         }
211         ch = new char[m_strOut.length()];
212         m_strOut.getChars(0, m_strOut.length(), ch, 0);
213         return ch;
214     }
215
216     private void decode_token()
217     {
218         int num = m_token[0] << 18 | m_token[1] << 12 | m_token[2] << 6 | m_token[3];
219         
220         m_bytes[0] = (byte)(0xFF & num >> 16);
221         m_bytes[1] = (byte)(0xFF & num >> 8);
222         m_bytes[2] = (byte)(0xFF & num);
223         
224         m_streamOut.write(m_bytes, 0, 3);
225     }
226     
227     private final void decode_final_token()
228     {
229         
230         byte b0 = m_token[0];
231         byte b1 = m_token[1];
232         byte b2 = m_token[2];
233         byte b3 = m_token[3];
234         
235         int eq_count = 0;
236         
237         if (b0 == EOF) {
238             b0 = 0;
239             eq_count++;
240         }
241         if (b1 == EOF) {
242             b1 = 0;
243             eq_count++;
244         }
245         if (b2 == EOF) {
246             b2 = 0;
247             eq_count++;
248         }
249         if (b3 == EOF) {
250             b3 = 0;
251             eq_count++;
252         }
253         
254         int num = b0 << 18 | b1 << 12 | b2 << 6 | b3;
255         
256         m_streamOut.write((byte)(num >> 16));
257         if (eq_count <= 1)
258          {
259             m_streamOut.write((byte)(num >> 8 & 0xFF));
260             if (eq_count == 0)
261             {
262                 m_streamOut.write((byte)(num & 0xFF));
263             }
264         }
265     }
266     
267     private void eof()
268     {
269         if (m_token != null && m_token_length != 0)
270         {
271             while (m_token_length < 4)
272             {
273                 m_token[m_token_length++] = EOF;
274             }
275             decode_final_token();
276         }
277         m_token_length = 0;
278         m_token = new byte[4];
279         m_bytes = new byte[3];
280     }
281     
282     public final void translate(char[] ch)
283     {
284         translate(ch, 0, ch.length);
285     }
286     
287     public final void translate(char[] ch, int offset, int length)
288     {
289         if (m_token == null)
290         {
291             return;
292         }
293         
294         for (int i = offset; i < offset + length; i++)
295         {
296             byte t = DECODE_MAP[ch[i] & 0xff];
297             
298             if (t == EOF)
299             {
300                 eof();
301             }
302             else
303             {
304                 if (t != NUL)
305                 {
306                     m_token[m_token_length++] = t;
307                 }
308             }
309             if (m_token_length == 4)
310             {
311                 decode_token();
312                 m_token_length = 0;
313             }
314         }
315     }
316     
317     public byte[] getByteArray()
318     {
319         eof();
320         return m_streamOut.toByteArray();
321     }
322 }
323
Popular Tags