Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #1203 (new enhancement)

Opened 1 month ago

Last modified 4 days ago

NullStream

Reported by: fawzi Assigned to: kris
Priority: major Milestone: 0.99.8
Component: Core Functionality Version: 0.99.6 Jeff
Keywords: Cc: larsivi

Description

I wanted to have a NullStream?, i.e. something like /dev/null that discards everything that is written to it. This might be something that tango could offer.

Anyway I did the following:

module frm.NullStream;
import tango.io.model.IConduit;
class NullStream: OutputStream {
    this(){}

    uint write (void[] src){
        return src.length;
    }

    OutputStream copy (InputStream src){
        if (src !is null){
            ubyte[512] buf;
            uint count;
            do {
                count = src.read(buf[]);
            } while(count != Eof)
        }
        return this;
    }
    
    IConduit conduit () { return null; } // ok??

    void close () { } // do something to avoid successive writes?

    OutputStream flush () { return this; }
}

Writing it I noted that the conduit is (unlike what I would have thought) considered a real basic concept, and a stream has its conduit (I had expected a relation only in the other direction, a conduit has streams). Maybe in some occasions this is useful, but for the NullStream? it is a nuisance, so I just return null as conduit. Is this ok, or should one implement a NullConduit??

In my usage it seem to work, but I wondered about the occasions in which this back ref is needed... So

- add a NullStream? to tango?

- is conduit property in streams really needed? can it be null? maybe some comments in the documentation would be helpful

thanks Fawzi

Attachments

NullConduit.d (1.4 kB) - added by fawzi on 08/25/08 13:02:28.
A null conduit implementation

Change History

07/23/08 10:01:40 changed by larsivi

  • owner changed from sean to kris.

08/03/08 15:18:33 changed by kris

Hi Fawzi,

Tango would do this sort of thing via a conduit, as you noted. The simplest way to do that would be to subclass tango.io.Conduit and implement the read/write methods appropriately

08/11/08 02:01:11 changed by kris

  • cc set to larsivi.

If you create a NullConduit?, we'll add it to tango.io.device

08/25/08 13:02:28 changed by fawzi

  • attachment NullConduit.d added.

A null conduit implementation

08/25/08 13:04:25 changed by fawzi

Added a NullConduit? implementation (and nullStream function).

As variant one could make them singletons, or really check for writes to closed stream.