org.sadun.util
Class TelnetInputStream

java.lang.Object
  extended byjava.io.InputStream
      extended byjava.io.FilterInputStream
          extended byorg.sadun.util.TelnetInputStream
Direct Known Subclasses:
TelnetNVTChannel

public class TelnetInputStream
extends java.io.FilterInputStream

A stream to handle telnet-based communication.

The stream recognizes the standard TELNET command sequences (RFC854) in an input stream and allows for either batch or immediate handling.

Batch handling can be realized by retrieving the list of commands when the input is blocked, by using getCommands().

Immediate handling can be realized by registering a TelnetInputStream.TelnetCommandListener and responding to the command (to the appropriate output stream) with the result, and invoking #commandHandled(TelnetCommand) after the command has been properly handled.

TelnetInputStream.BasicNVTListener is a listener which automatically denies any option request, limiting the interaction capabilities to the Telnet NVT.

See also the class TelnetInputStreamConsumer for a wrapper which provides efficient consumption capabilities for machine-processing TELNET conversations.

Version:
0.2
Author:
Cristiano Sadun

Nested Class Summary
static class TelnetInputStream.BasicNVTListener
          A standard TelnetInputStream.TelnetCommandListenerthat refuses any option and suggestions, limiting the functionality to the basic Telnet NVT (network virtual terminal) functionality.
 class TelnetInputStream.TelnetCommand
          A Telnet command, associated to a specific TelnetInputStream.
static interface TelnetInputStream.TelnetCommandListener
          A listener to Telnet commands.
 
Field Summary
static int AO
          The Telnet command code AO (245)
static int AYT
          The Telnet command code IAC (246)
static int BREAK
          The Telnet command code BREAK (243)
static int DM
          The Telnet command code DM (242)
static int DO
          The Telnet command code DO (253)
static int DONT
          The Telnet command code DONT (254)
static int EC
          The Telnet command code EC (247)
static int EL
          The Telnet command code EL (248)
static int GA
          The Telnet command code GA (249)
static int IAC
          The Telnet command code IAC (255)
static int IP
          The Telnet command code IP (244)
static int NOP
          The Telnet command code NOP (241)
static int SB
          The Telnet command code SB (250)
static int SE
          The Telnet command code SE (240)
static int WILL
          The Telnet command code WILL (251)
static int WONT
          The Telnet command code WONT (252)
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
TelnetInputStream(java.io.InputStream in)
          Create a telnet input stream over the given input stream.
 
Method Summary
 void commandHandled(TelnetInputStream.TelnetCommand command)
          Remove a command from the list of unhandled commands.
 TelnetInputStream.TelnetCommand[] getCommands()
          Return the list of commands encountered so far, and not yet handled.
 java.io.PrintStream getDebugStream()
          A Line mode helper.
 int read()
           
 void registerTelnetCommandListener(TelnetInputStream.TelnetCommandListener tcl)
          Register a listener to Telnet commands.
 void removeTelnetCommandListener(TelnetInputStream.TelnetCommandListener tcl)
          Remove a listener to Telnet commands.
 void setDebugStream(java.io.PrintStream debugStream)
           
 
Methods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, read, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

IAC

public static final int IAC
The Telnet command code IAC (255)

See Also:
Constant Field Values

SE

public static final int SE
The Telnet command code SE (240)

See Also:
Constant Field Values

NOP

public static final int NOP
The Telnet command code NOP (241)

See Also:
Constant Field Values

DM

public static final int DM
The Telnet command code DM (242)

See Also:
Constant Field Values

BREAK

public static final int BREAK
The Telnet command code BREAK (243)

See Also:
Constant Field Values

IP

public static final int IP
The Telnet command code IP (244)

See Also:
Constant Field Values

AO

public static final int AO
The Telnet command code AO (245)

See Also:
Constant Field Values

AYT

public static final int AYT
The Telnet command code IAC (246)

See Also:
Constant Field Values

EC

public static final int EC
The Telnet command code EC (247)

See Also:
Constant Field Values

EL

public static final int EL
The Telnet command code EL (248)

See Also:
Constant Field Values

GA

public static final int GA
The Telnet command code GA (249)

See Also:
Constant Field Values

SB

public static final int SB
The Telnet command code SB (250)

See Also:
Constant Field Values

WILL

public static final int WILL
The Telnet command code WILL (251)

See Also:
Constant Field Values

WONT

public static final int WONT
The Telnet command code WONT (252)

See Also:
Constant Field Values

DO

public static final int DO
The Telnet command code DO (253)

See Also:
Constant Field Values

DONT

public static final int DONT
The Telnet command code DONT (254)

See Also:
Constant Field Values
Constructor Detail

TelnetInputStream

public TelnetInputStream(java.io.InputStream in)
Create a telnet input stream over the given input stream.

Method Detail

read

public int read()
         throws java.io.IOException
Throws:
java.io.IOException

getCommands

public TelnetInputStream.TelnetCommand[] getCommands()
Return the list of commands encountered so far, and not yet handled. (Handling is notified by invoking #commandHandled(TelnetCommand).

Returns:
the list of commands encountered so far, and not yet handled.

commandHandled

public void commandHandled(TelnetInputStream.TelnetCommand command)
Remove a command from the list of unhandled commands. This method is used to notify the stream that a Telnet command has been handled, and is typically invoked after either batch or immediate command processing.

Parameters:
command - the command that has been handled.

registerTelnetCommandListener

public void registerTelnetCommandListener(TelnetInputStream.TelnetCommandListener tcl)
Register a listener to Telnet commands.

Parameters:
tcl - the listener to register

removeTelnetCommandListener

public void removeTelnetCommandListener(TelnetInputStream.TelnetCommandListener tcl)
Remove a listener to Telnet commands.

Parameters:
tcl - the listener to remove

getDebugStream

public java.io.PrintStream getDebugStream()
A Line mode helper. Converts both CR LF and CR NUL into a new line, and strips it.

Returns:
a line, interpreted basing on CR LF and CR NUL.
Throws:
java.io.IOException - public String readLine() throws IOException { StringBuffer sb = null; int c0 = -1, c1 = -1; while ((c1 = read()) != -1) { if (sb == null) sb = new StringBuffer(); if ((c0 == '\r' && c1 == '\n') | // CRLF| (c0 == '\r' && c1 == 0)) // CR NUL break; sb.append((char) (c0 = c1)); } if (sb == null) return null; sb.deleteCharAt(sb.length() - 1); String result = sb.toString(); if (trimLinesEnabled) result = result.trim(); return result; } /** Return whether or not the line mode helper #readLine()will trim received lines.

setDebugStream

public void setDebugStream(java.io.PrintStream debugStream)