Question about writeStream

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Question about writeStream

JeremieRegnault
Hi,

I am writing tests with streams, and I noticed that a stream opened on a
string doesn't close, but FileStreams do close. Is it normal ?

--
Jérémie Regnault

Reply | Threaded
Open this post in threaded view
|

Re: Question about writeStream

Sven Van Caekenberghe-2

> On 29 Jun 2017, at 16:07, Jérémie Regnault <[hidden email]> wrote:
>
> Hi,
>
> I am writing tests with streams, and I noticed that a stream opened on a
> string doesn't close, but FileStreams do close. Is it normal ?

How do you mean that ? That FileStreams close automatically ?

That is indeed the case, that they are closed as a finalization action (i.e. when they become garbage), but that I consider that a fail safe thing, not something you should rely on. They should be properly closed, like #readStreamDo: and #writeStreamDo: do.

Streams over collections such as Strings do not need closing, as there is no opening either. But general stream code better does.

> --
> Jérémie Regnault
>


Reply | Threaded
Open this post in threaded view
|

Re: Question about writeStream

Guillermo Polito
In fact the question comes from the fact that they are not polymorphic and you should know if you're manipulating a closable stream or not...

Maybe a solution is indeed to use #writeStreamDo:, that should behave well even for in-memory, collection streams, no?

On Thu, Jun 29, 2017 at 4:58 PM, Sven Van Caekenberghe <[hidden email]> wrote:

> On 29 Jun 2017, at 16:07, Jérémie Regnault <[hidden email]> wrote:
>
> Hi,
>
> I am writing tests with streams, and I noticed that a stream opened on a
> string doesn't close, but FileStreams do close. Is it normal ?

How do you mean that ? That FileStreams close automatically ?

That is indeed the case, that they are closed as a finalization action (i.e. when they become garbage), but that I consider that a fail safe thing, not something you should rely on. They should be properly closed, like #readStreamDo: and #writeStreamDo: do.

Streams over collections such as Strings do not need closing, as there is no opening either. But general stream code better does.

> --
> Jérémie Regnault
>





--

   

Guille Polito


Research Engineer

French National Center for Scientific Research - http://www.cnrs.fr



Web: http://guillep.github.io

Phone: +33 06 52 70 66 13