Path: blob/master/src/java.base/share/classes/sun/net/TelnetOutputStream.java
41152 views
/*1* Copyright (c) 1994, 2013, 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 write 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 TelnetOutputStream extends BufferedOutputStream {72boolean stickyCRLF = false;73boolean seenCR = false;7475public boolean binaryMode = false;7677public TelnetOutputStream(OutputStream fd, boolean binary) {78super(fd);79binaryMode = binary;80}8182/**83* set the stickyCRLF flag. Tells whether the terminal considers CRLF as a single84* char.85*86* @param on the <code>boolean</code> to set the flag to.87*/88public void setStickyCRLF(boolean on) {89stickyCRLF = on;90}9192/**93* Writes the int to the stream and does CR LF processing if necessary.94*/95public void write(int c) throws IOException {96if (binaryMode) {97super.write(c);98return;99}100101if (seenCR) {102if (c != '\n')103super.write(0);104super.write(c);105if (c != '\r')106seenCR = false;107} else { // !seenCR108if (c == '\n') {109super.write('\r');110super.write('\n');111return;112}113if (c == '\r') {114if (stickyCRLF)115seenCR = true;116else {117super.write('\r');118c = 0;119}120}121super.write(c);122}123}124125/**126* Write the bytes at offset <i>off</i> in buffer <i>bytes</i> for127* <i>length</i> bytes.128*/129public void write(byte bytes[], int off, int length) throws IOException {130if (binaryMode) {131super.write(bytes, off, length);132return;133}134135while (--length >= 0) {136write(bytes[off++]);137}138}139}140141142