-
Type:
Bug
-
Resolution: Fixed
-
Priority:
P4
-
Affects Version/s: 9
-
Component/s: core-libs
-
None
-
b99
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)
...
* <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)
...