1
package org.herac.tuxguitar.gui.tools.browser.ftp.utils;
3
import java.io.ByteArrayInputStream;
4
import java.io.ByteArrayOutputStream;
6
public class Base64Encoder {
8
private static final int BUFFER_SIZE = 1024;
10
private static byte ENCODING[] = {
11
(byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H',
12
(byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P',
13
(byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X',
14
(byte) 'Y', (byte) 'Z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f',
15
(byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n',
16
(byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's', (byte) 't', (byte) 'u', (byte) 'v',
17
(byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2', (byte) '3',
18
(byte) '4', (byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) '+', (byte) '/',
22
private static int get1(byte buf[], int off) {
23
return (buf[off] & 0xfc) >> 2;
26
private static int get2(byte buf[], int off) {
27
return ((buf[off] & 0x3) << 4) | ((buf[off + 1] & 0xf0) >>> 4);
30
private static int get3(byte buf[], int off) {
31
return ((buf[off + 1] & 0x0f) << 2) | ((buf[off + 2] & 0xc0) >>> 6);
34
private static int get4(byte buf[], int off) {
35
return buf[off + 2] & 0x3f;
38
public static byte[] encode(byte[] bytes) {
41
ByteArrayInputStream in = new ByteArrayInputStream( bytes );
42
ByteArrayOutputStream out = new ByteArrayOutputStream();
44
byte buffer[] = new byte[BUFFER_SIZE];
48
while ((got = in.read(buffer, off, BUFFER_SIZE - off)) > 0) {
49
if ((got + off) >= 3) {
52
while (off + 3 <= got) {
53
int c1 = get1(buffer, off);
54
int c2 = get2(buffer, off);
55
int c3 = get3(buffer, off);
56
int c4 = get4(buffer, off);
59
out.write(ENCODING[c1]);
60
out.write(ENCODING[c2]);
61
out.write(ENCODING[c3]);
63
out.write(ENCODING[c4]);
67
out.write(ENCODING[c1]);
68
out.write(ENCODING[c2]);
70
out.write(ENCODING[c3]);
71
out.write(ENCODING[c4]);
75
out.write(ENCODING[c1]);
77
out.write(ENCODING[c2]);
78
out.write(ENCODING[c3]);
79
out.write(ENCODING[c4]);
84
out.write(ENCODING[c1]);
85
out.write(ENCODING[c2]);
86
out.write(ENCODING[c3]);
87
out.write(ENCODING[c4]);
91
out.write(ENCODING[c1]);
92
out.write(ENCODING[c2]);
93
out.write(ENCODING[c3]);
94
out.write(ENCODING[c4]);
101
for (int i = 0; i < 3; i++){
102
buffer[i] = (i < got - off) ? buffer[off + i] : ((byte) 0);
112
out.write(ENCODING[get1(buffer, 0)]);
113
out.write(ENCODING[get2(buffer, 0)]);
118
out.write(ENCODING[get1(buffer, 0)]);
119
out.write(ENCODING[get2(buffer, 0)]);
120
out.write(ENCODING[get3(buffer, 0)]);
124
return out.toByteArray();
126
}catch(Throwable throwable){
127
throwable.printStackTrace();
b'\\ No newline at end of file'