public class SentinelInputStream
extends java.io.FilterInputStream
The primary use for this class is as a wrapper in reading from a serial port input stream.
The idea is similar to using a BufferedReader to read a String (BufferedReader.readLine()), but this one does not block. The BufferedReader blocks so long as the underyling reader returns zero bytes read, which for the javax.comm.CommPort InputStream is everytime a ReceiveTimeout occurs, so that's not very handy.
Example:
SentinelInputStream sis = new SentinelInputStream (port.getInputStream ()); sis.setSentinel ((byte) '\n'); byte[] buffer = new byte[1024]; int bytes = sis.read (buffer); if (bytes != -1) { // Do something with newline terminated data. }
BufferedReader.readLine(boolean)
Modifier and Type | Field and Description |
---|---|
protected byte[] |
buffer
A buffer to construct Strings in.
|
protected boolean |
dumpData
A flag to indicate if, after a read, the stream should produce a hex dump
of data to system.out.
|
protected int |
sentinelByte
The byte which indicates the end of the read.
|
Constructor and Description |
---|
SentinelInputStream(java.io.InputStream in)
Create a new SentinalInputStream which performs reads from the underlying
InputStream in.
|
Modifier and Type | Method and Description |
---|---|
void |
clearSentinel()
Clear the sentinel byte.
|
boolean |
getDumpData()
Get the current data dump status.
|
void |
matchString(java.lang.String matchString)
Receive data up to the length of the given String, if it does not match
throw an IOException with the actual data received in the message.
|
int |
read()
Reads the next byte of data from this input stream.
|
int |
read(byte[] b,
int off,
int len)
Read a buffer full of data.
|
java.lang.String |
readString(int sentinel,
int maxChars)
Read a String from the underlying stream.
|
void |
setDumpData(boolean dumpData)
Turn data dumping on or off.
|
void |
setSentinel(byte b)
Set the sentinel byte.
|
protected int sentinelByte
protected byte[] buffer
protected boolean dumpData
public SentinelInputStream(java.io.InputStream in)
in
- The underlying InputStream.public void setSentinel(byte b)
b
- The byte which should trigger a return from a blocking read().public void clearSentinel()
public void setDumpData(boolean dumpData)
Data dumping displays a hex dump to System.out as data is received.
dumpData
- public boolean getDumpData()
Data dumping displays a hex dump to System.out as data is received.
public int read() throws java.io.IOException
int
in the range 0
to
255
. If no byte is available because the end of the stream
has been reached, the value -1
is returned. This method
blocks until input data is available, the end of the stream is detected,
or an exception is thrown.
This method simply performs in.read()
and returns the
result.
read
in class java.io.FilterInputStream
-1
if the end of the
stream is reached.java.io.IOException
- if an I/O error occurs.FilterInputStream.in
public int read(byte[] b, int off, int len) throws java.io.IOException
If no sentinal is set, simply calls the underyling stream's read (byte[], int, int) method.
read
in class java.io.FilterInputStream
b
- The byte[] to read data into.off
- The starting position in b to read data to.len
- The maximum number of bytes to read.java.io.IOException
- If the underyling stream throws IOException.InputStream
,
FilterInputStream
public java.lang.String readString(int sentinel, int maxChars) throws java.io.IOException
sentinel
- The byte that marks the end of the String.maxChars
- The maximum expected number of chars.java.io.IOException
- If an thrown by the underlying stream.public void matchString(java.lang.String matchString) throws java.io.IOException
matchString
- The string that should be received.java.io.IOException
- If an error occurs on the underlying stream.Copyright ? 2002 Clarity Systems Group, LLC. All Rights Reserved.