Serialio
Class SnoopInputStream

java.lang.Object
  extended byjava.io.InputStream
      extended bySerialio.SnoopInputStream
All Implemented Interfaces:
java.util.EventListener, SnoopListener

public class SnoopInputStream
extends java.io.InputStream
implements SnoopListener

Create an 'snoop' input stream on the serial port that searches for data patterns. Note that timeouts are not supported in this class since caller controls readinging via SnoopInputStream.read.

  • Copyright (c) 1998-2004 Serialio.com, All Rights Reserved.


    Field Summary
    protected  java.io.InputStream in
               
    protected  byte lastByte
               
     
    Constructor Summary
    SnoopInputStream()
              This empty constructor is for Bean friendlyness
    SnoopInputStream(java.io.InputStream in)
              Create an input stream that allows 'snooping' for data patterns on incoming data
     
    Method Summary
     void addSnoop(SnoopListener lsnr, byte[] data, boolean ignoreCase)
              Add a 'Snoop' on input stream
     void addSnoop(SnoopListener lsnr, java.lang.String data, boolean ignoreCase)
              Add a 'Snoop' on input stream
     void addTokenSnoop(SnoopTokenListener stl, byte[] tokBeg, byte[] tokEnd, byte separator, int tok, int id)
              Add a 'Token Snoop' on input stream.
     void addTokenSnoop(SnoopTokenListener stl, byte[] tokBeg, byte[] tokEnd, byte separator, int tok, int id, int tokLenMax)
              Add a 'Token Snoop' on input stream.
     void addTokenSnoop(SnoopTokenListener stl, java.lang.String tokBeg, java.lang.String tokEnd, java.lang.String separator, int tok, int id)
              Add a 'Token Snoop' on input stream.
     void addTokenSnoop(SnoopTokenListener stl, java.lang.String tokBeg, java.lang.String tokEnd, java.lang.String separator, int tok, int id, int tokLenMax)
              Add a 'Token Snoop' on input stream.
     int available()
              Indicates the number of bytes in the read queue
     void checkDataForMatch(byte b)
              Check byte for match with existing snoops
     void checkForTokenMatch(int bt)
               
     void close()
              Closes this input stream
     boolean dataMatch(byte[] d1, byte[] d2)
              Check for match of two byte arrays.
     java.io.InputStream getInputStream()
              Get input stream this object is snooping on
     boolean getSnoopEnable()
              Get state of snooping on input stream
     int getSubTokenCount()
              Get number of sub-tokens in current token when snooping for Tokens.
     void mark(int rdLimit)
               
     boolean markSupported()
               
    protected  void notifySighting(Serialio.SnoopInputStream.Snoop s)
              Notify listeners
     int read()
              Reads data from the input stream.
     int read(byte[] buf)
              Reads data from the input stream
     int read(byte[] buf, int off, int len)
              Reads data from the input stream
     void removeSnoop(SnoopListener lsnr)
              Remove 'Snoop' on input stream
     void removeSnoop(SnoopListener lsnr, byte[] data)
              Remove snoop on input stream
     void removeSnoop(SnoopListener lsnr, java.lang.String data)
              Remove 'Snoop' on input stream
     void removeTokenSnoop(int id)
              Remove the Token snoop on input stream
     void reset()
               
     void setEndTokenSnoop(SnoopTokenListener stl, byte[] tokEnd, int id)
              Set a 'Token Snoop' on the input stream.
     void setInputStream(java.io.InputStream in)
              Set input stream this object will snooping on
     void setSnoopEnable(boolean onOff)
              Enable/disable snooping on input stream
     void setStreamID(int id)
              Set the stream ID
     void snoop(byte b)
              Forces the data through the snoop logic without having to read data from the input stream.
     void snoopEvent(byte[] data)
              Receives notification when searched for data is sighted or timeout
     
    Methods inherited from class java.io.InputStream
    skip
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     

    Field Detail

    in

    protected java.io.InputStream in

    lastByte

    protected byte lastByte
    Constructor Detail

    SnoopInputStream

    public SnoopInputStream()
    This empty constructor is for Bean friendlyness


    SnoopInputStream

    public SnoopInputStream(java.io.InputStream in)
    Create an input stream that allows 'snooping' for data patterns on incoming data

    Method Detail

    setStreamID

    public void setStreamID(int id)
    Set the stream ID


    snoop

    public void snoop(byte b)
               throws java.io.IOException,
                      ExTokenOverflow
    Forces the data through the snoop logic without having to read data from the input stream. This allows the snoop logic to be used on non-stream based data.

    Throws:
    java.io.IOException - if an I/O error occurs.
    java.lang.ArrayIndexOutOfBoundsException - if token buffer gets exceeded
    ExTokenOverflow

    read

    public int read()
             throws java.io.IOException,
                    ExTokenOverflow
    Reads data from the input stream.

    Returns:
    the next byte of data
    Throws:
    java.io.IOException - if an I/O error occurs.
    java.lang.ArrayIndexOutOfBoundsException - if token buffer gets exceeded
    ExTokenOverflow

    read

    public int read(byte[] buf)
             throws java.io.IOException,
                    ExTokenOverflow
    Reads data from the input stream

    Returns:
    The total number of bytes put into the buffer
    Throws:
    java.io.IOException - if an I/O error occurs.
    java.lang.ArrayIndexOutOfBoundsException - if token buffer gets exceeded
    ExTokenOverflow

    read

    public int read(byte[] buf,
                    int off,
                    int len)
             throws java.io.IOException,
                    ExTokenOverflow
    Reads data from the input stream

    Parameters:
    off - The beginning offset in the buffer where data will be put
    len - The length of data requested to be put in the buffer
    Returns:
    The total number of bytes put into the buffer
    Throws:
    java.io.IOException - if an error occurs.
    java.lang.ArrayIndexOutOfBoundsException - if token buffer gets exceeded
    ExTokenOverflow

    checkForTokenMatch

    public void checkForTokenMatch(int bt)
                            throws ExTokenOverflow
    Throws:
    ExTokenOverflow

    available

    public int available()
                  throws java.io.IOException
    Indicates the number of bytes in the read queue

    Returns:
    The number of bytes available for reading
    Throws:
    java.io.IOException - if an I/O error occurs.

    close

    public void close()
               throws java.io.IOException
    Closes this input stream

    Throws:
    java.io.IOException - if an I/O error occurs.

    setSnoopEnable

    public void setSnoopEnable(boolean onOff)
    Enable/disable snooping on input stream

    Parameters:
    onOff - The state of snooping

    getSnoopEnable

    public boolean getSnoopEnable()
    Get state of snooping on input stream

    Returns:
    state of snooping

    getInputStream

    public java.io.InputStream getInputStream()
    Get input stream this object is snooping on

    Returns:
    InputStream

    getSubTokenCount

    public int getSubTokenCount()
    Get number of sub-tokens in current token when snooping for Tokens.

    Returns:
    number of sub-tokens

    setInputStream

    public void setInputStream(java.io.InputStream in)
    Set input stream this object will snooping on

    Parameters:
    in - The InputStream

    markSupported

    public boolean markSupported()

    mark

    public void mark(int rdLimit)

    reset

    public void reset()
               throws java.io.IOException
    Throws:
    java.io.IOException

    addTokenSnoop

    public void addTokenSnoop(SnoopTokenListener stl,
                              byte[] tokBeg,
                              byte[] tokEnd,
                              byte separator,
                              int tok,
                              int id,
                              int tokLenMax)
    Add a 'Token Snoop' on input stream.
    Default for tokLenMax is 1024
    Token number: tokNum=0 returns the entire data between tokBeg & tokEnd
    Token number: tokNum>0 returns the sub-token between tokBeg & tokEnd separated by the separator.
    For example when tokBeg="$GPRMC" tokEnd="\r\n" the addTokenSnoop are requested for 0 and 2, and the data stream sees this:
    $GPRMC,155358.53,A,4332.7806,N,11316.6861,W,173.8,000.4,180102,14,E*69
    Then the SnoopTokenListener will get two events, one for tokNum=0 with data
    ,155358.53,A,4332.7806,N,11316.6861,W,173.8,000.4,180102,14,E*69
    and one for tokNum=2 with data
    155358.53
    Note in the above example, if the first token is requested the data returned will be null. There is no data between tokbeg and the first separator. If tokBeg="$GPRMC," then the first token (1) would be "155358.53".

    Parameters:
    stl - The listener for the Snoop (data pattern)
    tokBeg - The begin token to Snoop (search) for
    tokEnd - The end token to Snoop (search) for
    separator - The separator which separates the tokens
    tok - The token number to be returned
    id - The token id given to the token
    tokLenMax - The maximum length of the token

    setEndTokenSnoop

    public void setEndTokenSnoop(SnoopTokenListener stl,
                                 byte[] tokEnd,
                                 int id)
    Set a 'Token Snoop' on the input stream. Calling this method enables 'singleTokenSnoop mode', in this mode tokBeg is always true, and token event is fired each time the tokEnd is seen. This is useful if you want to have an event sent each time an ending token is seen e.g. each time a CR, LF, CRLF or whatever is seen.

    Parameters:
    stl - The listener for the Snoop (data pattern)
    tokEnd - The end token to Snoop (search) for
    id - The token id given to the token

    addTokenSnoop

    public void addTokenSnoop(SnoopTokenListener stl,
                              byte[] tokBeg,
                              byte[] tokEnd,
                              byte separator,
                              int tok,
                              int id)
    Add a 'Token Snoop' on input stream. (see addTokenSnoop above for details)

    Parameters:
    stl - The listener for the Snoop (data pattern)
    tokBeg - The begin token to Snoop (search) for
    tokEnd - The end token to Snoop (search) for
    separator - The separator which separates the tokens
    id - The token id given to the token

    addTokenSnoop

    public void addTokenSnoop(SnoopTokenListener stl,
                              java.lang.String tokBeg,
                              java.lang.String tokEnd,
                              java.lang.String separator,
                              int tok,
                              int id)
    Add a 'Token Snoop' on input stream. (see addTokenSnoop above for details)

    Parameters:
    stl - The listener for the Snoop (data pattern)
    tokBeg - The begin token to Snoop (search) for
    tokEnd - The end token to Snoop (search) for
    separator - The separator which separates the tokens
    id - The token id given to the token

    addTokenSnoop

    public void addTokenSnoop(SnoopTokenListener stl,
                              java.lang.String tokBeg,
                              java.lang.String tokEnd,
                              java.lang.String separator,
                              int tok,
                              int id,
                              int tokLenMax)
    Add a 'Token Snoop' on input stream. (see addTokenSnoop above for details)

    Parameters:
    stl - The listener for the Snoop (data pattern)
    tokBeg - The begin token to Snoop (search) for
    tokEnd - The end token to Snoop (search) for
    separator - The separator which separates the tokens. Note: Separator is ignored when the parameter tokNum=0.
    id - The token id given to the token
    tokLenMax - The maximum length of the token

    removeTokenSnoop

    public void removeTokenSnoop(int id)
    Remove the Token snoop on input stream

    Parameters:
    id - The token id to be removed

    addSnoop

    public void addSnoop(SnoopListener lsnr,
                         java.lang.String data,
                         boolean ignoreCase)
    Add a 'Snoop' on input stream

    Parameters:
    lsnr - The listener for the Snoop (data pattern)
    data - The data pattern to Snoop (search) for
    ignoreCase - Ignore the case (only applies if data is ASCII)

    addSnoop

    public void addSnoop(SnoopListener lsnr,
                         byte[] data,
                         boolean ignoreCase)
    Add a 'Snoop' on input stream

    Parameters:
    lsnr - The listener for the Snoop (data pattern)
    data - The data pattern to Snoop (search) for
    ignoreCase - Ignore the case (only applies if data is ASCII)

    removeSnoop

    public void removeSnoop(SnoopListener lsnr,
                            java.lang.String data)
    Remove 'Snoop' on input stream

    Parameters:
    lsnr - Listener of Snoop to be removed.
    data - Snoop of Snoop to be removed. Note: Snoop is only removed if lsnr & data match

    removeSnoop

    public void removeSnoop(SnoopListener lsnr,
                            byte[] data)
    Remove snoop on input stream

    Parameters:
    lsnr - Listener of Snoop to be removed.
    data - Snoop of Snoop to be removed. Note: Snoop is only removed if lsnr & data match

    removeSnoop

    public void removeSnoop(SnoopListener lsnr)
    Remove 'Snoop' on input stream

    Parameters:
    lsnr - Listener of Snoop to be removed. Note: ALL Snoops for this listener will be removed.

    dataMatch

    public boolean dataMatch(byte[] d1,
                             byte[] d2)
    Check for match of two byte arrays.

    Parameters:
    d1 - Byte array of data
    d2 - Byte array of data

    snoopEvent

    public void snoopEvent(byte[] data)
    Description copied from interface: SnoopListener
    Receives notification when searched for data is sighted or timeout

    Specified by:
    snoopEvent in interface SnoopListener

    notifySighting

    protected void notifySighting(Serialio.SnoopInputStream.Snoop s)
    Notify listeners


    checkDataForMatch

    public void checkDataForMatch(byte b)
    Check byte for match with existing snoops

    Parameters:
    b - Byte to match. Patterns are considered 'mutually exclusive' for example consider the following snoops hi hihi hihihi and the data stream hihihi this will find a match of hi(2), hi(4), hihi(4), hi(6), hihihi(6) i.e. it will NOT generate two matches for the 'overlapping' pattern of hihi