@ Override public Stream < Chunk < byte [ ] , BytesReference > > chunks ( ) { Iterator < Chunk < byte [ ] , BytesReference > > iterator = new Iterator < Chunk < byte [ ] , BytesReference > > ( ) { Chunk < byte [ ] , BytesReference > nextData = null ; @ Override public boolean hasNext ( ) { if ( nextData != null ) { return true ; } else { try { nextData = readChunk ( ) ; return nextData != null ; } catch ( IOException e ) { throw new UncheckedIOException ( e ) ; } } } @ Override public Chunk < byte [ ] , BytesReference > next ( ) { if ( nextData != null || hasNext ( ) ) { Chunk < byte [ ] , BytesReference > data = nextData ; nextData = null ; return data ; } else { throw new NoSuchElementException ( ) ; } } } ; return StreamSupport . stream ( Spliterators . spliteratorUnknownSize ( iterator , Spliterator . ORDERED | Spliterator . NONNULL ) , false ) ; }
This methods creates a Java 8 stream of chunks .