title: Strange behavior of Stream.spliterator for parallel streams
body: <p>I'm using the stream spliterator directly for the low-level operations in the library I'm writing. Recently I discovered very weird behavior when I take the stream spliterator and interleave <code>tryAdvance/trySplit</code> calls. Here's a simple code which demonstrates the problem:</p>
  
  <pre><code>import java.util.Arrays;
  import java.util.Spliterator;
  
  public class SpliteratorBug {
      public static void main(String[] args) {
          Integer[][] input = { { 1 }, { 2, 3 }, { 4, 5, 6 }, { 7, 8 }, { 9 } };
          Spliterator&lt;Integer&gt; spliterator = Arrays.stream(input).parallel()
                  .flatMap(Arrays::stream).spliterator();
          spliterator.trySplit();
          spliterator.tryAdvance(s -&gt; {});
          spliterator.trySplit();
          spliterator.forEachRemaining(System.out::println);
      }
  }
  </code></pre>
  
  <p>The output is</p>
  
  <pre><code>5
  6
  9
  </code></pre>
  
  <p>As you can see, after flat-mapping I should get the ordered stream of consecutive numbers from <code>1</code> to <code>9</code>. I split the spliterator once, so it should jump to some intermediate location. Next I consume an element from it and split it one more time. After that I print all the remaining elements. I expect that I will have several consecutive elements from the stream tail (probably zero elements, it would also be fine). However what I get is <code>5</code> and <code>6</code>, then sudden jump to <code>9</code>.</p>
  
  <p>I know that currently in JDK spliterators are not used this way: they always split before the traversal. However official <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Spliterator.html">documentation</a> does not explicitly forbid to call the <code>trySplit</code> after <code>tryAdvance</code>.</p>
  
  <p>The problem was never observed when I use spliterator created directly from collection, array, generated source, etc. It's observed only if the spliterator was created from the parallel stream which had the intermediate <code>flatMap</code>.</p>
  
  <p>So the question is: did I hit the bug or it's explicitly forbidden somewhere to use the spliterator in this way?</p>