Class KeywordScanner


  • public class KeywordScanner
    extends Object
    Provides the ability to find specified byte[] patterns inside a larger byte[].
    • Constructor Summary

      Constructors 
      Constructor Description
      KeywordScanner()  
      KeywordScanner​(byte[] data)
      Initializes a new KeywordScanner object with the provided data bytes.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int findNext()
      Find the next occurrence of the set pattern.
      int findNext​(int stop)
      Find the next occurrence of the set pattern, stopping at the specified index.
      int indexOf​(byte[] patternArg)
      Returns the first occurrence of the provided pattern in the data.
      int indexOf​(byte[] patternArg, int start)
      Returns the first occurrence of the provided pattern in the data, starting from the specified index.
      int indexOf​(byte[] patternArg, int start, int stop)
      Returns the first occurrence of the provided pattern in the data, starting from the specified index and stopping at the specified index.
      boolean isCaseSensitive()
      Returns the case sensitivity set for the scanner.
      List<Integer> listIndexOf​(byte[] patternArg)
      Returns a list of occurrences of the provided pattern in the data.
      List<Integer> listIndexOf​(byte[] patternArg, int start)
      Returns a list of occurrences of the provided pattern in the data, starting from the specified index.
      List<Integer> listIndexOf​(byte[] patternArg, int start, int stop)
      Returns a list of occurrences of the provided pattern in the data, starting from the specified index and stopping at the specified index.
      void resetData​(byte[] data)
      Reset the byte array.
      void resetData​(String data)  
      void resetData​(String data, String charsetName)  
      void resetData​(String data, Charset charset)  
      void setCaseSensitive​(boolean theCase)
      Sets the case sensitivity of the scanner.
    • Constructor Detail

      • KeywordScanner

        public KeywordScanner()
      • KeywordScanner

        public KeywordScanner​(byte[] data)
        Initializes a new KeywordScanner object with the provided data bytes.
        Parameters:
        data - the data to be scanned
    • Method Detail

      • resetData

        public void resetData​(String data)
      • resetData

        public void resetData​(String data,
                              String charsetName)
      • resetData

        public void resetData​(String data,
                              Charset charset)
      • resetData

        public void resetData​(@Nullable
                              byte[] data)
        Reset the byte array. Use of this method avoids having to instantiate a new KeywordScanner.
        Parameters:
        data - - bytes to match against
      • indexOf

        public int indexOf​(byte[] patternArg)
        Returns the first occurrence of the provided pattern in the data.
        Parameters:
        patternArg - the byte pattern to scan for, null returns -1
        Returns:
        the index in the data where the pattern begins, -1 if not found
      • indexOf

        public int indexOf​(byte[] patternArg,
                           int start)
        Returns the first occurrence of the provided pattern in the data, starting from the specified index.

        There is no restriction on the value of start. If it is negative, it has the same effect as if it were zero: the entire data will be scanned. If it is greater than the length of the data, it has the same effect as if it were equal to the length of the data: -1 is returned.

        Parameters:
        patternArg - the byte pattern to scan for, null returns -1
        start - the index to start searching from, negative values treated as 0
        Returns:
        the index in the data where the pattern begins, -1 if not found
      • indexOf

        public int indexOf​(@Nullable
                           byte[] patternArg,
                           int start,
                           int stop)
        Returns the first occurrence of the provided pattern in the data, starting from the specified index and stopping at the specified index.

        There is no restriction on the value of start. If it is negative, it has the same effect as if it were zero: the entire data may be scanned. If it is greater than the length of the data, it has the same effect as if it were equal to the length of the data: -1 is returned.

        If the value of stop is negative, greater than the data length, or less than or equal to the start value, -1 is returned.

        Parameters:
        patternArg - the byte pattern to scan for, null returns -1
        start - the index to start searching from, negative values treated as 0
        stop - the index to stop searching at, exclusive, negative value returns -1
        Returns:
        the index in the data where the pattern begins, -1 if not found
      • listIndexOf

        public List<Integer> listIndexOf​(byte[] patternArg)
        Returns a list of occurrences of the provided pattern in the data.
        Parameters:
        patternArg - the byte pattern to scan for, null returns empty list
        Returns:
        index list of positions in the data where the pattern begins, empty list if not found
      • listIndexOf

        public List<Integer> listIndexOf​(byte[] patternArg,
                                         int start)
        Returns a list of occurrences of the provided pattern in the data, starting from the specified index.

        There is no restriction on the value of start. If it is negative, it has the same effect as if it were zero: the entire data will be scanned. If it is greater than the length of the data, it has the same effect as if it were equal to the length of the data: empty list returned.

        Parameters:
        patternArg - the byte pattern to scan for, null returns empty list
        start - the index to start searching from, negative values treated as 0
        Returns:
        index list of positions in the data where the pattern begins, empty list if not found
      • listIndexOf

        public List<Integer> listIndexOf​(@Nullable
                                         byte[] patternArg,
                                         int start,
                                         int stop)
        Returns a list of occurrences of the provided pattern in the data, starting from the specified index and stopping at the specified index.

        There is no restriction on the value of start. If it is negative, it has the same effect as if it were zero: the entire data may be scanned. If it is greater than the length of the data, it has the same effect as if it were equal to the length of the data: an empty list is returned.

        If the value of stop is negative, greater than the data length, or less than or equal to the start value, an empty list is returned

        Parameters:
        patternArg - the byte pattern to scan for, null returns empty list
        start - the index to start searching from, negative values treated as 0
        stop - the index to stop searching at, exclusive, negative value returns empty list
        Returns:
        index list of positions in the data where the pattern begins, empty list if not found
      • findNext

        public int findNext​(int stop)
        Find the next occurrence of the set pattern, stopping at the specified index.

        If the value of stop is negative, greater than or equal to the data length, or less than the previously found index: -1 is returned.

        This method should follow a call to one of the indexOf methods. These methods will set the pattern and return the index of the first occurrence of the provided pattern. Calls to this method will then return the index of subsequent occurrences. Without first establishing a pattern in this way, -1 will be returned.

        Parameters:
        stop - the index to stop searching at, exclusive, negative value returns -1
        Returns:
        the index, less than the stop index, where the next occurrence of the pattern is found, -1 if not found
      • findNext

        public int findNext()
        Find the next occurrence of the set pattern.

        This method should follow a call to one of the indexOf methods. These methods will set the pattern and return the index of the first occurrence of the provided pattern. Calls to this method will then return the index of subsequent occurrences. Without first establishing a pattern in this way, -1 will be returned.

        Returns:
        the index where the next occurrence of the pattern is found, -1 if not found
      • setCaseSensitive

        public void setCaseSensitive​(boolean theCase)
        Sets the case sensitivity of the scanner.
        Parameters:
        theCase - if set to false, the scanner will ignore case. Default is true.
      • isCaseSensitive

        public boolean isCaseSensitive()
        Returns the case sensitivity set for the scanner.
        Returns:
        true if the scanner is case-sensitive, false otherwise