001/* 002 * Copyright 2010-2023 The jdependency developers. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.vafer.jdependency.utils; 017 018import java.util.Iterator; 019import java.util.stream.Stream; 020import java.util.stream.StreamSupport; 021import java.util.Spliterator; 022import java.util.Spliterators; 023import java.util.jar.JarEntry; 024import java.util.jar.JarInputStream; 025import java.io.IOException; 026 027/** 028 * internal - do not use 029 */ 030 031public final class StreamUtils { 032 033 private StreamUtils() {} 034 035 // public static <T> Stream<T> asStream(Iterator<T> sourceIterator) { 036 // return asStream(sourceIterator, false); 037 // } 038 039 // public static <T> Stream<T> asStream(Iterator<T> sourceIterator, boolean parallel) { 040 // Iterable<T> iterable = () -> sourceIterator; 041 // return StreamSupport.stream(iterable.spliterator(), parallel); 042 // } 043 044 public static Stream<JarEntry> asStream( final JarInputStream pInputStream ) { 045 return StreamSupport.stream(Spliterators.spliteratorUnknownSize( 046 new Iterator<JarEntry>() { 047 048 JarEntry entry = null; 049 050 public boolean hasNext() { 051 try { 052 if (entry == null) { 053 entry = pInputStream.getNextJarEntry(); 054 } 055 return entry != null; 056 } catch(IOException e) { 057 throw new RuntimeException(e); 058 } 059 } 060 061 public JarEntry next() { 062 try { 063 JarEntry result = entry != null 064 ? entry 065 : pInputStream.getNextJarEntry(); 066 entry = null; 067 return result; 068 } catch(IOException e) { 069 throw new RuntimeException(e); 070 } 071 } 072 073 }, Spliterator.IMMUTABLE), false); 074 } 075}