Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8341596

Add Reader.of(CharSequence)

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 24
    • core-libs
    • None
    • minimal
    • This is a new static method, should be no compatibility risk.
    • Java API
    • SE

      Summary

      New static factory method Reader of(CharSequence) on java.io.Reader to return a Reader that reads characters from the given CharSequence.

      Problem

      java.io.Reader is specified to be "Abstract class for reading character streams." Yet, there is no convenient way to create a Reader from a CharSequence.

      The motivation for the new method is performance as new StringReader(charSequence.toString()) is expensive and the resulting StringReader has overhead to due synchronization (the overhead increased when biased locked was removed).

      Solution

      Add a new static method factory method public static Reader of(CharSequence) to Reader.

      Add API note to java.io.StringReader indicating it is superseded by Reader::of in non-thread-safe contexts.

      Specification

      /**
       * Returns a {@code Reader} that reads characters from a
       * {@code CharSequence}.
       *
       * <p> The reader is initially open and reading starts at the
       * first character in the sequence.
       *
       * <p> The resulting reader is not safe for use by multiple
       * concurrent threads. If the reader is to be used by more than one
       * thread it should be controlled by appropriate synchronization.
       *
       * <p> If the sequence changes while the reader is open, e.g. the length
       * changes, the behavior is undefined.
       *
       * <p> The returned reader supports the {@link #mark mark()} and
       * {@link #reset reset()} operations.
       *
       * @param cs {@code CharSequence} providing the character stream.
       * @return a {@code Reader} which reads characters from {@code cs}
       * @throws NullPointerException if {@code cs} is {@code null}
       *
       * @since 24
       */
      public static Reader of(final CharSequence cs)

            mkarg Markus Karg
            mkarg Markus Karg
            Alan Bateman, Chen Liang, Roger Riggs
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: