Path: blob/master/src/java.base/share/classes/sun/net/TelnetInputStream.java
41152 views
/*1* Copyright (c) 1994, 1995, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.net;2627import java.io.*;2829/**30* This class provides input and output streams for telnet clients.31* This class overrides read to do CRLF processing as specified in32* RFC 854. The class assumes it is running on a system where lines33* are terminated with a single newline {@literal <LF>} character.34*35* This is the relevant section of RFC 824 regarding CRLF processing:36*37* <pre>38* The sequence "CR LF", as defined, will cause the NVT to be39* positioned at the left margin of the next print line (as would,40* for example, the sequence "LF CR"). However, many systems and41* terminals do not treat CR and LF independently, and will have to42* go to some effort to simulate their effect. (For example, some43* terminals do not have a CR independent of the LF, but on such44* terminals it may be possible to simulate a CR by backspacing.)45* Therefore, the sequence "CR LF" must be treated as a single "new46* line" character and used whenever their combined action is47* intended; the sequence "CR NUL" must be used where a carriage48* return alone is actually desired; and the CR character must be49* avoided in other contexts. This rule gives assurance to systems50* which must decide whether to perform a "new line" function or a51* multiple-backspace that the TELNET stream contains a character52* following a CR that will allow a rational decision.53*54* Note that "CR LF" or "CR NUL" is required in both directions55* (in the default ASCII mode), to preserve the symmetry of the56* NVT model. Even though it may be known in some situations57* (e.g., with remote echo and suppress go ahead options in58* effect) that characters are not being sent to an actual59* printer, nonetheless, for the sake of consistency, the protocol60* requires that a NUL be inserted following a CR not followed by61* a LF in the data stream. The converse of this is that a NUL62* received in the data stream after a CR (in the absence of63* options negotiations which explicitly specify otherwise) should64* be stripped out prior to applying the NVT to local character65* set mapping.66* </pre>67*68* @author Jonathan Payne69*/7071public class TelnetInputStream extends FilterInputStream {72/** If stickyCRLF is true, then we're a machine, like an IBM PC,73where a Newline is a CR followed by LF. On UNIX, this is false74because Newline is represented with just a LF character. */75boolean stickyCRLF = false;76boolean seenCR = false;7778public boolean binaryMode = false;7980public TelnetInputStream(InputStream fd, boolean binary) {81super(fd);82binaryMode = binary;83}8485public void setStickyCRLF(boolean on) {86stickyCRLF = on;87}8889public int read() throws IOException {90if (binaryMode)91return super.read();9293int c;9495/* If last time we determined we saw a CRLF pair, and we're96not turning that into just a Newline (that is, we're97stickyCRLF), then return the LF part of that sticky98pair now. */99100if (seenCR) {101seenCR = false;102return '\n';103}104105if ((c = super.read()) == '\r') { /* CR */106switch (c = super.read()) {107default:108case -1: /* this is an error */109throw new TelnetProtocolException("misplaced CR in input");110111case 0: /* NUL - treat CR as CR */112return '\r';113114case '\n': /* CRLF - treat as NL */115if (stickyCRLF) {116seenCR = true;117return '\r';118} else {119return '\n';120}121}122}123return c;124}125126/** read into a byte array */127public int read(byte bytes[]) throws IOException {128return read(bytes, 0, bytes.length);129}130131/**132* Read into a byte array at offset <i>off</i> for length <i>length</i>133* bytes.134*/135public int read(byte bytes[], int off, int length) throws IOException {136if (binaryMode)137return super.read(bytes, off, length);138139int c;140int offStart = off;141142while (--length >= 0) {143c = read();144if (c == -1)145break;146bytes[off++] = (byte)c;147}148return (off > offStart) ? off - offStart : -1;149}150}151152153