src/java.base/share/classes/sun/net/TelnetInputStream.java
author chegar
Fri, 18 Oct 2019 21:25:01 +0100
branchdatagramsocketimpl-branch
changeset 58697 e3ff12d14d43
parent 47216 71c04702a3d5
permissions -rw-r--r--
datagramsocketimpl-branch: minor refactoring
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, 1995, 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 read 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
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
    34
 * are terminated with a single newline {@literal <LF>} character.
2
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 TelnetInputStream extends FilterInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /** If stickyCRLF is true, then we're a machine, like an IBM PC,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        where a Newline is a CR followed by LF.  On UNIX, this is false
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        because Newline is represented with just a LF character. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    boolean         stickyCRLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    boolean         seenCR = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public boolean  binaryMode = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public TelnetInputStream(InputStream fd, boolean binary) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        super(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        binaryMode = binary;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public void setStickyCRLF(boolean on) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        stickyCRLF = on;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (binaryMode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            return super.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        /* If last time we determined we saw a CRLF pair, and we're
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
           not turning that into just a Newline (that is, we're
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
           stickyCRLF), then return the LF part of that sticky
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
           pair now. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (seenCR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            seenCR = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if ((c = super.read()) == '\r') {    /* CR */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            switch (c = super.read()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            case -1:                        /* this is an error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                throw new TelnetProtocolException("misplaced CR in input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            case 0:                         /* NUL - treat CR as CR */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                return '\r';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            case '\n':                      /* CRLF - treat as NL */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                if (stickyCRLF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    seenCR = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    return '\r';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    return '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /** read into a byte array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public int read(byte bytes[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return read(bytes, 0, bytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Read into a byte array at offset <i>off</i> for length <i>length</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public int read(byte bytes[], int off, int length) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (binaryMode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return super.read(bytes, off, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        int offStart = off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        while (--length >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (c == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            bytes[off++] = (byte)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return (off > offStart) ? off - offStart : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
}