|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| package org.apache.geronimo.mail.util; |
|
19 |
| |
|
20 |
| import java.io.IOException; |
|
21 |
| import java.io.InputStream; |
|
22 |
| import java.io.FilterInputStream; |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| public class Base64DecoderStream extends FilterInputStream { |
|
32 |
| |
|
33 |
| static protected final String MAIL_BASE64_IGNOREERRORS = "mail.mime.base64.ignoreerrors"; |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| static protected final int BUFFERED_UNITS = 2000; |
|
38 |
| |
|
39 |
| |
|
40 |
| protected Base64Encoder decoder = new Base64Encoder(); |
|
41 |
| |
|
42 |
| |
|
43 |
| protected boolean ignoreErrors = false; |
|
44 |
| |
|
45 |
| |
|
46 |
| protected byte[] encodedChars = new byte[BUFFERED_UNITS * 4]; |
|
47 |
| |
|
48 |
| |
|
49 |
| protected byte[] decodedChars = new byte[BUFFERED_UNITS * 3]; |
|
50 |
| |
|
51 |
| protected int decodedCount = 0; |
|
52 |
| |
|
53 |
| protected int decodedIndex = 0; |
|
54 |
| |
|
55 |
| |
|
56 |
4
| public Base64DecoderStream(InputStream in) {
|
|
57 |
4
| super(in);
|
|
58 |
| |
|
59 |
4
| ignoreErrors = SessionUtil.getBooleanProperty(MAIL_BASE64_IGNOREERRORS, false);
|
|
60 |
| } |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
6
| private boolean dataAvailable() {
|
|
69 |
6
| return decodedCount != 0;
|
|
70 |
| } |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
0
| private byte getBufferedChar() {
|
|
78 |
0
| decodedCount--;
|
|
79 |
0
| return decodedChars[decodedIndex++];
|
|
80 |
| } |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
6
| private boolean decodeStreamData() throws IOException {
|
|
88 |
6
| decodedIndex = 0;
|
|
89 |
| |
|
90 |
| |
|
91 |
6
| int readCharacters = fillEncodedBuffer();
|
|
92 |
| |
|
93 |
6
| if (readCharacters > 0) {
|
|
94 |
4
| decodedCount = decoder.decode(encodedChars, 0, readCharacters, decodedChars);
|
|
95 |
4
| return true;
|
|
96 |
| } |
|
97 |
2
| return false;
|
|
98 |
| } |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
0
| private int getByte() throws IOException {
|
|
107 |
0
| if (!dataAvailable()) {
|
|
108 |
0
| if (!decodeStreamData()) {
|
|
109 |
0
| return -1;
|
|
110 |
| } |
|
111 |
| } |
|
112 |
0
| decodedCount--;
|
|
113 |
0
| return decodedChars[decodedIndex++];
|
|
114 |
| } |
|
115 |
| |
|
116 |
5
| private int getBytes(byte[] data, int offset, int length) throws IOException {
|
|
117 |
| |
|
118 |
5
| int readCharacters = 0;
|
|
119 |
5
| while (length > 0) {
|
|
120 |
| |
|
121 |
6
| if (!dataAvailable()) {
|
|
122 |
| |
|
123 |
6
| if (!decodeStreamData()) {
|
|
124 |
2
| return readCharacters > 0 ? readCharacters : -1;
|
|
125 |
| } |
|
126 |
| } |
|
127 |
| |
|
128 |
| |
|
129 |
4
| int copyCount = Math.min(decodedCount, length);
|
|
130 |
4
| System.arraycopy(decodedChars, decodedIndex, data, offset, copyCount);
|
|
131 |
4
| decodedIndex += copyCount;
|
|
132 |
4
| decodedCount -= copyCount;
|
|
133 |
4
| offset += copyCount;
|
|
134 |
4
| length -= copyCount;
|
|
135 |
4
| readCharacters += copyCount;
|
|
136 |
| } |
|
137 |
3
| return readCharacters;
|
|
138 |
| } |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
6
| private int fillEncodedBuffer() throws IOException
|
|
151 |
| { |
|
152 |
6
| int readCharacters = 0;
|
|
153 |
| |
|
154 |
6
| while (true) {
|
|
155 |
| |
|
156 |
1062
| int ch = in.read();
|
|
157 |
| |
|
158 |
1062
| if (ch == -1) {
|
|
159 |
| |
|
160 |
| |
|
161 |
6
| if ((readCharacters % 4) != 0) {
|
|
162 |
| |
|
163 |
0
| if (!ignoreErrors) {
|
|
164 |
0
| throw new IOException("Base64 encoding error, data truncated");
|
|
165 |
| } |
|
166 |
| |
|
167 |
0
| return (readCharacters / 4) * 4;
|
|
168 |
| } |
|
169 |
| |
|
170 |
6
| return readCharacters;
|
|
171 |
| } |
|
172 |
| |
|
173 |
1056
| else if (decoder.isValidBase64(ch)) {
|
|
174 |
1032
| encodedChars[readCharacters++] = (byte)ch;
|
|
175 |
| |
|
176 |
1032
| if (readCharacters >= encodedChars.length) {
|
|
177 |
0
| return readCharacters;
|
|
178 |
| } |
|
179 |
| } |
|
180 |
| |
|
181 |
| |
|
182 |
| } |
|
183 |
| } |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
0
| public int read() throws IOException
|
|
190 |
| { |
|
191 |
0
| return getByte();
|
|
192 |
| } |
|
193 |
| |
|
194 |
| |
|
195 |
5
| public int read(byte [] buffer, int offset, int length) throws IOException {
|
|
196 |
5
| return getBytes(buffer, offset, length);
|
|
197 |
| } |
|
198 |
| |
|
199 |
| |
|
200 |
0
| public boolean markSupported() {
|
|
201 |
0
| return false;
|
|
202 |
| } |
|
203 |
| |
|
204 |
| |
|
205 |
0
| public int available() throws IOException {
|
|
206 |
0
| return ((in.available() / 4) * 3) + decodedCount;
|
|
207 |
| } |
|
208 |
| } |