Class StringSource

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Serializable, java.lang.AutoCloseable, java.lang.Readable

    public class StringSource
    extends Source
    A source of characters based on a String.
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.lang.String mEncoding
      The encoding to report.
      protected int mMark
      The bookmark.
      protected int mOffset
      The current offset into the string.
      protected java.lang.String mString
      The source of characters.
      • Fields inherited from class org.htmlparser.lexer.Source

        EOF
      • Fields inherited from class java.io.Reader

        lock
    • Constructor Summary

      Constructors 
      Constructor Description
      StringSource​(java.lang.String string)
      Construct a source using the provided string.
      StringSource​(java.lang.String string, java.lang.String character_set)
      Construct a source using the provided string and encoding.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()
      Get the number of available characters.
      void close()
      Does nothing.
      void destroy()
      Close the source.
      char getCharacter​(int offset)
      Retrieve a character again.
      void getCharacters​(char[] array, int offset, int start, int end)
      Retrieve characters again.
      void getCharacters​(java.lang.StringBuffer buffer, int offset, int length)
      Append characters already read into a StringBuffer.
      java.lang.String getEncoding()
      Get the encoding being used to convert characters.
      java.lang.String getString​(int offset, int length)
      Retrieve a string comprised of characters already read.
      void mark​(int readAheadLimit)
      Mark the present position in the source.
      boolean markSupported()
      Tell whether this source supports the mark() operation.
      int offset()
      Get the position (in characters).
      int read()
      Read a single character.
      int read​(char[] cbuf)
      Read characters into an array.
      int read​(char[] cbuf, int off, int len)
      Read characters into a portion of an array.
      boolean ready()
      Tell whether this source is ready to be read.
      void reset()
      Reset the source.
      void setEncoding​(java.lang.String character_set)
      Set the encoding to the given character set.
      long skip​(long n)
      Skip characters.
      void unread()
      Undo the read of a single character.
      • Methods inherited from class java.io.Reader

        nullReader, read, transferTo
      • Methods inherited from class java.lang.Object

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

      • mString

        protected java.lang.String mString
        The source of characters.
      • mOffset

        protected int mOffset
        The current offset into the string.
      • mEncoding

        protected java.lang.String mEncoding
        The encoding to report. Only used by getEncoding().
      • mMark

        protected int mMark
        The bookmark.
    • Constructor Detail

      • StringSource

        public StringSource​(java.lang.String string)
        Construct a source using the provided string. Until it is set, the encoding will be reported as ISO-8859-1.
        Parameters:
        string - The source of characters.
      • StringSource

        public StringSource​(java.lang.String string,
                            java.lang.String character_set)
        Construct a source using the provided string and encoding. The encoding is only used by getEncoding().
        Parameters:
        string - The source of characters.
        character_set - The encoding to report.
    • Method Detail

      • getEncoding

        public java.lang.String getEncoding()
        Get the encoding being used to convert characters.
        Specified by:
        getEncoding in class Source
        Returns:
        The current encoding.
      • setEncoding

        public void setEncoding​(java.lang.String character_set)
                         throws ParserException
        Set the encoding to the given character set. This simply sets the encoding reported by getEncoding().
        Specified by:
        setEncoding in class Source
        Parameters:
        character_set - The character set to use to convert characters.
        Throws:
        ParserException - Not thrown.
      • close

        public void close()
                   throws java.io.IOException
        Does nothing. It's supposed to close the source, but use destroy() instead.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in class Source
        Throws:
        java.io.IOException - not used
        See Also:
        destroy()
      • read

        public int read()
                 throws java.io.IOException
        Read a single character.
        Specified by:
        read in class Source
        Returns:
        The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or EOF if the source is exhausted.
        Throws:
        java.io.IOException - If an I/O error occurs.
      • read

        public int read​(char[] cbuf,
                        int off,
                        int len)
                 throws java.io.IOException
        Read characters into a portion of an array.
        Specified by:
        read in class Source
        Parameters:
        cbuf - Destination buffer
        off - Offset at which to start storing characters
        len - Maximum number of characters to read
        Returns:
        The number of characters read, or EOF if the source is exhausted.
        Throws:
        java.io.IOException - If an I/O error occurs.
      • read

        public int read​(char[] cbuf)
                 throws java.io.IOException
        Read characters into an array.
        Specified by:
        read in class Source
        Parameters:
        cbuf - Destination buffer.
        Returns:
        The number of characters read, or EOF if the source is exhausted.
        Throws:
        java.io.IOException - If an I/O error occurs.
      • ready

        public boolean ready()
                      throws java.io.IOException
        Tell whether this source is ready to be read.
        Specified by:
        ready in class Source
        Returns:
        Equivalent to a non-zero available(), i.e. there are still more characters to read.
        Throws:
        java.io.IOException - Thrown if the source is closed.
      • reset

        public void reset()
                   throws java.lang.IllegalStateException
        Reset the source. Repositions the read point to begin at zero.
        Specified by:
        reset in class Source
        Throws:
        java.lang.IllegalStateException - If the source has been closed.
      • markSupported

        public boolean markSupported()
        Tell whether this source supports the mark() operation.
        Specified by:
        markSupported in class Source
        Returns:
        true.
      • mark

        public void mark​(int readAheadLimit)
                  throws java.io.IOException
        Mark the present position in the source. Subsequent calls to reset() will attempt to reposition the source to this point.
        Specified by:
        mark in class Source
        Parameters:
        readAheadLimit - Not used.
        Throws:
        java.io.IOException - Thrown if the source is closed.
      • skip

        public long skip​(long n)
                  throws java.io.IOException,
                         java.lang.IllegalArgumentException
        Skip characters. Note: n is treated as an int
        Specified by:
        skip in class Source
        Parameters:
        n - The number of characters to skip.
        Returns:
        The number of characters actually skipped
        Throws:
        java.lang.IllegalArgumentException - If n is negative.
        java.io.IOException - If the source is closed.
      • unread

        public void unread()
                    throws java.io.IOException
        Undo the read of a single character.
        Specified by:
        unread in class Source
        Throws:
        java.io.IOException - If no characters have been read or the source is closed.
      • getCharacter

        public char getCharacter​(int offset)
                          throws java.io.IOException
        Retrieve a character again.
        Specified by:
        getCharacter in class Source
        Parameters:
        offset - The offset of the character.
        Returns:
        The character at offset.
        Throws:
        java.io.IOException - If the source is closed or an attempt is made to read beyond offset().
      • getCharacters

        public void getCharacters​(char[] array,
                                  int offset,
                                  int start,
                                  int end)
                           throws java.io.IOException
        Retrieve characters again.
        Specified by:
        getCharacters in class Source
        Parameters:
        array - The array of characters.
        offset - The starting position in the array where characters are to be placed.
        start - The starting position, zero based.
        end - The ending position (exclusive, i.e. the character at the ending position is not included), zero based.
        Throws:
        java.io.IOException - If the source is closed or an attempt is made to read beyond offset().
      • getString

        public java.lang.String getString​(int offset,
                                          int length)
                                   throws java.io.IOException
        Retrieve a string comprised of characters already read. Asking for characters ahead of offset() will throw an exception.
        Specified by:
        getString in class Source
        Parameters:
        offset - The offset of the first character.
        length - The number of characters to retrieve.
        Returns:
        A string containing the length characters at offset.
        Throws:
        java.io.IOException - If the source is closed or an attempt is made to read beyond offset().
      • getCharacters

        public void getCharacters​(java.lang.StringBuffer buffer,
                                  int offset,
                                  int length)
                           throws java.io.IOException
        Append characters already read into a StringBuffer. Asking for characters ahead of offset() will throw an exception.
        Specified by:
        getCharacters in class Source
        Parameters:
        buffer - The buffer to append to.
        offset - The offset of the first character.
        length - The number of characters to retrieve.
        Throws:
        java.io.IOException - If the source is closed or an attempt is made to read beyond offset().
      • destroy

        public void destroy()
                     throws java.io.IOException
        Close the source. Once a source has been closed, further read, ready, mark, reset, skip, unread, getCharacter or getString invocations will throw an IOException. Closing a previously-closed source, however, has no effect.
        Specified by:
        destroy in class Source
        Throws:
        java.io.IOException - Not thrown
      • offset

        public int offset()
        Get the position (in characters).
        Specified by:
        offset in class Source
        Returns:
        The number of characters that have already been read, or EOF if the source is closed.
      • available

        public int available()
        Get the number of available characters.
        Specified by:
        available in class Source
        Returns:
        The number of characters that can be read or zero if the source is closed.