|
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.FilterOutputStream; |
|
21 |
| import java.io.IOException; |
|
22 |
| import java.io.OutputStream; |
|
23 |
| import java.io.PrintStream; |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| public class UUEncoderStream extends FilterOutputStream { |
|
33 |
| |
|
34 |
| |
|
35 |
| protected static final int DEFAULT_MODE = 644; |
|
36 |
| protected static final String DEFAULT_NAME = "encoder.buf"; |
|
37 |
| |
|
38 |
| protected static final int MAX_CHARS_PER_LINE = 45; |
|
39 |
| |
|
40 |
| |
|
41 |
| protected String name; |
|
42 |
| |
|
43 |
| protected int mode; |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| protected boolean beginWritten = false; |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| protected UUEncoder encoder = new UUEncoder(); |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| protected int bufferedBytes = 0; |
|
58 |
| |
|
59 |
| |
|
60 |
| protected byte[] buffer = new byte[45]; |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
5
| public UUEncoderStream(OutputStream out) {
|
|
69 |
5
| this(out, DEFAULT_NAME, DEFAULT_MODE);
|
|
70 |
| } |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
0
| public UUEncoderStream(OutputStream out, String name) {
|
|
81 |
0
| this(out, name, DEFAULT_MODE);
|
|
82 |
| } |
|
83 |
| |
|
84 |
| |
|
85 |
5
| public UUEncoderStream(OutputStream out, String name, int mode) {
|
|
86 |
5
| super(out);
|
|
87 |
| |
|
88 |
5
| this.name = name;
|
|
89 |
5
| this.mode = mode;
|
|
90 |
| } |
|
91 |
| |
|
92 |
| |
|
93 |
10
| private void checkBegin() throws IOException {
|
|
94 |
10
| if (!beginWritten) {
|
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
5
| PrintStream writer = new PrintStream(out);
|
|
99 |
| |
|
100 |
5
| writer.print("begin " + mode + " " + name + "\r\n");
|
|
101 |
5
| writer.flush();
|
|
102 |
5
| beginWritten = true;
|
|
103 |
| } |
|
104 |
| } |
|
105 |
| |
|
106 |
5
| private void writeEnd() throws IOException {
|
|
107 |
5
| PrintStream writer = new PrintStream(out);
|
|
108 |
| |
|
109 |
5
| writer.print("\nend\r\n");
|
|
110 |
5
| writer.flush();
|
|
111 |
| } |
|
112 |
| |
|
113 |
10
| private void flushBuffer() throws IOException {
|
|
114 |
| |
|
115 |
10
| checkBegin();
|
|
116 |
| |
|
117 |
10
| if (bufferedBytes != 0) {
|
|
118 |
10
| encoder.encode(buffer, 0, bufferedBytes, out);
|
|
119 |
| |
|
120 |
10
| bufferedBytes = 0;
|
|
121 |
| } |
|
122 |
| } |
|
123 |
| |
|
124 |
5
| private int bufferSpace() {
|
|
125 |
5
| return MAX_CHARS_PER_LINE - bufferedBytes;
|
|
126 |
| } |
|
127 |
| |
|
128 |
5
| private boolean isBufferFull() {
|
|
129 |
5
| return bufferedBytes >= MAX_CHARS_PER_LINE;
|
|
130 |
| } |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
0
| public void write(int ch) throws IOException {
|
|
136 |
| |
|
137 |
0
| buffer[bufferedBytes++] = (byte)ch;
|
|
138 |
| |
|
139 |
| |
|
140 |
0
| if (isBufferFull()) {
|
|
141 |
0
| flushBuffer();
|
|
142 |
| } |
|
143 |
| } |
|
144 |
| |
|
145 |
0
| public void write(byte [] data) throws IOException {
|
|
146 |
0
| write(data, 0, data.length);
|
|
147 |
| } |
|
148 |
| |
|
149 |
5
| public void write(byte [] data, int offset, int length) throws IOException {
|
|
150 |
| |
|
151 |
5
| int copyBytes = Math.min(bufferSpace(), length);
|
|
152 |
| |
|
153 |
5
| System.arraycopy(buffer, bufferedBytes, data, offset, copyBytes);
|
|
154 |
5
| bufferedBytes += copyBytes;
|
|
155 |
5
| offset += copyBytes;
|
|
156 |
5
| length -= copyBytes;
|
|
157 |
| |
|
158 |
| |
|
159 |
5
| if (isBufferFull()) {
|
|
160 |
5
| flushBuffer();
|
|
161 |
| } |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
5
| if (length >= MAX_CHARS_PER_LINE) {
|
|
166 |
5
| int fullLinesLength = (length / MAX_CHARS_PER_LINE) * MAX_CHARS_PER_LINE;
|
|
167 |
| |
|
168 |
5
| encoder.encode(data, offset, fullLinesLength, out);
|
|
169 |
5
| offset += fullLinesLength;
|
|
170 |
5
| length -= fullLinesLength;
|
|
171 |
| } |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
5
| if (length > 0) {
|
|
177 |
5
| System.arraycopy(buffer, 0, data, offset, length);
|
|
178 |
5
| bufferedBytes += length;
|
|
179 |
5
| offset += length;
|
|
180 |
5
| length -= length;
|
|
181 |
| } |
|
182 |
| } |
|
183 |
| |
|
184 |
5
| public void flush() throws IOException {
|
|
185 |
| |
|
186 |
5
| flushBuffer();
|
|
187 |
| |
|
188 |
5
| writeEnd();
|
|
189 |
| |
|
190 |
5
| out.flush();
|
|
191 |
| } |
|
192 |
| |
|
193 |
0
| public void close() throws IOException {
|
|
194 |
| |
|
195 |
0
| flush();
|
|
196 |
0
| out.close();
|
|
197 |
| } |
|
198 |
| |
|
199 |
| } |
|
200 |
| |
|
201 |
| |