Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who should be responsible for closing a stream

Tags:

c#

.net

stream

I'm writing an application that creates a "Catalog" of files, which can be attributed with other meta data files such as attachments and thumbnails.

I'm trying to abstract the interface to a catalog to the point where a consumer of a catalog does not need to know about the underlying file system used to store the files. So I've created an interface called IFileSystemAdaptor which is shown below.

public interface IFileSystemAdaptor:IDisposable
{
    void WriteFileData(string fileName, Stream data);
    Stream ReadFileData(string filename);
    void DeleteFileData(string filename);
    void ClearAllData();
    void WriteMetaFileData(string filename, string path, Stream data);
    Stream ReadMetaFileData(string filename, string path);
    void DeleteMetaFileData(string filename, string path);
    void ClearMetaFilesData(string filename);
}

Essentially my IFileSystemAdaptor interface exposes a flat list of files, that can also be associated with additional meta data files.

As you can see I'm using references to generic Stream objects to abstract the interface to a file's data. This way one implementation of a Catalog could return files from a hard disk, while another could return the data from a web server.

Now I'm trying to figure out how to keep my program from leaving streams open. Is there a rule of thumb for what members should close streams? Should the consumer of a stream close it, or should the member that original created the stream be responsible for closing it.

like image 816
Eric Anastas Avatar asked Sep 17 '10 21:09

Eric Anastas


People also ask

Do we have to close the stream always?

You should always close a stream in order to free open resources on your OS. Opening a stream always returns an identifier which you can use to close it wherever you are in your code (as long as the identifier is valid), whether their from another method or class.

Why do we need to close streams?

yes you need to close stream because, the stream is already full with content and when you close the stream then you can use it again. also data is flush in drive when use flush method. when you close the stream JVM will see that stream is not can be use for further operation.

Do you need to close streams in Java?

Streams have a BaseStream. close() method and implement AutoCloseable, but nearly all stream instances do not actually need to be closed after use. Generally, only streams whose source is an IO channel (such as those returned by Files. lines(Path, Charset)) will require closing.


2 Answers

My Rules:

Should the consumer of a stream close it

  1. If I return a stream from a method, the consumer is responsible. I'm giving it to you, It's your responsilibity.

  2. If I accept a stream as a parameter in a method, I don't close it. When exiting the method, I don't know if the calling method still needs it. It's your stream, I'm just borrowing it, and I don't want to mess you up.

  3. If I create a stream and pass it to another method, my method closes it (or tries to) when I am done with it. I don't know what you are going to do with it, but it's my stream, so I am responsible for it.

like image 192
kemiller2002 Avatar answered Oct 08 '22 23:10

kemiller2002


My spontaneous thought in this case is that the consumer should hold the responsibility for closing the streams. An IFileSystemAdaptor can't know when the consumer is done using the stream, so it also can't decide when to close it.

like image 2
Fredrik Mörk Avatar answered Oct 09 '22 01:10

Fredrik Mörk