39
40
* This means that the encoded text will simply start with the first line
40
41
* of encoded text and end with the last line of encoded text.
42
* @version $Id: Base64EncodeStream.java 1345683 2012-06-03 14:50:33Z gadams $
43
* @version $Id: Base64EncodeStream.java 1681698 2015-05-26 07:49:35Z ssteiner $
44
45
* Originally authored by Thomas DeWeese, Vincent Hardy, and Chuck McManis.
46
47
public class Base64EncodeStream extends OutputStream {
48
49
/** This array maps the 6 bit values to their characters */
49
private static final byte[] pem_array = {
50
private static final byte[] PEM_ARRAY = {
51
'A','B','C','D','E','F','G','H', // 0
52
'I','J','K','L','M','N','O','P', // 1
53
'Q','R','S','T','U','V','W','X', // 2
54
'Y','Z','a','b','c','d','e','f', // 3
55
'g','h','i','j','k','l','m','n', // 4
56
'o','p','q','r','s','t','u','v', // 5
57
'w','x','y','z','0','1','2','3', // 6
58
'4','5','6','7','8','9','+','/' // 7
52
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0
53
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 1
54
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 2
55
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 3
56
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 4
57
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 5
58
'w', 'x', 'y', 'z', '0', '1', '2', '3', // 6
59
'4', '5', '6', '7', '8', '9', '+', '/' // 7
61
62
byte [] atom = new byte[3];
63
64
byte [] encodeBuf = new byte[4];
67
68
boolean closeOutOnClose;
69
70
public Base64EncodeStream(OutputStream out) {
70
this.out = new PrintStream(out);
72
this.out = new PrintStream(out, false, "UTF-8");
73
} catch (UnsupportedEncodingException e) {
71
76
closeOutOnClose = true;
74
79
public Base64EncodeStream(OutputStream out, boolean closeOutOnClose) {
75
this.out = new PrintStream(out);
81
this.out = new PrintStream(out, false, "UTF-8");
82
} catch (UnsupportedEncodingException e) {
76
85
this.closeOutOnClose = closeOutOnClose;
79
public void close () throws IOException {
88
public void close() throws IOException {
92
if (closeOutOnClose) {
117
128
* padding characters.
119
130
void encodeAtom() throws IOException {
122
135
switch (atomLen) {
126
encodeBuf[0] = pem_array[((a >>> 2) & 0x3F)];
127
encodeBuf[1] = pem_array[((a << 4) & 0x30)];
139
encodeBuf[0] = PEM_ARRAY[((a >>> 2) & 0x3F)];
140
encodeBuf[1] = PEM_ARRAY[((a << 4) & 0x30)];
128
141
encodeBuf[2] = encodeBuf[3] = '=';
133
encodeBuf[0] = pem_array[((a >>> 2) & 0x3F)];
134
encodeBuf[1] = pem_array[(((a << 4) & 0x30) | ((b >>> 4) & 0x0F))];
135
encodeBuf[2] = pem_array[((b << 2) & 0x3C)];
146
encodeBuf[0] = PEM_ARRAY[((a >>> 2) & 0x3F)];
147
encodeBuf[1] = PEM_ARRAY[(((a << 4) & 0x30) | ((b >>> 4) & 0x0F))];
148
encodeBuf[2] = PEM_ARRAY[((b << 2) & 0x3C)];
136
149
encodeBuf[3] = '=';
142
encodeBuf[0] = pem_array[((a >>> 2) & 0x3F)];
143
encodeBuf[1] = pem_array[(((a << 4) & 0x30) | ((b >>> 4) & 0x0F))];
144
encodeBuf[2] = pem_array[(((b << 2) & 0x3C) | ((c >>> 6) & 0x03))];
145
encodeBuf[3] = pem_array[c & 0x3F];
155
encodeBuf[0] = PEM_ARRAY[((a >>> 2) & 0x3F)];
156
encodeBuf[1] = PEM_ARRAY[(((a << 4) & 0x30) | ((b >>> 4) & 0x0F))];
157
encodeBuf[2] = PEM_ARRAY[(((b << 2) & 0x3C) | ((c >>> 6) & 0x03))];
158
encodeBuf[3] = PEM_ARRAY[c & 0x3F];
147
160
if (lineLen == 64) {