jdk/src/share/classes/sun/net/TelnetOutputStream.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 5506 202f599c92aa
child 23010 6dadb192ad81
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1994, 2002, 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;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * This class provides input and output streams for telnet clients.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * This class overrides write to do CRLF processing as specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * RFC 854. The class assumes it is running on a system where lines
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * are terminated with a single newline <LF> character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This is the relevant section of RFC 824 regarding CRLF processing:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The sequence "CR LF", as defined, will cause the NVT to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * positioned at the left margin of the next print line (as would,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * for example, the sequence "LF CR").  However, many systems and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * terminals do not treat CR and LF independently, and will have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * go to some effort to simulate their effect.  (For example, some
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * terminals do not have a CR independent of the LF, but on such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * terminals it may be possible to simulate a CR by backspacing.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Therefore, the sequence "CR LF" must be treated as a single "new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * line" character and used whenever their combined action is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * intended; the sequence "CR NUL" must be used where a carriage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * return alone is actually desired; and the CR character must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * avoided in other contexts.  This rule gives assurance to systems
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * which must decide whether to perform a "new line" function or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * multiple-backspace that the TELNET stream contains a character
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * following a CR that will allow a rational decision.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *    Note that "CR LF" or "CR NUL" is required in both directions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *    (in the default ASCII mode), to preserve the symmetry of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *    NVT model.  Even though it may be known in some situations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *    (e.g., with remote echo and suppress go ahead options in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *    effect) that characters are not being sent to an actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *    printer, nonetheless, for the sake of consistency, the protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *    requires that a NUL be inserted following a CR not followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *    a LF in the data stream.  The converse of this is that a NUL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *    received in the data stream after a CR (in the absence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *    options negotiations which explicitly specify otherwise) should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *    be stripped out prior to applying the NVT to local character
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *    set mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @author      Jonathan Payne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
public class TelnetOutputStream extends BufferedOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    boolean         stickyCRLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    boolean         seenCR = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public boolean  binaryMode = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public TelnetOutputStream(OutputStream fd, boolean binary) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        super(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        binaryMode = binary;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
    84
     * set the stickyCRLF flag. Tells whether the terminal considers CRLF as a single
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * char.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param   on      the <code>boolean</code> to set the flag to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public void setStickyCRLF(boolean on) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        stickyCRLF = on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Writes the int to the stream and does CR LF processing if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public void write(int c) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (binaryMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            super.write(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (seenCR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (c != '\n')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                super.write(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            super.write(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (c != '\r')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                seenCR = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        } else { // !seenCR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (c == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                super.write('\r');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                super.write('\n');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (c == '\r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                if (stickyCRLF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    seenCR = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    super.write('\r');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    c = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            super.write(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Write the bytes at offset <i>off</i> in buffer <i>bytes</i> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <i>length</i> bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public void write(byte bytes[], int off, int length) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (binaryMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            super.write(bytes, off, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        while (--length >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            write(bytes[off++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
}