jdk/src/java.base/share/classes/sun/net/smtp/SmtpClient.java
author redestad
Tue, 03 May 2016 15:50:54 +0200
changeset 37781 71ed5645f17c
parent 37593 824750ada3d6
child 44758 e9f82f61bc22
permissions -rw-r--r--
8155775: Re-examine naming of privileged methods to access System properties Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10419
diff changeset
     2
 * Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.net.smtp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.net.TransferProtocolClient;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 25859
diff changeset
    31
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class implements the SMTP client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * You can send a piece of mail by creating a new SmtpClient, calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * the "to" method to add destinations, calling "from" to name the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * sender, calling startMessage to return a stream to which you write
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * the message (with RFC733 headers) and then you finally close the Smtp
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author      James Gosling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class SmtpClient extends TransferProtocolClient {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    String mailhost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    SmtpPrintStream message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * issue the QUIT command to the SMTP server and close the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public void closeServer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        if (serverIsOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            closeMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            issueCommand("QUIT\r\n", 221);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            super.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    void issueCommand(String cmd, int expect) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        sendServer(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        int reply;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        while ((reply = readServerResponse()) != expect)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            if (reply != 220) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                throw new SmtpProtocolException(getResponseString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private void toCanonical(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (s.startsWith("<"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            issueCommand("rcpt to: " + s + "\r\n", 250);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            issueCommand("rcpt to: <" + s + ">\r\n", 250);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public void to(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        int st = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        int limit = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        int pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        int lastnonsp = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        int parendepth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        boolean ignore = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        while (pos < limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            int c = s.charAt(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (parendepth > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                if (c == '(')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    parendepth++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                else if (c == ')')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    parendepth--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                if (parendepth == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    if (lastnonsp > st)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        ignore = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                        st = pos + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            } else if (c == '(')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                parendepth++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            else if (c == '<')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                st = lastnonsp = pos + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            else if (c == '>')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                ignore = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            else if (c == ',') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                if (lastnonsp > st)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    toCanonical(s.substring(st, lastnonsp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                st = pos + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                ignore = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                if (c > ' ' && !ignore)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    lastnonsp = pos + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                else if (st == pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    st++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (lastnonsp > st)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            toCanonical(s.substring(st, lastnonsp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public void from(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (s.startsWith("<"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            issueCommand("mail from: " + s + "\r\n", 250);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            issueCommand("mail from: <" + s + ">\r\n", 250);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /** open a SMTP connection to host <i>host</i>. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private void openServer(String host) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        mailhost = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        openServer(mailhost, 25);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        issueCommand("helo "+InetAddress.getLocalHost().getHostName()+"\r\n", 250);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public PrintStream startMessage() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        issueCommand("data\r\n", 354);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            message = new SmtpPrintStream(serverOutput, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } catch (UnsupportedEncodingException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   137
            throw new InternalError(encoding+" encoding not found", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    void closeMessage() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (message != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            message.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /** New SMTP client connected to host <i>host</i>. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public SmtpClient (String host) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (host != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                openServer(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                mailhost = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            String s;
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
   160
            mailhost = GetPropertyAction.privilegedGetProperty("mail.host");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            if (mailhost != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                openServer(mailhost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            mailhost = "localhost";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            openServer(mailhost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            mailhost = "mailhost";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            openServer(mailhost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /** Create an uninitialized SMTP client. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public SmtpClient () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        this(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public SmtpClient(int to) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        setConnectTimeout(to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            String s;
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
   186
            mailhost = GetPropertyAction.privilegedGetProperty("mail.host");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (mailhost != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                openServer(mailhost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            mailhost = "localhost";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            openServer(mailhost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            mailhost = "mailhost";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            openServer(mailhost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public String getMailHost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return mailhost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    String getEncoding () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return encoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
class SmtpPrintStream extends java.io.PrintStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private SmtpClient target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private int lastc = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    SmtpPrintStream (OutputStream fos, SmtpClient cl) throws UnsupportedEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        super(fos, false, cl.getEncoding());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        target = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (target == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (lastc != '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            write('\n');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            target.issueCommand(".\r\n", 250);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            target.message = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            out = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            target = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public void write(int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            // quote a dot at the beginning of a line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            if (lastc == '\n' && b == '.') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                out.write('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            // translate NL to CRLF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (b == '\n' && lastc != '\r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                out.write('\r');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            out.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            lastc = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public void write(byte b[], int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            int lc = lastc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            while (--len >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                int c = b[off++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                // quote a dot at the beginning of a line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                if (lc == '\n' && c == '.')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    out.write('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                // translate NL to CRLF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                if (c == '\n' && lc != '\r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    out.write('\r');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                out.write(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                lc = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            lastc = lc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public void print(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        int len = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            write(s.charAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
}