commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rob Spoor <>
Subject [IO] Improvements to CharSequenceReader
Date Thu, 01 Aug 2019 10:31:07 GMT

CharSequenceReader is great, but I think there can be two improvements:

1) read(char[], int, int) currently calls read() several times, which 
delegates to charSequence.charAt. That's fine in Java 8 and before, but 
in Java 9 the internal storage of String, StringBuilder and StringBuffer 
is changed. It's better to use getChars. For instance, after the 
length/offset check:

         if (charSequence instanceof String) {
             int count = Math.min(length, charSequence.length() - idx);
             ((String) charSequence).getChars(idx, idx + count, array, 
             return count;
         // similar for StringBuilder and StringBuffer
         // existing code

2) It would be nice to be able to create a CharSequenceReader for only a 
portion of the CharSequence. This prevents the need of having to call 
subSequence, which creates a whole new String for StringBuilder and 
The class would get two extra int fields, start and end:
* start is only used in close() to reset idx and mark to start instead of 0
* end is used as upper bound instead of charSequence.length()

To prevent IndexOutOfBoundsExceptions if the CharSequence is shrunk, a 
private method end() could be used, which returns Math.min(end, 
charSequence.length()). The original constructor could then set start 
and end to 0 and Integer.MAX_VALUE respectively. One or two constructors 
would need to be added to support setting the start and end, and 
possibly only the start.

One thing that may make this difficult is serialization. 
CharSequenceReader implements Serializable, so it should first be 
investigated if this doesn't break serialization.

Kind regards,


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message