~hjd/ubuntu/wily/xmlgraphics-commons/debian-merged

« back to all changes in this revision

Viewing changes to src/java/org/apache/xmlgraphics/util/io/Base64EncodeStream.java

  • Committer: Hans Joachim Desserud
  • Date: 2015-11-11 18:22:53 UTC
  • mfrom: (9.1.5 sid)
  • Revision ID: hans_joachim_desserud-20151111182253-zwi0frfm97j0wddn
  * Merge from Debian unstable.  Remaining changes:
    - d/control: Drop dependencies required for unit testing as they
      include libmockito-java which would pull maven into main, disable unit
      test execution.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 * limitations under the License.
16
16
 */
17
17
 
18
 
/* $Id: Base64EncodeStream.java 1345683 2012-06-03 14:50:33Z gadams $ */
 
18
/* $Id: Base64EncodeStream.java 1681698 2015-05-26 07:49:35Z ssteiner $ */
19
19
 
20
20
package org.apache.xmlgraphics.util.io;
21
21
 
22
22
import java.io.IOException;
23
23
import java.io.OutputStream;
24
24
import java.io.PrintStream;
 
25
import java.io.UnsupportedEncodingException;
25
26
 
26
27
// CSOFF: ConstantName
27
28
// CSOFF: InnerAssignment
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.
41
42
 *
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 $
43
44
 *
44
45
 * Originally authored by Thomas DeWeese, Vincent Hardy, and Chuck McManis.
45
46
 */
46
47
public class Base64EncodeStream extends OutputStream {
47
48
 
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 = {
50
51
    //   0   1   2   3   4   5   6   7
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
59
60
    };
60
61
 
61
62
    byte [] atom = new byte[3];
62
 
    int     atomLen = 0;
 
63
    int     atomLen;
63
64
    byte [] encodeBuf = new byte[4];
64
 
    int     lineLen = 0;
 
65
    int     lineLen;
65
66
 
66
67
    PrintStream  out;
67
68
    boolean closeOutOnClose;
68
69
 
69
70
    public Base64EncodeStream(OutputStream out) {
70
 
        this.out = new PrintStream(out);
 
71
        try {
 
72
            this.out = new PrintStream(out, false, "UTF-8");
 
73
        } catch (UnsupportedEncodingException e) {
 
74
            e.printStackTrace();
 
75
        }
71
76
        closeOutOnClose = true;
72
77
    }
73
78
 
74
79
    public Base64EncodeStream(OutputStream out, boolean closeOutOnClose) {
75
 
        this.out = new PrintStream(out);
 
80
        try {
 
81
            this.out = new PrintStream(out, false, "UTF-8");
 
82
        } catch (UnsupportedEncodingException e) {
 
83
            e.printStackTrace();
 
84
        }
76
85
        this.closeOutOnClose = closeOutOnClose;
77
86
    }
78
87
 
79
 
    public void close () throws IOException {
 
88
    public void close() throws IOException {
80
89
        if (out != null) {
81
90
            encodeAtom();
82
91
            out.flush();
83
 
            if (closeOutOnClose)
 
92
            if (closeOutOnClose) {
84
93
                out.close();
85
 
            out=null;
 
94
            }
 
95
            out = null;
86
96
        }
87
97
    }
88
98
 
98
108
 
99
109
    public void write(int b) throws IOException {
100
110
        atom[atomLen++] = (byte)b;
101
 
        if (atomLen == 3)
 
111
        if (atomLen == 3) {
102
112
            encodeAtom();
 
113
        }
103
114
    }
104
115
 
105
116
    public void write(byte []data) throws IOException {
117
128
     * padding characters.
118
129
     */
119
130
    void encodeAtom() throws IOException {
120
 
        byte a, b, c;
 
131
        byte a;
 
132
        byte b;
 
133
        byte c;
121
134
 
122
135
        switch (atomLen) {
123
136
        case 0: return;
124
137
        case 1:
125
138
            a = atom[0];
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] = '=';
129
142
            break;
130
143
        case 2:
131
144
            a = atom[0];
132
145
            b = atom[1];
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] = '=';
137
150
            break;
138
151
        default:
139
152
            a = atom[0];
140
153
            b = atom[1];
141
154
            c = atom[2];
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];
146
159
        }
147
160
        if (lineLen == 64) {
148
161
            out.println();
161
174
     * padding characters.
162
175
     */
163
176
    void encodeFromArray(byte[] data, int offset, int len)
164
 
        throws IOException{
165
 
        byte a, b, c;
166
 
        if (len == 0)
 
177
        throws IOException {
 
178
        byte a;
 
179
        byte b;
 
180
        byte c;
 
181
        if (len == 0) {
167
182
            return;
 
183
        }
168
184
 
169
185
        // System.out.println("atomLen: " + atomLen +
170
186
        //                    " len: " + len +
173
189
        if (atomLen != 0) {
174
190
            switch(atomLen) {
175
191
            case 1:
176
 
                atom[1] = data[offset++]; len--; atomLen++;
177
 
                if (len == 0) return;
178
 
                atom[2] = data[offset++]; len--; atomLen++;
 
192
                atom[1] = data[offset++];
 
193
                len--;
 
194
                atomLen++;
 
195
                if (len == 0) {
 
196
                    return;
 
197
                }
 
198
                atom[2] = data[offset++];
 
199
                len--;
 
200
                atomLen++;
179
201
                break;
180
202
            case 2:
181
 
                atom[2] = data[offset++]; len--; atomLen++;
 
203
                atom[2] = data[offset++];
 
204
                len--;
 
205
                atomLen++;
182
206
                break;
183
207
            default:
184
208
            }
185
209
            encodeAtom();
186
210
        }
187
211
 
188
 
        while (len >=3) {
 
212
        while (len >= 3) {
189
213
            a = data[offset++];
190
214
            b = data[offset++];
191
215
            c = data[offset++];
192
216
 
193
 
            encodeBuf[0] = pem_array[((a >>> 2) & 0x3F)];
194
 
            encodeBuf[1] = pem_array[(((a << 4) & 0x30) | ((b >>> 4) & 0x0F))];
195
 
            encodeBuf[2] = pem_array[(((b << 2) & 0x3C) | ((c >>> 6) & 0x03))];
196
 
            encodeBuf[3] = pem_array[c & 0x3F];
 
217
            encodeBuf[0] = PEM_ARRAY[((a >>> 2) & 0x3F)];
 
218
            encodeBuf[1] = PEM_ARRAY[(((a << 4) & 0x30) | ((b >>> 4) & 0x0F))];
 
219
            encodeBuf[2] = PEM_ARRAY[(((b << 2) & 0x3C) | ((c >>> 6) & 0x03))];
 
220
            encodeBuf[3] = PEM_ARRAY[c & 0x3F];
197
221
            out.write(encodeBuf);
198
222
 
199
223
            lineLen += 4;
202
226
                lineLen = 0;
203
227
            }
204
228
 
205
 
            len -=3;
 
229
            len -= 3;
206
230
        }
207
231
 
208
232
        switch (len) {
211
235
            break;
212
236
        case 2:
213
237
            atom[0] = data[offset];
214
 
            atom[1] = data[offset+1];
 
238
            atom[1] = data[offset + 1];
215
239
            break;
216
240
        default:
217
241
        }