1 /**
2 *
3 * Copyright 2006 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.geronimo.javamail.util;
19
20 import java.io.IOException;
21 import java.io.OutputStream;
22
23 /**
24 * An implementation of an OutputStream that performs MIME linebreak
25 * canonicalization and "byte-stuff" so that data content does not get mistaken
26 * for a message data-end marker (CRLF.CRLF)l
27 *
28 * @version $Rev: 432884 $ $Date: 2006-08-19 14:53:20 -0700 (Sat, 19 Aug 2006) $
29 */
30 public class MIMEOutputStream extends OutputStream {
31
32
33 protected OutputStream out;
34
35
36 protected int lastWrite = -1;
37
38
39
40
41
42
43 protected boolean atLineBreak = true;
44
45 /**
46 * Create an output stream that writes to the target output stream.
47 *
48 * @param out
49 * The wrapped output stream.
50 */
51 public MIMEOutputStream(OutputStream out) {
52 this.out = out;
53 }
54
55
56
57
58 public void write(int ch) throws IOException {
59
60
61 if (ch == '\r') {
62 out.write((byte) '\r');
63 out.write((byte) '\n');
64
65 atLineBreak = true;
66 }
67
68
69 else if (ch == '\n') {
70
71 if (lastWrite != '\r') {
72
73 out.write((byte) '\r');
74 out.write((byte) '\n');
75 }
76
77
78 atLineBreak = true;
79 }
80
81 else if (ch == '.') {
82
83
84 if (atLineBreak) {
85 out.write('.');
86 }
87 out.write('.');
88 atLineBreak = false;
89 } else {
90
91 out.write(ch);
92 atLineBreak = false;
93 }
94
95 lastWrite = ch;
96 }
97 }