JDK-6325564 : (str) Provide CharSequenceReader with sub-sequence capability
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 5.0
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: solaris_10
  • CPU: x86
  • Submitted: 2005-09-19
  • Updated: 2017-07-19
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE REQUEST :
Provide a CharSequenceReader as an exact copy of the existing StringReader class as a necessary generalisation after introduction of the CharSequence interface.

The actual StringReader can become a subclass of CharSequenceReader, with an overloaded constructor for binary backward compatibility.

All these requests would be fulfilled by storing internally a CharSequence instead of a String. It would allow for String, StringBuffer, StringBuilder or any other character sequence.

StringReader is not Serializable, and it is quite unlikely that anybody has ever made a serializable subclass, so it could easily be reparented as deriving from a new CharSequenceReader class, with a single overloaded constructor and no other specific methods.

It would also be very useful, and very simple, to add a constructor to read only a subset of the CharSequence/String without explicitely extracting the subsequence/substring and thus increasing performance.

    /**
     * Create a new char sequence reader.
     * Direct replacement for new CharSequenceReader(s.subSequence(start, end)).
     *
     * @param s  CharSequence providing the character stream.
     * @param start Beginning of the subSequence.
     * @param end End of the subSequence.
     */
    public CharSequenceReader(CharSequence cs, int start, int end) {
        if (start < 0 || start > end || end > cs.length()) {
            throw new IndexOutOfBoundsException();
        }
	this.str = cs;
        this.next = start;
        this.mark = start;
	this.length = end;
    }


JUSTIFICATION :
The CharSequence interface has generalized the concept of a String, StringBuffer, StringBuilder and others for read access.
Related classes should also be generalised.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Obtain a reader for any sort of CharSequence objects or a sub-sequence of them.
ACTUAL -
A reader can only be obtained from a String using StringReader. This is too restrictive since introduction of the CharSequence interface.

---------- BEGIN SOURCE ----------
// Source code of java.lang.StringReader + new contructor:

    /**
     * Create a new char sequence reader.
     * Direct replacement for new CharSequenceReader(s.subSequence(start, end)).
     *
     * @param s  CharSequence providing the character stream.
     * @param start Beginning of the subSequence.
     * @param end End of the subSequence.
     */
    public CharSequenceReader(CharSequence cs, int start, int end) {
        if (start < 0 || start > end || end > cs.length()) {
            throw new IndexOutOfBoundsException();
        }
	this.str = cs;
        this.next = start;
        this.mark = start;
	this.length = end;
    }

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Copy of class StringReader with member field "str" of type "CharSequence" instead of "String" + new constructor above.

Comments
If this enhancement is deemed worthwhile, an alternative might be to add a new constructor to StringReader that takes a CharSequence parameter.
04-05-2017