Identifying resource-using streams
Victor Nazarov
asviraspossible at gmail.com
Tue Apr 22 12:37:57 UTC 2014
On Mon, Apr 21, 2014 at 11:31 PM, Peter Levart <peter.levart at gmail.com>wrote:
>
> On 04/21/2014 08:42 AM, Timo Kinnunen wrote:
>
>> almost all of the cases where I plan to use Streams involve reading the
>> contents of directories, reading the contents of files or reading the
>> contents of JAR files. All are cases where resources are involved and the
>> processing too complicated to fit in a single method.
>>
>> Using things like a Supplier<FileInputStream> it would be very easy to
>> delay the resource acquisition so that it could happen inside the terminal
>> method, making every terminal naturally responsible for releasing resources.
>>
>> All of the above brings up another unfortunate feature of the API: a
>> Stream is very close to being an immutable stateless builder for
>> stream-like processing but it is neither immutable nor stateless.
>>
>> If Stream was immutable and stateless then you would be able to reuse,
>> rerun and combine Streams freely and all the processing would be happening
>> in a single place, inside terminals. Because where else could it happen?
>>
>
> Hi,
>
> Here's some ideas I played with while Streams API was still being
> developed:
>
> https://github.com/plevart/streamx
>
> I have just updated it so that it compiles with released JDK 8 API.
>
>
>
Here is my take on stream library.
https://github.com/sviperll/chicory/tree/master/chicory-stream
Streams doesn't have close method, instread resources are aquired and
released on every terminal operation. See the difinition of
StreamableTextFile for example:
https://github.com/sviperll/chicory/blob/master/chicory-stream/src/main/java/org/github/sviperll/stream/StreamableTextFile.java
The only case when you required to manually close resource is when you use
iterators. Stream provides openIterator method. See tests for example usage:
https://github.com/sviperll/chicory/blob/master/chicory-stream/src/test/java/org/github/sviperll/stream/StreamTest.java
--
Victor Nazarov
More information about the lambda-dev
mailing list