Class CharReaderScanner

java.lang.Object
io.github.mmm.scanner.AbstractCharStreamScanner
io.github.mmm.scanner.CharReaderScanner
All Implemented Interfaces:
TextFormatProcessor, TextPosition, CharStreamScanner, AutoCloseable

public class CharReaderScanner extends AbstractCharStreamScanner
Implementation of CharStreamScanner that adapts a Reader to read and parse textual data.
  • Field Details

  • Constructor Details

    • CharReaderScanner

      public CharReaderScanner()
      The constructor.
    • CharReaderScanner

      public CharReaderScanner(TextFormatMessageHandler messageHandler)
      The constructor.
      Parameters:
      messageHandler - the TextFormatMessageHandler.
    • CharReaderScanner

      public CharReaderScanner(Reader reader)
      The constructor.
      Parameters:
      reader - the (initial) Reader.
    • CharReaderScanner

      public CharReaderScanner(TextFormatMessageHandler messageHandler, Reader reader)
      The constructor.
      Parameters:
      messageHandler - the TextFormatMessageHandler.
      reader - the (initial) Reader.
    • CharReaderScanner

      public CharReaderScanner(int capacity)
      The constructor.
      Parameters:
      capacity - the buffer capacity.
    • CharReaderScanner

      public CharReaderScanner(int capacity, TextFormatMessageHandler messageHandler)
      The constructor.
      Parameters:
      capacity - the buffer capacity.
      messageHandler - the TextFormatMessageHandler.
    • CharReaderScanner

      public CharReaderScanner(int capacity, Reader reader)
      The constructor.
      Parameters:
      capacity - the buffer capacity.
      reader - the (initial) Reader.
    • CharReaderScanner

      public CharReaderScanner(int capacity, TextFormatMessageHandler messageHandler, Reader reader)
      The constructor.
      Parameters:
      capacity - the buffer capacity.
      messageHandler - the TextFormatMessageHandler.
      reader - the (initial) Reader.
  • Method Details

    • getPosition

      public int getPosition()
      Returns:
      the position in the sequence to scan or in other words the number of characters that have been read. Will initially be 0. Please note that this API is designed for scanning textual content (for parsers). Therefore we consider 2.1 terabyte as a suitable limit.
    • peek

      public char peek(int lookaheadOffset)
      Description copied from interface: CharStreamScanner
      Like CharStreamScanner.peek() but with further lookahead.
      Attention:
      This method requires lookahead. For implementations that are backed by an underlying stream (or reader) the given lookaheadOffset shall not exceed the available lookahead size (buffer capacity given at construction time). Otherwise the method may fail.
      Parameters:
      lookaheadOffset - the lookahead offset. If 0 this method will behave like CharStreamScanner.peek(). In case of 1 it will return the character after the next one and so forth.
      Returns:
      the peeked character at the given lookaheadOffset or CharStreamScanner.EOS if no such character exists.
    • peekString

      public String peekString(int count)
      Description copied from interface: CharStreamScanner
      This method peeks the number of next characters given by count and returns them as String. If there are less characters available the returned String will be shorter than count and only contain the available characters. Unlike CharStreamScanner.read(int) this method does not consume the characters and will therefore not change the state of this scanner.
      Attention:
      This method requires lookahead. For implementations that are backed by an underlying stream (or reader) the given count shall not exceed the available lookahead size (buffer capacity given at construction time). Otherwise the method may fail.
      Parameters:
      count - is the number of characters to peek. You may use Integer.MAX_VALUE to peek until the end of text (EOT) if the data-size is suitable.
      Returns:
      a string with the given number of characters or all available characters if less than count. Will be the empty string if no character is available at all.
    • peekWhile

      public String peekWhile(CharFilter filter, int maxLen)
      Parameters:
      filter - the CharFilter accepting only the characters to peek.
      maxLen - the maximum number of characters to peek (to get as lookahead without modifying this stream).
      Returns:
      a String with the peeked characters of the given maxLen or less if a character was hit that is not accepted by the given filter or the end-of-text has been reached before. The state of this stream remains unchanged.
      See Also:
    • getBufferToParse

      public String getBufferToParse()
      Specified by:
      getBufferToParse in interface CharStreamScanner
      Overrides:
      getBufferToParse in class AbstractCharStreamScanner
      Returns:
      the String with the characters that have not yet been parsed but are available in the underlying buffer. May be used for debugging or error messages.
    • setReader

      public void setReader(Reader reader)
      Resets this buffer for reuse with a new Reader.
      This will also reset the position.
      Parameters:
      reader - the new Reader to set. May be null to entirely clear this buffer.
    • fill

      protected boolean fill()
      Description copied from class: AbstractCharStreamScanner
      Consumes all remaining data in the internal AbstractCharStreamScanner.buffer and fills the AbstractCharStreamScanner.buffer with further data (if available from underlying source such as a stream/reader). If the end of the stream has not been reached, all buffers should be filled now.
      Overrides:
      fill in class AbstractCharStreamScanner
      Returns:
      true if data was filled, false if EOS.
    • close

      public void close()
    • isEos

      public boolean isEos()
      Overrides:
      isEos in class AbstractCharStreamScanner
      Returns:
      true if the end of stream (EOS) has been reached, false otherwise. If true (EOS) the internal buffer contains the entire rest of the data to scan in memory. If then also all data is consumed from the buffer, EOT has been reached. For instances of that are not backed by an underlying stream of data (like CharSequenceScanner) this method will always return true.
    • isEob

      protected boolean isEob()
      Overrides:
      isEob in class AbstractCharStreamScanner
      Returns:
      true if end of buffer (EOB) or in other words no data is available after the current AbstractCharStreamScanner.buffer, false otherwise (e.g. if not EOS).
    • isEot

      public boolean isEot()
      Description copied from class: AbstractCharStreamScanner
      ATTENTION:
      Overrides:
      isEot in class AbstractCharStreamScanner
      Returns:
      true if end of text (EOT) is known to have been reached, false otherwise. The returned result will be almost the same as !AbstractCharStreamScanner.hasNext() but this method will not modify the state of this scanner (read additional data, modify buffers, etc.). However, if the underlying stream is already consumed without returning -1 to signal EOS this method may return false even though the next call of AbstractCharStreamScanner.hasNext() may also return false.
    • expect

      public boolean expect(String expected, boolean ignoreCase, boolean lookahead, int off)
      Description copied from interface: CharStreamScanner
      This method determines if the given expected String is completely present at the current position. It will only consume characters and change the state if lookahead is false and the expected String was found (entirely).
      Attention:
      This method requires lookahead. For implementations that are backed by an underlying stream (or reader) the length of the expected String shall not exceed the available lookahead size (buffer capacity given at construction time). Otherwise the method may fail.
      Parameters:
      expected - the expected String to search for.
      ignoreCase - - if true the case of the characters is ignored when compared, false otherwise.
      lookahead - - if true the state of the scanner remains unchanged even if the expected String has been found, false otherwise (expected String is consumed on match).
      off - the number of characters that have already been peeked and after which the given String is expected. Will typically be 0. If lookahead is false and the expected String was found these characters will be skipped together with the expected String.
      Returns:
      true if the expected string was successfully found, false otherwise.
    • verifyLookahead

      protected void verifyLookahead(int length)
      Overrides:
      verifyLookahead in class AbstractCharStreamScanner
      Parameters:
      length - the number of characters for lookahead (match without consuming). May fail if the length exceeds the buffer size.
    • expectRestWithLookahead

      protected boolean expectRestWithLookahead(char[] stopChars, boolean ignoreCase, Runnable appender, boolean skip)
      Specified by:
      expectRestWithLookahead in class AbstractCharStreamScanner
      Parameters:
      stopChars - the stop String as char[]. If ignoreCase is true in lower case.
      ignoreCase - - true to (also) compare chars in lower case, false otherwise.
      appender - an optional lambda to run before shifting buffers to append data.
      skip - - true to update buffers and offset such that on success this scanner points after the expected stop String, false otherwise (to not consume any character in any case).
      Returns:
      true if the stop String (stopChars) was found and consumed, false otherwise (and no data consumed).
      See Also:
    • reset

      protected void reset()
      Description copied from class: AbstractCharStreamScanner
      Resets the internal state.
      Overrides:
      reset in class AbstractCharStreamScanner