Source Code Cross Referenced for TerminalIO.java in  » Net » j2ssh » com » sshtools » daemon » terminal » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » j2ssh » com.sshtools.daemon.terminal 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         *  SSHTools - Java SSH2 API
0003:         *
0004:         *  Copyright (C) 2002-2003 Lee David Painter and Contributors.
0005:         *
0006:         *  Contributions made by:
0007:         *
0008:         *  Brett Smith
0009:         *  Richard Pernavas
0010:         *  Erwin Bolwidt
0011:         *
0012:         *  This program is free software; you can redistribute it and/or
0013:         *  modify it under the terms of the GNU General Public License
0014:         *  as published by the Free Software Foundation; either version 2
0015:         *  of the License, or (at your option) any later version.
0016:         *
0017:         *  This program is distributed in the hope that it will be useful,
0018:         *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0019:         *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0020:         *  GNU General Public License for more details.
0021:         *
0022:         *  You should have received a copy of the GNU General Public License
0023:         *  along with this program; if not, write to the Free Software
0024:         *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0025:         */
0026:        /**
0027:         * SSHTools - Java SSH API The contents of this package has been derived from
0028:         * the TelnetD library available from http://sourceforge.net/projects/telnetd
0029:         * The original license of the source code is as follows: TelnetD library
0030:         * (embeddable telnet daemon) Copyright (C) 2000 Dieter Wimberger This library
0031:         * is free software; you can either redistribute it and/or modify it under the
0032:         * terms of the GNU Lesser General Public License version 2.1,1999 as
0033:         * published by the Free Software Foundation (see copy received along with the
0034:         * library), or under the terms of the BSD-style license received along with
0035:         * this library.
0036:         */package com.sshtools.daemon.terminal;
0037:
0038:        import com.sshtools.j2ssh.io.*;
0039:        import com.sshtools.j2ssh.session.*;
0040:
0041:        import java.io.*;
0042:
0043:        /**
0044:         *
0045:         *
0046:         * @author $author$
0047:         * @version $Revision: 1.13 $
0048:         */
0049:        public class TerminalIO implements  PseudoTerminal {
0050:            // implements BasicTerminalIO {
0051:
0052:            /**  */
0053:            public static final int EOL_CRLF = 1;
0054:
0055:            /**  */
0056:            public static final int EOL_CR = 2;
0057:
0058:            /**  */
0059:            public static final int[] HOME = { 0, 0 };
0060:
0061:            /**  */
0062:            public static final int IOERROR = -1; //CTRL-D beim login
0063:
0064:            /**  */
0065:            public static final int
0066:            // Positioning 10xx
0067:            UP = 1001; //CTRL-D beim login
0068:
0069:            /**  */
0070:            public static final int DOWN = 1002; //CTRL-D beim login
0071:
0072:            /**  */
0073:            public static final int RIGHT = 1003; //CTRL-D beim login
0074:
0075:            /**  */
0076:            public static final int LEFT = 1004; //CTRL-D beim login
0077:
0078:            /**  */
0079:            public static final int STORECURSOR = 1051; //CTRL-D beim login
0080:
0081:            /**  */
0082:            public static final int RESTORECURSOR = 1052; //CTRL-D beim login
0083:
0084:            /**  */
0085:            public static final int
0086:            // Erasing 11xx
0087:            EEOL = 1100; //CTRL-D beim login
0088:
0089:            /**  */
0090:            public static final int EBOL = 1101; //CTRL-D beim login
0091:
0092:            /**  */
0093:            public static final int EEL = 1103; //CTRL-D beim login
0094:
0095:            /**  */
0096:            public static final int EEOS = 1104; //CTRL-D beim login
0097:
0098:            /**  */
0099:            public static final int EBOS = 1105; //CTRL-D beim login
0100:
0101:            /**  */
0102:            public static final int EES = 1106; //CTRL-D beim login
0103:
0104:            /**  */
0105:            public static final int
0106:            // Escape Sequence-ing 12xx
0107:            ESCAPE = 1200; //CTRL-D beim login
0108:
0109:            /**  */
0110:            public static final int BYTEMISSING = 1201; //CTRL-D beim login
0111:
0112:            /**  */
0113:            public static final int UNRECOGNIZED = 1202; //CTRL-D beim login
0114:
0115:            /**  */
0116:            public static final int
0117:            // Control Characters 13xx
0118:            ENTER = 10; //CTRL-D beim login
0119:
0120:            /**  */
0121:            public static final int
0122:            //ENTER = 1300, //LF is ENTER at the moment
0123:            TABULATOR = 1301; //CTRL-D beim login
0124:
0125:            /**  */
0126:            public static final int DELETE = 1302; //CTRL-D beim login
0127:
0128:            /**  */
0129:            public static final int BACKSPACE = 1303; //CTRL-D beim login
0130:
0131:            /**  */
0132:            public static final int COLORINIT = 1304; //CTRL-D beim login
0133:
0134:            /**  */
0135:            public static final int HANDLED = 1305; //CTRL-D beim login
0136:
0137:            /**  */
0138:            public static final int LOGOUTREQUEST = 1306; //CTRL-D beim login
0139:
0140:            /**  */
0141:            public static final int LineUpdate = 475;
0142:
0143:            /**  */
0144:            public static final int CharacterUpdate = 476;
0145:
0146:            /**  */
0147:            public static final int ScreenpartUpdate = 477;
0148:
0149:            /**  */
0150:            public static final int EditBuffer = 575;
0151:
0152:            /**  */
0153:            public static final int LineEditBuffer = 576;
0154:
0155:            /**  */
0156:            public static final int BEL = 7;
0157:
0158:            /**  */
0159:            public static final int BS = 8;
0160:
0161:            /**  */
0162:            public static final int DEL = 127;
0163:
0164:            /**  */
0165:            public static final int CR = 13;
0166:
0167:            /**  */
0168:            public static final int LF = 10;
0169:
0170:            /**  */
0171:            public static final int FCOLOR = 10001;
0172:
0173:            /**  */
0174:            public static final int BCOLOR = 10002;
0175:
0176:            /**  */
0177:            public static final int STYLE = 10003;
0178:
0179:            /**  */
0180:            public static final int RESET = 10004;
0181:
0182:            /**  */
0183:            public static final int BOLD = 1;
0184:
0185:            /**  */
0186:            public static final int BOLD_OFF = 22;
0187:
0188:            /**  */
0189:            public static final int ITALIC = 3;
0190:
0191:            /**  */
0192:            public static final int ITALIC_OFF = 23;
0193:
0194:            /**  */
0195:            public static final int BLINK = 5;
0196:
0197:            /**  */
0198:            public static final int BLINK_OFF = 25;
0199:
0200:            /**  */
0201:            public static final int UNDERLINED = 4;
0202:
0203:            /**  */
0204:            public static final int UNDERLINED_OFF = 24;
0205:
0206:            //Constants
0207:
0208:            /**  */
0209:            public static final int BLACK = 30;
0210:
0211:            /**  */
0212:            public static final int RED = 31;
0213:
0214:            /**  */
0215:            public static final int GREEN = 32;
0216:
0217:            /**  */
0218:            public static final int YELLOW = 33;
0219:
0220:            /**  */
0221:            public static final int BLUE = 34;
0222:
0223:            /**  */
0224:            public static final int MAGENTA = 35;
0225:
0226:            /**  */
0227:            public static final int CYAN = 36;
0228:
0229:            /**  */
0230:            public static final int white = 37;
0231:
0232:            /**  */
0233:            public static final String CRLF = "\r\n";
0234:            private Terminal terminal;
0235:            private DataInputStream in;
0236:            private DataOutputStream out;
0237:            private boolean closing;
0238:            private boolean cr;
0239:            private boolean nl;
0240:            private boolean acousticSignalling; //flag for accoustic signalling
0241:            private boolean autoflush; //flag for autoflushing mode
0242:            private int eol = EOL_CRLF;
0243:            private int lastByte;
0244:            private boolean uselast = false;
0245:            private Colorizer color = Colorizer.getReference();
0246:            private String term;
0247:            private int cols;
0248:            private int rows;
0249:            private PipedInputStream masterIn;
0250:            private PipedOutputStream masterOut;
0251:            private InputStream slaveIn;
0252:            private OutputStream slaveOut;
0253:            private IOStreamConnector ios;
0254:
0255:            //private OutputStream masterOut;
0256:            // private OutputStream slaveOut = new SlaveOutputStream();
0257:            public TerminalIO(InputStream in, OutputStream out, String term,
0258:                    int cols, int rows) throws IOException {
0259:                attachStreams(in, out);
0260:                this .term = term;
0261:                this .rows = rows;
0262:                this .cols = cols;
0263:                acousticSignalling = true;
0264:                masterOut = new PipedOutputStream();
0265:                masterIn = new PipedInputStream(masterOut);
0266:                autoflush = true;
0267:                closing = false;
0268:                cr = false;
0269:
0270:                //set default terminal
0271:                setDefaultTerminal();
0272:            }
0273:
0274:            /**
0275:             *
0276:             *
0277:             * @return
0278:             */
0279:            public InputStream getMasterInputStream() {
0280:                return masterIn;
0281:            }
0282:
0283:            /**
0284:             *
0285:             *
0286:             * @param slaveIn
0287:             */
0288:            public void bindSlaveInputStream(InputStream slaveIn) {
0289:                this .slaveIn = slaveIn;
0290:                this .ios = new IOStreamConnector(slaveIn, masterOut);
0291:            }
0292:
0293:            /**
0294:             *
0295:             *
0296:             * @param slaveOut
0297:             */
0298:            public void bindSlaveOutputStream(OutputStream slaveOut) {
0299:                this .slaveOut = slaveOut;
0300:            }
0301:
0302:            /**
0303:             *
0304:             *
0305:             * @return
0306:             */
0307:            public OutputStream getSlaveOutputStream() {
0308:                return slaveOut;
0309:            }
0310:
0311:            /**
0312:             *
0313:             *
0314:             * @return
0315:             */
0316:            public int getWidth() {
0317:                return 0;
0318:            }
0319:
0320:            /**
0321:             *
0322:             *
0323:             * @return
0324:             */
0325:            public int getHeight() {
0326:                return 0;
0327:            }
0328:
0329:            /**
0330:             *
0331:             *
0332:             * @return
0333:             */
0334:            public String getTerm() {
0335:                return terminal.getName();
0336:            }
0337:
0338:            /**
0339:             *
0340:             *
0341:             * @return
0342:             */
0343:            public String getEncodedTerminalModes() {
0344:                return "";
0345:            }
0346:
0347:            /* public void setMasterOutputStream(OutputStream masterOut) {
0348:             this.masterOut = masterOut;
0349:            }*/
0350:            public InputStream getAttachedInputStream() throws IOException {
0351:                if (in == null) {
0352:                    throw new IOException(
0353:                            "The teminal is not attached to an InputStream");
0354:                }
0355:
0356:                return in;
0357:            }
0358:
0359:            /**
0360:             *
0361:             *
0362:             * @return
0363:             *
0364:             * @throws IOException
0365:             */
0366:            public OutputStream getAttachedOutputStream() throws IOException {
0367:                if (out == null) {
0368:                    throw new IOException(
0369:                            "The terminal is not attached to an OutputStream");
0370:                }
0371:
0372:                return out;
0373:            }
0374:
0375:            /**
0376:             *
0377:             */
0378:            public void detachStreams() {
0379:                this .in = null;
0380:                this .out = null;
0381:            }
0382:
0383:            /**
0384:             *
0385:             *
0386:             * @return
0387:             */
0388:            public int getEOL() {
0389:                return eol;
0390:            }
0391:
0392:            /**
0393:             *
0394:             *
0395:             * @return
0396:             */
0397:            public String getEOLString() {
0398:                return ((eol == EOL_CR) ? "\r" : "\r\n");
0399:            }
0400:
0401:            /**
0402:             *
0403:             *
0404:             * @param eol
0405:             */
0406:            public void setEOL(int eol) {
0407:                this .eol = eol;
0408:            }
0409:
0410:            /**
0411:             *
0412:             *
0413:             * @param in
0414:             * @param out
0415:             */
0416:            public void attachStreams(InputStream in, OutputStream out) {
0417:                this .in = new DataInputStream(new BufferedInputStream(in));
0418:                this .out = new DataOutputStream(new BufferedOutputStream(out));
0419:            }
0420:
0421:            /**
0422:             *
0423:             *
0424:             * @return
0425:             *
0426:             * @throws IOException
0427:             */
0428:            public int read() throws IOException {
0429:                int i = stripCRSeq(rawread());
0430:
0431:                //translate possible control sequences
0432:                i = terminal.translateControlCharacter(i);
0433:
0434:                if ((i > 256) && (i == ESCAPE)) {
0435:                    i = handleEscapeSequence(i);
0436:                }
0437:
0438:                return i;
0439:            }
0440:
0441:            /**
0442:             *
0443:             *
0444:             * @param ch
0445:             *
0446:             * @throws IOException
0447:             */
0448:            public void write(char ch) throws IOException {
0449:                write((byte) ch);
0450:
0451:                if (autoflush) {
0452:                    flush();
0453:                }
0454:            }
0455:
0456:            /**
0457:             *
0458:             *
0459:             * @param str
0460:             *
0461:             * @throws IOException
0462:             */
0463:            public void write(String str) throws IOException {
0464:                write((color.colorize(str, terminal.supportsSGR())).getBytes());
0465:
0466:                if (autoflush) {
0467:                    flush();
0468:                }
0469:            }
0470:
0471:            /**
0472:             *
0473:             *
0474:             * @param str
0475:             *
0476:             * @throws IOException
0477:             */
0478:            public void println(String str) throws IOException {
0479:                write(str);
0480:                write(getEOLString().getBytes());
0481:
0482:                if (autoflush) {
0483:                    flush();
0484:                }
0485:            }
0486:
0487:            /**
0488:             *
0489:             *
0490:             * @throws IOException
0491:             */
0492:            public void println() throws IOException {
0493:                write(getEOLString().getBytes());
0494:
0495:                if (autoflush) {
0496:                    flush();
0497:                }
0498:            }
0499:
0500:            /**
0501:             *
0502:             *
0503:             * @throws IOException
0504:             */
0505:            public void eraseToEndOfLine() throws IOException {
0506:                doErase(EEOL);
0507:            }
0508:
0509:            /**
0510:             *
0511:             *
0512:             * @throws IOException
0513:             */
0514:            public void eraseToBeginOfLine() throws IOException {
0515:                doErase(EBOL);
0516:            }
0517:
0518:            /**
0519:             *
0520:             *
0521:             * @throws IOException
0522:             */
0523:            public void eraseLine() throws IOException {
0524:                doErase(EEL);
0525:            }
0526:
0527:            /**
0528:             *
0529:             *
0530:             * @throws IOException
0531:             */
0532:            public void eraseToEndOfScreen() throws IOException {
0533:                doErase(EEOS);
0534:            }
0535:
0536:            /**
0537:             *
0538:             *
0539:             * @throws IOException
0540:             */
0541:            public void eraseToBeginOfScreen() throws IOException {
0542:                doErase(EBOS);
0543:            }
0544:
0545:            /**
0546:             *
0547:             *
0548:             * @throws IOException
0549:             */
0550:            public void eraseScreen() throws IOException {
0551:                doErase(EES);
0552:            }
0553:
0554:            private void doErase(int funcConst) throws IOException {
0555:                write(terminal.getEraseSequence(funcConst));
0556:
0557:                if (autoflush) {
0558:                    flush();
0559:                }
0560:            }
0561:
0562:            /**
0563:             *
0564:             *
0565:             * @param direction
0566:             * @param times
0567:             *
0568:             * @throws IOException
0569:             */
0570:            public void moveCursor(int direction, int times) throws IOException {
0571:                write(terminal.getCursorMoveSequence(direction, times));
0572:
0573:                if (autoflush) {
0574:                    flush();
0575:                }
0576:            }
0577:
0578:            /**
0579:             *
0580:             *
0581:             * @param times
0582:             *
0583:             * @throws IOException
0584:             */
0585:            public void moveLeft(int times) throws IOException {
0586:                moveCursor(LEFT, times);
0587:            }
0588:
0589:            /**
0590:             *
0591:             *
0592:             * @param times
0593:             *
0594:             * @throws IOException
0595:             */
0596:            public void moveRight(int times) throws IOException {
0597:                moveCursor(RIGHT, times);
0598:            }
0599:
0600:            /**
0601:             *
0602:             *
0603:             * @param times
0604:             *
0605:             * @throws IOException
0606:             */
0607:            public void moveUp(int times) throws IOException {
0608:                moveCursor(UP, times);
0609:            }
0610:
0611:            /**
0612:             *
0613:             *
0614:             * @param times
0615:             *
0616:             * @throws IOException
0617:             */
0618:            public void moveDown(int times) throws IOException {
0619:                moveCursor(DOWN, times);
0620:            }
0621:
0622:            /**
0623:             *
0624:             *
0625:             * @param row
0626:             * @param col
0627:             *
0628:             * @throws IOException
0629:             */
0630:            public void setCursor(int row, int col) throws IOException {
0631:                int[] pos = new int[2];
0632:                pos[0] = row;
0633:                pos[1] = col;
0634:                write(terminal.getCursorPositioningSequence(pos));
0635:
0636:                if (autoflush) {
0637:                    flush();
0638:                }
0639:            }
0640:
0641:            /**
0642:             *
0643:             *
0644:             * @throws IOException
0645:             */
0646:            public void homeCursor() throws IOException {
0647:                write(terminal.getCursorPositioningSequence(HOME));
0648:
0649:                if (autoflush) {
0650:                    flush();
0651:                }
0652:            }
0653:
0654:            /**
0655:             *
0656:             *
0657:             * @throws IOException
0658:             */
0659:            public void storeCursor() throws IOException {
0660:                write(terminal.getSpecialSequence(STORECURSOR));
0661:            }
0662:
0663:            /**
0664:             *
0665:             *
0666:             * @throws IOException
0667:             */
0668:            public void restoreCursor() throws IOException {
0669:                write(terminal.getSpecialSequence(RESTORECURSOR));
0670:            }
0671:
0672:            /**
0673:             *
0674:             *
0675:             * @throws IOException
0676:             */
0677:            public void closeInput() throws IOException {
0678:                if (in == null) {
0679:                    throw new IOException(
0680:                            "The terminal is not attached to an inputstream");
0681:                }
0682:
0683:                in.close();
0684:            }
0685:
0686:            private int read16int() throws IOException {
0687:                if (in == null) {
0688:                    throw new IOException(
0689:                            "The terminal is not attached to an inputstream");
0690:                }
0691:
0692:                return in.readUnsignedShort();
0693:            }
0694:
0695:            private int rawread() throws IOException {
0696:                if (in == null) {
0697:                    throw new IOException(
0698:                            "The terminal is not attached to an inputstream");
0699:                }
0700:
0701:                return in.readUnsignedByte();
0702:            }
0703:
0704:            private int stripCRSeq(int input) throws IOException {
0705:                if (in == null) {
0706:                    throw new IOException(
0707:                            "The terminal is not attached to an inputstream");
0708:                }
0709:
0710:                // Check for CR
0711:                if (input == CR) {
0712:                    // Mark the current position and test for LF
0713:                    in.mark(1);
0714:
0715:                    // If there is more data to read, check for LF
0716:                    if (in.available() > 0) {
0717:                        int next = in.readUnsignedByte();
0718:
0719:                        // If we don't have LF then reset back to the mark position
0720:                        if (next != LF) {
0721:                            in.reset();
0722:                        }
0723:                    }
0724:
0725:                    return TerminalIO.ENTER;
0726:                }
0727:
0728:                return input;
0729:            }
0730:
0731:            /**
0732:             *
0733:             *
0734:             * @param b
0735:             *
0736:             * @throws IOException
0737:             */
0738:            public void write(byte b) throws IOException {
0739:                if (out == null) {
0740:                    throw new IOException(
0741:                            "The terminal is not attached to an outputstream");
0742:                }
0743:
0744:                if (eol == EOL_CRLF) {
0745:                    if (!cr && (b == 10)) {
0746:                        out.write(13);
0747:
0748:                        //if(masterOut!=null)
0749:                        //  masterOut.write(13);
0750:                    }
0751:
0752:                    //ensure CRLF(\r\n) is written for CR(\r) to adhere
0753:                    //to the telnet protocol.
0754:                    if (cr && (b != 10)) {
0755:                        out.write(10);
0756:
0757:                        // if(masterOut!=null)
0758:                        //   masterOut.write(10);
0759:                    }
0760:
0761:                    out.write(b);
0762:
0763:                    // if(masterOut!=null)
0764:                    //   masterOut.write(b);
0765:                    if (b == 13) {
0766:                        cr = true;
0767:                    } else {
0768:                        cr = false;
0769:                    }
0770:                } else {
0771:                    out.write(b);
0772:
0773:                    // if(masterOut!=null)
0774:                    //   masterOut.write(b);
0775:                }
0776:            }
0777:
0778:            /**
0779:             *
0780:             *
0781:             * @param i
0782:             *
0783:             * @throws IOException
0784:             */
0785:            public void write(int i) throws IOException {
0786:                write((byte) i);
0787:            }
0788:
0789:            /**
0790:             *
0791:             *
0792:             * @param sequence
0793:             *
0794:             * @throws IOException
0795:             */
0796:            public void write(byte[] sequence) throws IOException {
0797:                for (int z = 0; z < sequence.length; z++) {
0798:                    write(sequence[z]);
0799:                }
0800:            }
0801:
0802:            /**
0803:             *
0804:             *
0805:             * @param sequence
0806:             *
0807:             * @throws IOException
0808:             */
0809:            public void write(int[] sequence) throws IOException {
0810:                for (int j = 0; j < sequence.length; j++) {
0811:                    write((byte) sequence[j]);
0812:                }
0813:            }
0814:
0815:            /**
0816:             *
0817:             *
0818:             * @throws IOException
0819:             */
0820:            public void flush() throws IOException {
0821:                if (out == null) {
0822:                    throw new IOException(
0823:                            "The terminal is not attached to an outputstream");
0824:                }
0825:
0826:                // If were attached then flush, else ignore
0827:                out.flush();
0828:            }
0829:
0830:            /**
0831:             *
0832:             *
0833:             * @throws IOException
0834:             */
0835:            public void closeOutput() throws IOException {
0836:                if (out == null) {
0837:                    throw new IOException(
0838:                            "The terminal is not attached to an outputstream");
0839:                }
0840:
0841:                closing = true;
0842:                out.close();
0843:            }
0844:
0845:            /**
0846:             *
0847:             *
0848:             * @param bool
0849:             */
0850:            public void setSignalling(boolean bool) {
0851:                acousticSignalling = bool;
0852:            }
0853:
0854:            /**
0855:             *
0856:             *
0857:             * @return
0858:             */
0859:            public boolean isSignalling() {
0860:                return acousticSignalling;
0861:            }
0862:
0863:            /**
0864:             *
0865:             *
0866:             * @throws IOException
0867:             */
0868:            public void bell() throws IOException {
0869:                if (acousticSignalling) {
0870:                    write(BEL);
0871:                }
0872:
0873:                if (autoflush) {
0874:                    flush();
0875:                }
0876:            }
0877:
0878:            /**
0879:             *
0880:             *
0881:             * @param topmargin
0882:             * @param bottommargin
0883:             *
0884:             * @return
0885:             *
0886:             * @throws IOException
0887:             */
0888:            public boolean defineScrollRegion(int topmargin, int bottommargin)
0889:                    throws IOException {
0890:                if (terminal.supportsScrolling()) {
0891:                    write(terminal.getScrollMarginsSequence(topmargin,
0892:                            bottommargin));
0893:                    flush();
0894:
0895:                    return true;
0896:                } else {
0897:                    return false;
0898:                }
0899:            }
0900:
0901:            /**
0902:             *
0903:             *
0904:             * @param color
0905:             *
0906:             * @throws IOException
0907:             */
0908:            public void setForegroundColor(int color) throws IOException {
0909:                if (terminal.supportsSGR()) {
0910:                    write(terminal.getGRSequence(FCOLOR, color));
0911:
0912:                    if (autoflush) {
0913:                        flush();
0914:                    }
0915:                }
0916:            }
0917:
0918:            /**
0919:             *
0920:             *
0921:             * @param color
0922:             *
0923:             * @throws IOException
0924:             */
0925:            public void setBackgroundColor(int color) throws IOException {
0926:                if (terminal.supportsSGR()) {
0927:                    //this method adds the offset to the fg color by itself
0928:                    write(terminal.getGRSequence(BCOLOR, color + 10));
0929:
0930:                    if (autoflush) {
0931:                        flush();
0932:                    }
0933:                }
0934:            }
0935:
0936:            /**
0937:             *
0938:             *
0939:             * @param b
0940:             *
0941:             * @throws IOException
0942:             */
0943:            public void setBold(boolean b) throws IOException {
0944:                if (terminal.supportsSGR()) {
0945:                    if (b) {
0946:                        write(terminal.getGRSequence(STYLE, BOLD));
0947:                    } else {
0948:                        write(terminal.getGRSequence(STYLE, BOLD_OFF));
0949:                    }
0950:
0951:                    if (autoflush) {
0952:                        flush();
0953:                    }
0954:                }
0955:            }
0956:
0957:            /**
0958:             *
0959:             *
0960:             * @param b
0961:             *
0962:             * @throws IOException
0963:             */
0964:            public void setUnderlined(boolean b) throws IOException {
0965:                if (terminal.supportsSGR()) {
0966:                    if (b) {
0967:                        write(terminal.getGRSequence(STYLE, UNDERLINED));
0968:                    } else {
0969:                        write(terminal.getGRSequence(STYLE, UNDERLINED_OFF));
0970:                    }
0971:
0972:                    if (autoflush) {
0973:                        flush();
0974:                    }
0975:                }
0976:            }
0977:
0978:            /**
0979:             *
0980:             *
0981:             * @param b
0982:             *
0983:             * @throws IOException
0984:             */
0985:            public void setItalic(boolean b) throws IOException {
0986:                if (terminal.supportsSGR()) {
0987:                    if (b) {
0988:                        write(terminal.getGRSequence(STYLE, ITALIC));
0989:                    } else {
0990:                        write(terminal.getGRSequence(STYLE, ITALIC_OFF));
0991:                    }
0992:
0993:                    if (autoflush) {
0994:                        flush();
0995:                    }
0996:                }
0997:            }
0998:
0999:            /**
1000:             *
1001:             *
1002:             * @param b
1003:             *
1004:             * @throws IOException
1005:             */
1006:            public void setBlink(boolean b) throws IOException {
1007:                if (terminal.supportsSGR()) {
1008:                    if (b) {
1009:                        write(terminal.getGRSequence(STYLE, BLINK));
1010:                    } else {
1011:                        write(terminal.getGRSequence(STYLE, BLINK_OFF));
1012:                    }
1013:
1014:                    if (autoflush) {
1015:                        flush();
1016:                    }
1017:                }
1018:            }
1019:
1020:            /**
1021:             *
1022:             *
1023:             * @throws IOException
1024:             */
1025:            public void resetAttributes() throws IOException {
1026:                if (terminal.supportsSGR()) {
1027:                    write(terminal.getGRSequence(RESET, 0));
1028:                }
1029:            }
1030:
1031:            private int handleEscapeSequence(int i) throws IOException {
1032:                if (i == ESCAPE) {
1033:                    int[] bytebuf = new int[terminal.getAtomicSequenceLength()];
1034:
1035:                    //fill atomic length
1036:                    //FIXME: ensure CAN, broken Escapes etc.
1037:                    for (int m = 0; m < bytebuf.length; m++) {
1038:                        bytebuf[m] = read();
1039:                    }
1040:
1041:                    return terminal.translateEscapeSequence(bytebuf);
1042:                }
1043:
1044:                if (i == BYTEMISSING) {
1045:                    //FIXME:longer escapes etc...
1046:                }
1047:
1048:                return HANDLED;
1049:            }
1050:
1051:            /**
1052:             *
1053:             *
1054:             * @return
1055:             */
1056:            public boolean isAutoflushing() {
1057:                return autoflush;
1058:            }
1059:
1060:            /**
1061:             *
1062:             *
1063:             * @param b
1064:             */
1065:            public void setAutoflushing(boolean b) {
1066:                autoflush = b;
1067:            }
1068:
1069:            /**
1070:             *
1071:             *
1072:             * @throws IOException
1073:             */
1074:            public void close() throws IOException {
1075:                closeOutput();
1076:            }
1077:
1078:            /**
1079:             *
1080:             *
1081:             * @return
1082:             */
1083:            public Terminal getTerminal() {
1084:                return terminal;
1085:            }
1086:
1087:            //getTerminal
1088:            public void setDefaultTerminal() throws IOException {
1089:                //set the terminal passing the negotiated string
1090:                setTerminal(term);
1091:            }
1092:
1093:            /**
1094:             *
1095:             *
1096:             * @param terminalName
1097:             *
1098:             * @throws IOException
1099:             */
1100:            public void setTerminal(String terminalName) throws IOException {
1101:                terminal = TerminalFactory.newInstance(terminalName);
1102:
1103:                //Terminal is set we init it....
1104:                initTerminal();
1105:            }
1106:
1107:            private void initTerminal() throws IOException {
1108:                write(terminal.getInitSequence());
1109:                flush();
1110:            }
1111:
1112:            /**
1113:             *
1114:             *
1115:             * @return
1116:             */
1117:            public int getRows() {
1118:                return rows;
1119:            }
1120:
1121:            /**
1122:             *
1123:             *
1124:             * @return
1125:             */
1126:            public int getColumns() {
1127:                return cols;
1128:            }
1129:        }
1130:
1131:        //class TerminalIO
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.