KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mail > smtp > SMTPOutputStream


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)SMTPOutputStream.java 1.10 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.mail.smtp;
29
30 import java.io.*;
31 import com.sun.mail.util.CRLFOutputStream;
32
33 /**
34  * In addition to converting lines into the canonical format,
35  * i.e., terminating lines with the CRLF sequence, escapes the "."
36  * by adding another "." to any "." that appears in the beginning
37  * of a line. See RFC821 section 4.5.2.
38  *
39  * @author Max Spivak
40  * @see CRLFOutputStream
41  */

42 public class SMTPOutputStream extends CRLFOutputStream {
43     public SMTPOutputStream(OutputStream os) {
44     super(os);
45     }
46
47     public void write(int b) throws IOException {
48     // if that last character was a newline, and the current
49
// character is ".", we always write out an extra ".".
50
if ((lastb == '\n' || lastb == '\r' || lastb == -1) && b == '.') {
51         out.write('.');
52     }
53     
54     super.write(b);
55     }
56
57     /*
58      * This method has been added to improve performance.
59      */

60     public void write(byte b[], int off, int len) throws IOException {
61     int lastc = (lastb == -1) ? '\n' : lastb;
62     int start = off;
63     
64     len += off;
65     for (int i = off; i < len; i++) {
66         if ((lastc == '\n' || lastc == '\r') && b[i] == '.') {
67         super.write(b, start, i - start);
68         out.write('.');
69         start = i;
70         }
71         lastc = b[i];
72     }
73     if ((len - start) > 0)
74         super.write(b, start, len - start);
75     }
76
77     /**
78      * Override flush method in FilterOutputStream.
79      *
80      * The MimeMessage writeTo method flushes its buffer at the end,
81      * but we don't want to flush data out to the socket until we've
82      * also written the terminating "\r\n.\r\n".
83      *
84      * We buffer nothing so there's nothing to flush. We depend
85      * on the fact that CRLFOutputStream also buffers nothing.
86      * SMTPTransport will manually flush the socket before reading
87      * the response.
88      */

89     public void flush() {
90     // do nothing
91
}
92 }
93
Popular Tags