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

Pattern splitAsStream is not late binding as required by the specification

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • 9
    • 9
    • core-libs
    • 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

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

            Dates

              Created:
              Updated:
              Resolved: