KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > mail > Base64Encoder


1 /*
2  * Base64Encoder.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: Base64Encoder.java,v 1.1.1.1 2002/09/24 16:09:45 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, 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 for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j.mail;
23
24 import java.io.InputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import org.armedbear.j.FastStringBuffer;
27
28 public final class Base64Encoder
29 {
30     private static final int MAX_LINE_LENGTH = 72;
31     private static final int BUFFER_SIZE = (MAX_LINE_LENGTH * 3) / 4;
32
33     private InputStream JavaDoc inputStream;
34
35     public Base64Encoder(InputStream JavaDoc inputStream)
36     {
37         this.inputStream = inputStream;
38     }
39
40     public String JavaDoc encodeLine() throws IOException JavaDoc
41     {
42         byte[] buffer = new byte[BUFFER_SIZE];
43         int length = inputStream.read(buffer, 0, buffer.length);
44         if (length <= 0)
45             return null;
46         if (length == buffer.length)
47             return encode(buffer);
48         // Partial line.
49
byte[] newBuffer = new byte[length];
50         System.arraycopy(buffer, 0, newBuffer, 0, length);
51         return encode(newBuffer);
52     }
53
54     private static String JavaDoc encode(byte[] input)
55     {
56         FastStringBuffer encoded = new FastStringBuffer();
57         for (int i = 0; i < input.length; i += 3)
58             encoded.append(encodeThreeBytes(input, i));
59         return encoded.toString();
60     }
61
62     private static char[] encodeThreeBytes(byte[] input, int offset)
63     {
64         int[] in = new int[3];
65         char[] out = new char[4];
66         int n;
67         for (n = 0; n < 3; n++) {
68             if (offset >= input.length)
69                 break;
70             // Convert signed bytes to positive integers with the same bit
71
// pattern.
72
if (input[offset] < 0)
73                 in[n] = input[offset] + 256;
74             else
75                 in[n] = input[offset];
76             ++offset;
77         }
78         if (n > 0) {
79             out[0] = map[in[0] >> 2];
80             out[1] = map[((in[0] & 3) << 4) | (in[1] >> 4)];
81             out[2] = map[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
82             out[3] = map[in[2] & 0x3f];
83             // Replace characters in output array with pad characters if input
84
// array contained fewer than three bytes.
85
if (n < 3) {
86                 out[3] = '=';
87                 if (n < 2)
88                     out[2] = '=';
89             }
90         }
91         return out;
92     }
93
94     public static final char[] getBase64Chars()
95     {
96         return map;
97     }
98
99     private static final char[] map = {
100         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
101         'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
102         'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
103         'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
104         'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
105         'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
106         'w', 'x', 'y', 'z', '0', '1', '2', '3',
107         '4', '5', '6', '7', '8', '9', '+', '/'
108     };
109 }
110
Popular Tags