📄 terminalio.java
字号:
//License/*** * Java TelnetD library (embeddable telnet daemon) * Copyright (c) 2000-2005 Dieter Wimberger * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of the author nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. ***/package net.wimpi.telnetd.io;import net.wimpi.telnetd.io.terminal.Terminal;import net.wimpi.telnetd.io.terminal.TerminalManager;import net.wimpi.telnetd.net.Connection;import net.wimpi.telnetd.net.ConnectionData;import net.wimpi.telnetd.net.ConnectionEvent;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import java.io.IOException;/** * Class for Terminal specific I/O. * It represents the layer between the application layer and the generic telnet I/O. * Terminal specific I/O is achieved via pluggable terminal classes * * @author Dieter Wimberger * @version 2.0 (16/07/2006) * @see net.wimpi.telnetd.io.TelnetIO * @see net.wimpi.telnetd.io.terminal.Terminal */public class TerminalIO implements BasicTerminalIO { private static Log log = LogFactory.getLog(TerminalIO.class); private TelnetIO m_TelnetIO; //low level I/O private Connection m_Connection; //the connection this instance is working for private ConnectionData m_ConnectionData; //holds data of the connection private Terminal m_Terminal; //active terminal object //Members private boolean m_AcousticSignalling; //flag for accoustic signalling private boolean m_Autoflush; //flag for autoflushing mode private boolean m_ForceBold; //flag for forcing bold output private boolean m_LineWrapping; /** * Constructor of the TerminalIO class. * * @param con Connection the instance will be working for */ public TerminalIO(Connection con) { m_Connection = con; m_AcousticSignalling = true; m_Autoflush = true; //store the associated ConnectionData instance m_ConnectionData = m_Connection.getConnectionData(); try { //create a new telnet io m_TelnetIO = new TelnetIO(); m_TelnetIO.setConnection(con); m_TelnetIO.initIO(); } catch (Exception ex) { //handle, at least log } //set default terminal try { setDefaultTerminal(); } catch (Exception ex) { log.error("TerminalIO()", ex); throw new RuntimeException(); } }//constructor /************************************************************************ * Visible character I/O methods * ************************************************************************/ /** * Read a single character and take care for terminal function calls. * * @return <ul> * <li>character read * <li>IOERROR in case of an error * <li>DELETE,BACKSPACE,TABULATOR,ESCAPE,COLORINIT,LOGOUTREQUEST * <li>UP,DOWN,LEFT,RIGHT * </ul> */ public synchronized int read() throws IOException { int i = m_TelnetIO.read(); //translate possible control sequences i = m_Terminal.translateControlCharacter(i); //catch & fire a logoutrequest event if (i == LOGOUTREQUEST) { m_Connection.processConnectionEvent(new ConnectionEvent(m_Connection, ConnectionEvent.CONNECTION_LOGOUTREQUEST)); i = HANDLED; } else if (i > 256 && i == ESCAPE) { //translate an incoming escape sequence i = handleEscapeSequence(i); } //return i holding a char or a defined special key return i; }//read public synchronized void write(byte b) throws IOException { m_TelnetIO.write(b); if (m_Autoflush) { flush(); } }//write public synchronized void write(char ch) throws IOException { m_TelnetIO.write(ch); if (m_Autoflush) { flush(); } }//write(char) public synchronized void write(String str) throws IOException { if (m_ForceBold) { m_TelnetIO.write(m_Terminal.formatBold(str)); } else { m_TelnetIO.write(m_Terminal.format(str)); } if (m_Autoflush) { flush(); } }//write(String) /*** End of Visible character I/O methods ******************************/ /** * ********************************************************************* * Erase methods * * ********************************************************************** */ public synchronized void eraseToEndOfLine() throws IOException { doErase(EEOL); }//eraseToEndOfLine public synchronized void eraseToBeginOfLine() throws IOException { doErase(EBOL); }//eraseToBeginOfLine public synchronized void eraseLine() throws IOException { doErase(EEL); }//eraseLine public synchronized void eraseToEndOfScreen() throws IOException { doErase(EEOS); }//eraseToEndOfScreen public synchronized void eraseToBeginOfScreen() throws IOException { doErase(EBOS); }//eraseToBeginOfScreen public synchronized void eraseScreen() throws IOException { doErase(EES); }//eraseScreen private synchronized void doErase(int funcConst) throws IOException { m_TelnetIO.write(m_Terminal.getEraseSequence(funcConst)); if (m_Autoflush) { flush(); } }//erase /*** End of Erase methods **********************************************/ /** * ********************************************************************* * Cursor related methods * * ********************************************************************** */ public synchronized void moveCursor(int direction, int times) throws IOException { m_TelnetIO.write(m_Terminal.getCursorMoveSequence(direction, times)); if (m_Autoflush) { flush(); } }//moveCursor public synchronized void moveLeft(int times) throws IOException { moveCursor(LEFT, times); }//moveLeft public synchronized void moveRight(int times) throws IOException { moveCursor(RIGHT, times); }//moveRight public synchronized void moveUp(int times) throws IOException { moveCursor(UP, times); }//moveUp public synchronized void moveDown(int times) throws IOException { moveCursor(DOWN, times); }//moveDown public synchronized void setCursor(int row, int col) throws IOException { int[] pos = new int[2]; pos[0] = row; pos[1] = col; m_TelnetIO.write(m_Terminal.getCursorPositioningSequence(pos)); if (m_Autoflush) { flush(); } }//setCursor public synchronized void homeCursor() throws IOException { m_TelnetIO.write(m_Terminal.getCursorPositioningSequence(HOME)); if (m_Autoflush) { flush(); } }//homeCursor public synchronized void storeCursor() throws IOException { m_TelnetIO.write(m_Terminal.getSpecialSequence(STORECURSOR)); }//store Cursor public synchronized void restoreCursor() throws IOException { m_TelnetIO.write(m_Terminal.getSpecialSequence(RESTORECURSOR)); }//restore Cursor /*** End of cursor related methods **************************************/ /** * ********************************************************************* * Special terminal function methods * * ********************************************************************** */ public synchronized void setSignalling(boolean bool) { m_AcousticSignalling = bool; }//setAcousticSignalling public synchronized boolean isSignalling() { return m_AcousticSignalling; }//isAcousticSignalling /** * Method to write the NVT defined BEL onto the stream. * If signalling is off, the method simply returns, without * any action. */ public synchronized void bell() throws IOException { if (m_AcousticSignalling) { m_TelnetIO.write(BEL); } if (m_Autoflush) { flush(); } }//bell /** * EXPERIMENTAL, not defined in the interface. */ public synchronized boolean defineScrollRegion(int topmargin, int bottommargin) throws IOException { if (m_Terminal.supportsScrolling()) { m_TelnetIO.write(m_Terminal.getScrollMarginsSequence(topmargin, bottommargin)); flush(); return true; } else { return false; } }//defineScrollRegion public synchronized void setForegroundColor(int color) throws IOException { if (m_Terminal.supportsSGR()) { m_TelnetIO.write(m_Terminal.getGRSequence(FCOLOR, color)); if (m_Autoflush) { flush(); } } }//setForegroundColor
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -