~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to TuxGuitar-browser-ftp/src/org/herac/tuxguitar/gui/tools/browser/ftp/utils/Base64Encoder.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.herac.tuxguitar.gui.tools.browser.ftp.utils;
 
2
 
 
3
import java.io.ByteArrayInputStream;
 
4
import java.io.ByteArrayOutputStream;
 
5
 
 
6
public class Base64Encoder {
 
7
        
 
8
        private static final int BUFFER_SIZE = 1024;
 
9
        
 
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) '/',
 
19
                (byte) '='
 
20
        };
 
21
        
 
22
        private static int get1(byte buf[], int off) {
 
23
                return (buf[off] & 0xfc) >> 2;
 
24
        }
 
25
        
 
26
        private static int get2(byte buf[], int off) {
 
27
                return ((buf[off] & 0x3) << 4) | ((buf[off + 1] & 0xf0) >>> 4);
 
28
        }
 
29
        
 
30
        private static int get3(byte buf[], int off) {
 
31
                return ((buf[off + 1] & 0x0f) << 2) | ((buf[off + 2] & 0xc0) >>> 6);
 
32
        }
 
33
        
 
34
        private static int get4(byte buf[], int off) {
 
35
                return buf[off + 2] & 0x3f;
 
36
        }
 
37
        
 
38
        public static byte[] encode(byte[] bytes) {
 
39
                try{
 
40
                        
 
41
                        ByteArrayInputStream  in  = new ByteArrayInputStream( bytes );
 
42
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
 
43
                        
 
44
                        byte buffer[] = new byte[BUFFER_SIZE];
 
45
                        int got = -1;
 
46
                        int off = 0;
 
47
                        int count = 0;
 
48
                        while ((got = in.read(buffer, off, BUFFER_SIZE - off)) > 0) {
 
49
                                if ((got + off) >= 3) {
 
50
                                        got += off;
 
51
                                        off = 0;
 
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);
 
57
                                                switch (count) {
 
58
                                                        case 73:
 
59
                                                                out.write(ENCODING[c1]);
 
60
                                                                out.write(ENCODING[c2]);
 
61
                                                                out.write(ENCODING[c3]);
 
62
                                                                out.write('\n');
 
63
                                                                out.write(ENCODING[c4]);
 
64
                                                                count = 1;
 
65
                                                                break;
 
66
                                                        case 74:
 
67
                                                                out.write(ENCODING[c1]);
 
68
                                                                out.write(ENCODING[c2]);
 
69
                                                                out.write('\n');
 
70
                                                                out.write(ENCODING[c3]);
 
71
                                                                out.write(ENCODING[c4]);
 
72
                                                                count = 2;
 
73
                                                                break;
 
74
                                                        case 75:
 
75
                                                                out.write(ENCODING[c1]);
 
76
                                                                out.write('\n');
 
77
                                                                out.write(ENCODING[c2]);
 
78
                                                                out.write(ENCODING[c3]);
 
79
                                                                out.write(ENCODING[c4]);
 
80
                                                                count = 3;
 
81
                                                                break;
 
82
                                                        case 76:
 
83
                                                                out.write('\n');
 
84
                                                                out.write(ENCODING[c1]);
 
85
                                                                out.write(ENCODING[c2]);
 
86
                                                                out.write(ENCODING[c3]);
 
87
                                                                out.write(ENCODING[c4]);
 
88
                                                                count = 4;
 
89
                                                                break;
 
90
                                                        default:
 
91
                                                                out.write(ENCODING[c1]);
 
92
                                                                out.write(ENCODING[c2]);
 
93
                                                                out.write(ENCODING[c3]);
 
94
                                                                out.write(ENCODING[c4]);
 
95
                                                                count += 4;
 
96
                                                                break;
 
97
                                                }
 
98
                                                off += 3;
 
99
                                        }
 
100
                                        
 
101
                                        for (int i = 0; i < 3; i++){
 
102
                                                buffer[i] = (i < got - off) ? buffer[off + i] : ((byte) 0);
 
103
                                        }
 
104
                                        off = got - off;
 
105
                                } else {
 
106
                                        off += got;
 
107
                                }
 
108
                        }
 
109
                        
 
110
                        switch (off) {
 
111
                                case 1:
 
112
                                        out.write(ENCODING[get1(buffer, 0)]);
 
113
                                        out.write(ENCODING[get2(buffer, 0)]);
 
114
                                        out.write('=');
 
115
                                        out.write('=');
 
116
                                        break;
 
117
                                case 2:
 
118
                                        out.write(ENCODING[get1(buffer, 0)]);
 
119
                                        out.write(ENCODING[get2(buffer, 0)]);
 
120
                                        out.write(ENCODING[get3(buffer, 0)]);
 
121
                                        out.write('=');
 
122
                        }
 
123
                        
 
124
                        return out.toByteArray();
 
125
                        
 
126
                }catch(Throwable throwable){
 
127
                        throwable.printStackTrace();
 
128
                }
 
129
                
 
130
                return bytes;
 
131
        }
 
132
}
 
 
b'\\ No newline at end of file'