|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.io.InputStream jhomenet.server.console.io.SystemInputStream
public class SystemInputStream
This is an implementation of an InputStream
for use as System.in
.
Since the underlying root InputStream
for a TINI program can come from
a serial port, a socket, or any other kind of input, the differing behaviors of these
input devices must be reconciled by this class. This class must ensure that input
becomes available only after an end of line sequence is received, and that the amount
available does not represent any 'inaccessible' data (if the data has not been followed
by an end of line character). It must also handle '\r', '\n', and '\r\n' as end of
line sequences.
Field Summary | |
---|---|
protected static int |
ASCII_BS
Backspace and delete defines |
protected static int |
ASCII_DEL
|
protected static int |
ASCII_SPACE
|
protected byte[] |
buffer
The internal buffer that holds all the data that has been received and not yet read by an application. |
protected boolean |
ECHO
Indicates whether or not to echo the character just read. |
protected int |
endPos
The ending position in the internal buffer where valid data ends. |
protected static byte[] |
ERASE
Byte sequence for a backspace. |
java.lang.String |
fileInName
If this SystemInputStream reads from a file,
this represents the name of the file. |
protected java.io.InputStream |
inputStream
The underlying InputStream that this
SystemPrintStream reads from. |
protected java.io.PrintStream |
printStream
The 'echo' stream. |
protected boolean |
rawMode
Indicates that the SystemInputStream should return
data as soon as it is available, and not wait until an
end of line sequence is received. |
protected Session |
session
The user session that owns this SystemInputStream . |
protected int |
startPos
The starting position in the internal buffer where valid data is located. |
Constructor Summary | |
---|---|
SystemInputStream(java.io.InputStream in,
java.io.PrintStream out)
Creates a new SystemPrintStream with the
specified underlying root stream and echo stream. |
|
SystemInputStream(java.io.InputStream in,
java.io.PrintStream out,
java.lang.String fileInName)
Creates a new SystemInputStream with the
specified underlying root stream and echo stream. |
Method Summary | |
---|---|
int |
available()
Returns the number of bytes that can be read from this input stream without blocking. |
boolean |
errorOccurred()
|
void |
executeCommand(java.lang.String commandStr)
|
boolean |
getEcho()
Returns state of read character echo. |
java.io.InputStream |
getRootStream()
Returns the underlying root InputStream of this stream. |
protected void |
increaseBuffer(int newDataSize)
Ensures the internal buffer is large enough for more data. |
protected int |
rawAvailable()
This method should be overridden by subclassing InputStream s. |
protected int |
rawRead()
This method should be overridden by subclassing InputStream s. |
protected int |
rawRead(byte[] buff,
int off,
int len)
Reads from the underlying stream and returns data even if an end of line sequence is not received. |
int |
read()
Reads the next character from the stream. |
int |
read(byte[] buff,
int off,
int len)
Tries to read len bytes from the stream. |
java.lang.String |
readLine()
Reads a line of text up to but not including the end of line sequence. |
void |
setEcho(boolean echo)
Turns on and off echoing back characters read by this stream. |
void |
setEchoStream(java.io.PrintStream echo)
Sets the PrintStream to echo back characters read by this stream. |
void |
setRawMode(boolean rawMode)
Sets the mode for reading data from the underlying stream. |
void |
setRootStream(java.io.InputStream newIn)
Sets the underlying InputStream to use. |
void |
setSession(Session session)
Informs this stream of its owning session. |
Methods inherited from class java.io.InputStream |
---|
close, mark, markSupported, read, reset, skip |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected Session session
SystemInputStream
.
protected java.io.InputStream inputStream
InputStream
that this
SystemPrintStream
reads from.
protected java.io.PrintStream printStream
ECHO
is true
,
this PrintStream
will write everything it reads. This is useful
in a telnet session, for instance, when a user types something and
it needs to be echoed back to the telnet client.
ECHO
protected boolean rawMode
SystemInputStream
should return
data as soon as it is available, and not wait until an
end of line sequence is received.
rawRead()
,
rawRead(byte[],int,int)
protected byte[] buffer
protected int startPos
protected int endPos
public java.lang.String fileInName
SystemInputStream
reads from a file,
this represents the name of the file.
protected boolean ECHO
printStream
protected static final int ASCII_BS
protected static final int ASCII_DEL
protected static final int ASCII_SPACE
protected static final byte[] ERASE
Constructor Detail |
---|
public SystemInputStream(java.io.InputStream in, java.io.PrintStream out)
SystemPrintStream
with the
specified underlying root stream and echo stream.
in
- underlying InputStream to useout
- stream for echo characterspublic SystemInputStream(java.io.InputStream in, java.io.PrintStream out, java.lang.String fileInName)
SystemInputStream
with the
specified underlying root stream and echo stream. Calling
this constructor signifies that the underlying
InputStream
is reading from a file.
in
- underlying InputStream to useout
- stream for echo charactersfileInName
- name of the file this stream is reading fromMethod Detail |
---|
public void setEcho(boolean echo)
echo
- true
to enable echoing, false
to disable itsetEchoStream(java.io.PrintStream)
public boolean getEcho()
setEchoStream(java.io.PrintStream)
,
setEcho(boolean)
public void setEchoStream(java.io.PrintStream echo)
PrintStream
to echo back characters read by this stream.
Note: you must call setEcho(true)
to enable echoing.
echo
- stream to echo back characters readsetEcho(boolean)
public void setSession(Session session)
session
- the owning sessionpublic void executeCommand(java.lang.String commandStr)
commandStr
- protected int rawRead() throws java.io.IOException
InputStream
s.
It should only be called from within a synchronized
block. It blocks until
data is available and returns the first value it reads. It does not wait
for an end of line sequence.
java.io.IOException
- if an error occurs reading from the underlying stream.rawRead(byte[],int,int)
,
rawAvailable()
,
setRawMode(boolean)
protected int rawRead(byte[] buff, int off, int len) throws java.io.IOException
Reads from the underlying stream and returns data even if an end of line sequence is not received. If not operating in raw reading mode, it also checks and handles backspace events. Stops if it finds an end of stream.
This method should only be called from within a synchronized
block.
java.io.IOException
- if an error occurs reading from the underlying streamrawRead()
,
setRawMode(boolean)
,
rawAvailable()
protected int rawAvailable() throws java.io.IOException
InputStream
s.
It returns the amount available from the underlying root stream.
This is not the amount available if the SystemInputStream
is not in raw mode.
java.io.IOException
- if an error occurs in the underlying stream.setRawMode(boolean)
,
rawRead()
,
rawRead(byte[],int,int)
public java.lang.String readLine()
String
of length 0).
InputStream
, or null
if no more input will be
available due to the end of the streamread()
,
read(byte[],int,int)
public final int read() throws java.io.IOException
read
in class java.io.InputStream
-1
if the end of the stream has been
reached
java.io.IOException
- if an error occurs in the underlying streamsetRawMode(boolean)
,
readLine()
,
read(byte[],int,int)
public int read(byte[] buff, int off, int len) throws java.io.IOException
len
bytes from the stream.
If the end of stream is encountered, no more data
will be read. If raw mode is not enabled, only
data that has been followed by an end of line
sequence or the end of the stream will be reported.
This method will block until enough data is available
to return or the end of the stream is reached.
read
in class java.io.InputStream
buff
- byte array to store the data readoff
- offset to begin storing data in the arraylen
- amount of data requested
-1
if the end of the stream has been
reached
java.io.IOException
- if an error occurs in the underlying streamread()
,
readLine()
,
setRawMode(boolean)
protected void increaseBuffer(int newDataSize)
synchronized
block.
newDataSize
- the amount of new data
that is about to be copied into the buffer
at location endPos
public final int available() throws java.io.IOException
available
in class java.io.InputStream
java.io.IOException
- if an error occurs in the underlying streamsetRawMode(boolean)
public void setRawMode(boolean rawMode)
true
, any call to read data will
return the next available data from the underlying stream.
If raw mode is false
, read calls only report data
that has been followed by an end of line character, or the
end of the stream, even if the read must block to do so.
rawMode
- false
to block for an end of line
sequence, true
to return any available datarawRead()
,
rawRead(byte[],int,int)
,
rawAvailable()
,
read()
,
read(byte[],int,int)
,
available()
public void setRootStream(java.io.InputStream newIn)
InputStream
to use.
newIn
- new root InputStream
to usegetRootStream()
public java.io.InputStream getRootStream()
InputStream
of this stream.
setRootStream(java.io.InputStream)
public boolean errorOccurred()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |