1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.gridsystems;
19
20 import java.io.ByteArrayOutputStream;
21 import java.io.FilterOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class Base64Encoder extends FilterOutputStream {
46
47
48 private static final char[] CHARS = {
49 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
50 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
51 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
52 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
53 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
54 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
55 '8', '9', '+', '/'
56 };
57
58
59 private int charCount;
60
61 private int carryOver;
62
63
64
65
66
67
68
69 public Base64Encoder(OutputStream out) {
70 super(out);
71 }
72
73
74
75
76
77
78
79 public void write(int b) throws IOException {
80
81
82
83
84
85
86
87 if (b < 0) {
88 b += 256;
89 }
90
91
92 if (charCount % 3 == 0) {
93 int lookup = b >> 2;
94 carryOver = b & 3;
95 out.write(CHARS[lookup]);
96 } else if (charCount % 3 == 1) {
97
98
99 int lookup = ((carryOver << 4) + (b >> 4)) & 63;
100 carryOver = b & 15;
101 out.write(CHARS[lookup]);
102 } else if (charCount % 3 == 2) {
103
104
105 int lookup = ((carryOver << 2) + (b >> 6)) & 63;
106 out.write(CHARS[lookup]);
107 lookup = b & 63;
108 out.write(CHARS[lookup]);
109 carryOver = 0;
110 }
111 charCount++;
112
113
114 if (charCount % 57 == 0) {
115 out.write('\n');
116 }
117 }
118
119
120
121
122
123
124
125
126
127
128 public void write(byte[] b, int off, int len) throws IOException {
129
130 for (int i = 0; i < len; i++) {
131 write(b[off + i]);
132 }
133 }
134
135
136
137
138
139
140 public void write(String s) throws IOException {
141 byte[] buf = Base64Utils.toByteArray(s);
142 write(buf, 0, buf.length);
143 }
144
145
146
147
148
149
150
151 public void close() throws IOException {
152
153 if (charCount % 3 == 1) {
154 int lookup = (carryOver << 4) & 63;
155 out.write(CHARS[lookup]);
156 out.write('=');
157 out.write('=');
158 } else if (charCount % 3 == 2) {
159 int lookup = (carryOver << 2) & 63;
160 out.write(CHARS[lookup]);
161 out.write('=');
162 }
163 super.close();
164 }
165
166
167
168
169
170
171
172 public static String encode(String s) {
173 ByteArrayOutputStream out = new ByteArrayOutputStream((int) (s.length() * 1.37));
174 Base64Encoder encodedOut = new Base64Encoder(out);
175
176 try {
177 encodedOut.write(s);
178 encodedOut.close();
179
180
181 return out.toString();
182 } catch (IOException ignored) { return null; }
183 }
184
185
186
187
188
189
190
191 public static byte[] binaryEncode(byte[] unencoded) {
192 ByteArrayOutputStream out = new ByteArrayOutputStream((int)(unencoded.length * 1.37));
193 Base64Encoder encodedOut = new Base64Encoder(out);
194
195 try {
196 encodedOut.write(unencoded);
197 encodedOut.close();
198
199 return out.toByteArray();
200 } catch (IOException ignored) { return null; }
201 }
202
203
204
205
206
207
208
209 public static byte[] binaryEncode(InputStream is) {
210 ByteArrayOutputStream out = new ByteArrayOutputStream();
211 Base64Encoder encodedOut = new Base64Encoder(out);
212
213 try {
214 byte[] data = new byte[128000];
215
216 int size;
217
218 while ((size = is.read(data)) != -1) {
219 encodedOut.write(data, 0, size);
220 }
221 encodedOut.close();
222
223 byte[] retorno = out.toByteArray();
224 out = null;
225
226 return retorno;
227 } catch (IOException ignored) { return null; }
228 }
229 }