01: /*
02: * Base64Encoder.java
03: *
04: * Copyright (C) 2000-2002 Peter Graves
05: * $Id: Base64Encoder.java,v 1.1.1.1 2002/09/24 16:09:45 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * 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;
25: import java.io.IOException;
26: import org.armedbear.j.FastStringBuffer;
27:
28: public final class Base64Encoder {
29: private static final int MAX_LINE_LENGTH = 72;
30: private static final int BUFFER_SIZE = (MAX_LINE_LENGTH * 3) / 4;
31:
32: private InputStream inputStream;
33:
34: public Base64Encoder(InputStream inputStream) {
35: this .inputStream = inputStream;
36: }
37:
38: public String encodeLine() throws IOException {
39: byte[] buffer = new byte[BUFFER_SIZE];
40: int length = inputStream.read(buffer, 0, buffer.length);
41: if (length <= 0)
42: return null;
43: if (length == buffer.length)
44: return encode(buffer);
45: // Partial line.
46: byte[] newBuffer = new byte[length];
47: System.arraycopy(buffer, 0, newBuffer, 0, length);
48: return encode(newBuffer);
49: }
50:
51: private static String encode(byte[] input) {
52: FastStringBuffer encoded = new FastStringBuffer();
53: for (int i = 0; i < input.length; i += 3)
54: encoded.append(encodeThreeBytes(input, i));
55: return encoded.toString();
56: }
57:
58: private static char[] encodeThreeBytes(byte[] input, int offset) {
59: int[] in = new int[3];
60: char[] out = new char[4];
61: int n;
62: for (n = 0; n < 3; n++) {
63: if (offset >= input.length)
64: break;
65: // Convert signed bytes to positive integers with the same bit
66: // pattern.
67: if (input[offset] < 0)
68: in[n] = input[offset] + 256;
69: else
70: in[n] = input[offset];
71: ++offset;
72: }
73: if (n > 0) {
74: out[0] = map[in[0] >> 2];
75: out[1] = map[((in[0] & 3) << 4) | (in[1] >> 4)];
76: out[2] = map[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
77: out[3] = map[in[2] & 0x3f];
78: // Replace characters in output array with pad characters if input
79: // array contained fewer than three bytes.
80: if (n < 3) {
81: out[3] = '=';
82: if (n < 2)
83: out[2] = '=';
84: }
85: }
86: return out;
87: }
88:
89: public static final char[] getBase64Chars() {
90: return map;
91: }
92:
93: private static final char[] map = { 'A', 'B', 'C', 'D', 'E', 'F',
94: 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
95: 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
96: 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
97: 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1',
98: '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
99: }
|