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

Pattern splitAsStream is not late binding as required by the specification

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 9
    • Fix Version/s: 9
    • Component/s: core-libs
    • Labels:
      None

      Description

      Currently Pattern.splitAsStream JavaDoc says [1]:

      * <p> If the input sequence is mutable, it must remain constant during the
      * execution of the terminal stream operation. Otherwise, the result of the
      * terminal stream operation is undefined.

      However in reality the sequence must remain constant from the stream
      creation till the end of the terminal operation. Let's check:

      public static void main(String[] args) {
         StringBuilder sb = new StringBuilder("a,b,c,d,e");
         Stream<String> stream = Pattern.compile(",").splitAsStream(sb);
         // Modify the CharSequence after stream creation
         sb.setLength(3);
         // During the terminal operation it remains constant
         stream.forEach(System.out::println);
      }

      The result is:
      a
      Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
         at java.lang.AbstractStringBuilder.charAt(AbstractStringBuilder.java:210)
         at java.lang.StringBuilder.charAt(StringBuilder.java:76)
      ...

        Attachments

          Activity

            People

            Assignee:
            psandoz Paul Sandoz
            Reporter:
            psandoz Paul Sandoz
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: