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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package org.madore.damlengine;
import java.io.InputStream;
import java.io.IOException;
public final class Base64 {
public static final char[] base64Alphabet = {
'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', '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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
};
public static String encode(InputStream str) {
byte[] inb = new byte[768];
char[] outb = new char[1024];
StringBuffer strbuf = new StringBuffer();
try {
int len;
while ( ( len = str.read(inb) ) > 0 ) {
for ( int i=0 ; 3*i<len ; i++ ) {
outb[4*i] = base64Alphabet[(inb[3*i]>>>2)&0x3f];
if ( 3*i+1 < len )
outb[4*i+1] = base64Alphabet[(inb[3*i]&3)<<4 | ((inb[3*i+1]>>>4)&0xf)];
else
outb[4*i+1] = base64Alphabet[(inb[3*i]&3)<<4];
if ( 3*i+2 < len )
outb[4*i+2] = base64Alphabet[(inb[3*i+1]&0xf)<<2 | ((inb[3*i+2]>>>6)&0x3)];
else if ( 3*i+1 < len )
outb[4*i+2] = base64Alphabet[(inb[3*i+1]&0xf)<<2];
else
outb[4*i+2] = '=';
if ( 3*i+2 < len )
outb[4*i+3] = base64Alphabet[(inb[3*i+2]&0x3f)];
else
outb[4*i+3] = '=';
}
strbuf.append(outb, 0, ((len+2)/3)*4);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return new String(strbuf);
}
}
|