Class ParserReader

java.lang.Object
java.io.Reader
org.apache.juneau.parser.ParserReader
All Implemented Interfaces:
Closeable, AutoCloseable, Readable, Positionable
Direct Known Subclasses:
UonReader

public class ParserReader extends Reader implements Positionable
Similar to a PushbackReader with a pushback buffer of 1 character.

Code is optimized to work with a 1 character buffer.

Additionally keeps track of current line and column number, and provides the ability to set mark points and capture characters from the previous mark point.

Notes:
  • This class is not thread safe.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Reader
    Wrapped reader

    Fields inherited from class java.io.Reader

    lock
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    No-op.
    Trims off the last character in the marking buffer.
    delete(int count)
    Trims off the specified number of last characters in the marking buffer.
    final String
    Returns the contents of the reusable character buffer as a string, and resets the buffer for next usage.
    final String
    getMarked(int offsetStart, int offsetEnd)
    Same as getMarked() except allows you to specify offsets into the buffer.
    Returns the current position in a reader or input stream.
    final void
    Start buffering the calls to read() so that the text can be gathered from the mark point on calling getFromMarked().
    Reads a numeric string from the specified reader.
    final int
    Peeks the next character in the stream.
    final int
    Same as peek() but skips over any whitespace characters.
    final int
    Reads a single character.
    int
    read(char[] cbuf, int off, int len)
    Subclasses can override this method to provide additional filtering.
    final String
    read(int num)
    Read the specified number of characters off the stream.
    final int
    Same as read() but detects and combines extended unicode characters (characters above 0x10000).
    final int
    Same as read() but skips over any whitespace characters.
    replace(char c)
    Replace the last read character in the buffer with the specified character.
    replace(int c, int offset)
    Replaces the last character in the marking buffer with the specified character.
    Pushes the last read character back into the stream.

    Methods inherited from class java.io.Reader

    mark, markSupported, nullReader, read, read, ready, reset, skip, transferTo

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • r

      protected final Reader r
      Wrapped reader
  • Constructor Details

  • Method Details

    • read

      public final int read() throws IOException
      Reads a single character.

      Note that this method does NOT process extended unicode characters (i.e. characters above 0x10000), but rather returns them as two chars. Use readCodePoint() to ensure proper handling of extended unicode.

      Overrides:
      read in class Reader
      Returns:
      The character read, or -1 if the end of the stream has been reached.
      Throws:
      IOException - If a problem occurred trying to read from the reader.
    • readSkipWs

      public final int readSkipWs() throws IOException
      Same as read() but skips over any whitespace characters.
      Returns:
      The first non-whitespace character, or -1 if the end of stream reached.
      Throws:
      IOException - Thrown by underlying stream.
    • readCodePoint

      public final int readCodePoint() throws IOException
      Same as read() but detects and combines extended unicode characters (characters above 0x10000).
      Returns:
      The character read, or -1 if the end of the stream has been reached.
      Throws:
      IOException - If a problem occurred trying to read from the reader.
    • mark

      public final void mark()
      Start buffering the calls to read() so that the text can be gathered from the mark point on calling getFromMarked().
    • peek

      public final int peek() throws IOException
      Peeks the next character in the stream.

      This is equivalent to doing a read() followed by an unread().

      Returns:
      The peeked character, or (char)-1 if the end of the stream has been reached.
      Throws:
      IOException - If a problem occurred trying to read from the reader.
    • peekSkipWs

      public final int peekSkipWs() throws IOException
      Same as peek() but skips over any whitespace characters.

      This is equivalent to doing a read() followed by an unread().

      Returns:
      The peeked character, or (char)-1 if the end of the stream has been reached.
      Throws:
      IOException - If a problem occurred trying to read from the reader.
    • read

      public final String read(int num) throws IOException
      Read the specified number of characters off the stream.
      Parameters:
      num - The number of characters to read.
      Returns:
      The characters packaged as a String.
      Throws:
      IOException - If a problem occurred trying to read from the reader.
    • unread

      public ParserReader unread() throws IOException
      Pushes the last read character back into the stream.
      Returns:
      This object.
      Throws:
      IOException - If a problem occurred trying to read from the reader.
    • close

      public void close() throws IOException
      No-op.

      Input readers are closed in the ParserPipe class.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class Reader
      Throws:
      IOException - If a problem occurred trying to read from the reader.
    • getMarked

      public final String getMarked()
      Returns the contents of the reusable character buffer as a string, and resets the buffer for next usage.
      Returns:
      The contents of the reusable character buffer as a string.
    • getMarked

      public final String getMarked(int offsetStart, int offsetEnd)
      Same as getMarked() except allows you to specify offsets into the buffer.

      For example, to return the marked string, but trim the first and last characters, call the following:

      getFromMarked(1, -1);

      Parameters:
      offsetStart - The offset of the start position.
      offsetEnd - The offset of the end position.
      Returns:
      The contents of the reusable character buffer as a string.
    • delete

      public final ParserReader delete()
      Trims off the last character in the marking buffer.

      Useful for removing escape characters from sequences.

      Returns:
      This object.
    • delete

      public final ParserReader delete(int count)
      Trims off the specified number of last characters in the marking buffer. Useful for removing escape characters from sequences.
      Parameters:
      count - The number of characters to delete.
      Returns:
      This object.
    • replace

      public final ParserReader replace(int c, int offset) throws IOException
      Replaces the last character in the marking buffer with the specified character.

      offset must be at least 1 for normal characters, and 2 for extended unicode characters in order for the replacement to fit into the buffer.

      Parameters:
      c - The new character.
      offset - The offset.
      Returns:
      This object.
      Throws:
      IOException - Thrown by underlying stream.
    • replace

      public final ParserReader replace(char c) throws IOException
      Replace the last read character in the buffer with the specified character.
      Parameters:
      c - The new character.
      Returns:
      This object.
      Throws:
      IOException - Thrown by underlying stream.
    • parseNumberString

      Reads a numeric string from the specified reader.
      Returns:
      The parsed number string.
      Throws:
      IOException - Thrown by underlying stream.
    • read

      public int read(char[] cbuf, int off, int len) throws IOException
      Subclasses can override this method to provide additional filtering.

      Default implementation simply calls the same method on the underlying reader.

      Specified by:
      read in class Reader
      Throws:
      IOException
    • getPosition

      Description copied from interface: Positionable
      Returns the current position in a reader or input stream.
      Specified by:
      getPosition in interface Positionable
      Returns:
      The current position in a reader or input stream.