Skip to content

Package: SMTPOutputStream

SMTPOutputStream

nameinstructionbranchcomplexitylinemethod
SMTPOutputStream(OutputStream)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
ensureAtBOL()
M: 6 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
flush()
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
write(byte[], int, int)
M: 62 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
write(int)
M: 23 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package org.eclipse.angus.mail.smtp;
18:
19: import java.io.*;
20: import org.eclipse.angus.mail.util.CRLFOutputStream;
21:
22: /**
23: * In addition to converting lines into the canonical format,
24: * i.e., terminating lines with the CRLF sequence, escapes the "."
25: * by adding another "." to any "." that appears in the beginning
26: * of a line. See RFC821 section 4.5.2.
27: *
28: * @author Max Spivak
29: * @see CRLFOutputStream
30: */
31: public class SMTPOutputStream extends CRLFOutputStream {
32: public SMTPOutputStream(OutputStream os) {
33:         super(os);
34: }
35:
36: @Override
37: public void write(int b) throws IOException {
38:         // if that last character was a newline, and the current
39:         // character is ".", we always write out an extra ".".
40:•        if ((lastb == '\n' || lastb == '\r' || lastb == -1) && b == '.') {
41:          out.write('.');
42:         }
43:         
44:         super.write(b);
45: }
46:
47: /*
48: * This method has been added to improve performance.
49: */
50: @Override
51: public void write(byte b[], int off, int len) throws IOException {
52:•        int lastc = (lastb == -1) ? '\n' : lastb;
53:         int start = off;
54:         
55:         len += off;
56:•        for (int i = off; i < len; i++) {
57:•         if ((lastc == '\n' || lastc == '\r') && b[i] == '.') {
58:                 super.write(b, start, i - start);
59:                 out.write('.');
60:                 start = i;
61:          }
62:          lastc = b[i];
63:         }
64:•        if ((len - start) > 0)
65:          super.write(b, start, len - start);
66: }
67:
68: /**
69: * Override flush method in FilterOutputStream.
70: *
71: * The MimeMessage writeTo method flushes its buffer at the end,
72: * but we don't want to flush data out to the socket until we've
73: * also written the terminating "\r\n.\r\n".
74: *
75: * We buffer nothing so there's nothing to flush. We depend
76: * on the fact that CRLFOutputStream also buffers nothing.
77: * SMTPTransport will manually flush the socket before reading
78: * the response.
79: */
80: @Override
81: public void flush() {
82:         // do nothing
83: }
84:
85: /**
86: * Ensure we're at the beginning of a line.
87: * Write CRLF if not.
88: *
89: * @exception        IOException        if the write fails
90: */
91: public void ensureAtBOL() throws IOException {
92:•        if (!atBOL)
93:          super.writeln();
94: }
95: }