View Javadoc
1 package org.apache.commons.net.telnet; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Commons" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.io.BufferedInputStream; 58 import java.io.IOException; 59 import java.io.InputStream; 60 import java.io.OutputStream; 61 import org.apache.commons.net.io.FromNetASCIIInputStream; 62 import org.apache.commons.net.io.ToNetASCIIOutputStream; 63 64 /**** 65 * The TelnetClient class implements the simple network virtual 66 * terminal (NVT) for the Telnet protocol according to RFC 854. It 67 * does not implement any of the extra Telnet options because it 68 * is meant to be used within a Java program providing automated 69 * access to Telnet accessible resources. 70 * <p> 71 * The class can be used by first connecting to a server using the 72 * SocketClient 73 * <a href="org.apache.commons.net.SocketClient.html#connect">connect</a> 74 * method. Then an InputStream and OutputStream for sending and 75 * receiving data over the Telnet connection can be obtained by 76 * using the <a href="#getInputStream"> getInputStream() </a> and 77 * <a href="#getOutputStream"> getOutputStream() </a> methods. 78 * When you finish using the streams, you must call 79 * <a href="#disconnect"> disconnect </a> rather than simply 80 * closing the streams. 81 * <p> 82 * <p> 83 * @author Daniel F. Savarese 84 ***/ 85 86 public class TelnetClient extends Telnet 87 { 88 private InputStream __input; 89 private OutputStream __output; 90 91 /**** 92 * Default TelnetClient constructor. 93 ***/ 94 public TelnetClient() 95 { 96 __input = null; 97 __output = null; 98 } 99 100 void _flushOutputStream() throws IOException 101 { 102 _output_.flush(); 103 } 104 void _closeOutputStream() throws IOException 105 { 106 _output_.close(); 107 } 108 109 /**** 110 * Handles special connection requirements. 111 * <p> 112 * @exception IOException If an error occurs during connection setup. 113 ***/ 114 protected void _connectAction_() throws IOException 115 { 116 super._connectAction_(); 117 InputStream input; 118 TelnetInputStream tmp; 119 120 if (FromNetASCIIInputStream.isConversionRequired()) 121 input = new FromNetASCIIInputStream(_input_); 122 else 123 input = _input_; 124 125 126 tmp = new TelnetInputStream(input, this); 127 tmp._start(); 128 // __input CANNOT refer to the TelnetInputStream. We run into 129 // blocking problems when some classes use TelnetInputStream, so 130 // we wrap it with a BufferedInputStream which we know is safe. 131 // This blocking behavior requires further investigation, but right 132 // now it looks like classes like InputStreamReader are not implemented 133 // in a safe manner. 134 __input = new BufferedInputStream(tmp); 135 __output = new ToNetASCIIOutputStream(new TelnetOutputStream(this)); 136 } 137 138 /**** 139 * Disconnects the telnet session, closing the input and output streams 140 * as well as the socket. If you have references to the 141 * input and output streams of the telnet connection, you should not 142 * close them yourself, but rather call disconnect to properly close 143 * the connection. 144 ***/ 145 public void disconnect() throws IOException 146 { 147 __input.close(); 148 __output.close(); 149 super.disconnect(); 150 } 151 152 /**** 153 * Returns the telnet connection output stream. You should not close the 154 * stream when you finish with it. Rather, you should call 155 * <a href="#disconnect"> disconnect </a>. 156 * <p> 157 * @return The telnet connection output stream. 158 ***/ 159 public OutputStream getOutputStream() 160 { 161 return __output; 162 } 163 164 /**** 165 * Returns the telnet connection input stream. You should not close the 166 * stream when you finish with it. Rather, you should call 167 * <a href="#disconnect"> disconnect </a>. 168 * <p> 169 * @return The telnet connection input stream. 170 ***/ 171 public InputStream getInputStream() 172 { 173 return __input; 174 } 175 }

This page was automatically generated by Maven