Class CharSource.StringCharSource

  • Direct Known Subclasses:
    CharSource.EmptyCharSource
    Enclosing class:
    CharSource

    private static class CharSource.StringCharSource
    extends CharSource.CharSequenceCharSource
    Subclass specialized for string instances.

    Since Strings are immutable and built into the jdk we can optimize some operations

    • use StringReader instead of CharSequenceReader. It is faster since it can use String.getChars(int, int, char[], int) instead of copying characters one by one with CharSequence.charAt(int).
    • use Appendable.append(CharSequence) in copyTo(Appendable) and copyTo(CharSink). We know this is correct since strings are immutable and so the length can't change, and it is faster because many writers and appendables are optimized for appending string instances.
    • Constructor Detail

      • StringCharSource

        protected StringCharSource​(java.lang.String seq)
    • Method Detail

      • openStream

        public java.io.Reader openStream()
        Description copied from class: CharSource
        Opens a new Reader for reading from this source. This method returns a new, independent reader each time it is called.

        The caller is responsible for ensuring that the returned reader is closed.

        Overrides:
        openStream in class CharSource.CharSequenceCharSource
      • copyTo

        public long copyTo​(java.lang.Appendable appendable)
                    throws java.io.IOException
        Description copied from class: CharSource
        Appends the contents of this source to the given Appendable (such as a Writer). Does not close appendable if it is Closeable.
        Overrides:
        copyTo in class CharSource
        Returns:
        the number of characters copied
        Throws:
        java.io.IOException - if an I/O error occurs while reading from this source or writing to appendable
      • copyTo

        public long copyTo​(CharSink sink)
                    throws java.io.IOException
        Description copied from class: CharSource
        Copies the contents of this source to the given sink.
        Overrides:
        copyTo in class CharSource
        Returns:
        the number of characters copied
        Throws:
        java.io.IOException - if an I/O error occurs while reading from this source or writing to sink